Java:实现一个能够改变显示单词的字体风格和大小的GUI【V2】(第十四周)

阅读: 评论:0

Java:实现一个能够改变显示单词的字体风格和大小的GUI【V2】(第十四周)

Java:实现一个能够改变显示单词的字体风格和大小的GUI【V2】(第十四周)

题目来源:大工慕课 链接
作者:Caleb Sung

题目要求

Implement a GUI as shown in the following figure:

Input Menu:



Edit Menu:


Tool Bar:


参考解答

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToolBar;public class TestFontGUI {public static void main(String[] args) {new FontGUI();}
}@SuppressWarnings("serial")
class FontGUI extends JFrame {int fontSize;JLabel label = new JLabel("The quick brown fox jumps over the lazy dog.");JRadioButtonMenuItem largeItem = new JRadioButtonMenuItem("Large");JRadioButtonMenuItem mediumItem = new JRadioButtonMenuItem("Medium");JRadioButtonMenuItem smallItem = new JRadioButtonMenuItem("Small");JCheckBoxMenuItem boldItem = new JCheckBoxMenuItem("Bold");JCheckBoxMenuItem italicItem = new JCheckBoxMenuItem("Italic");Dimension screenSize = DefaultToolkit().getScreenSize();String input;public FontGUI() {setTitle("Menu Test Frame");setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);label.setFont(new Font("Serif", Font.PLAIN, 25));add(label, BorderLayout.CENTER);JMenuBar mb = new JMenuBar();setJMenuBar(mb);JMenu inputMenu = new JMenu("Input");mb.add(inputMenu);JMenuItem newItem = new JMenuItem("New");inputMenu.add(newItem);newItem.addActionListener(e -> {input = JOptionPane.showInputDialog("Type here:");if (input == null) {JOptionPane.showConfirmDialog(null, "You must input something in this textbox!", "Attention",JOptionPane.DEFAULT_OPTION);input = "The quick brown fox jumps over the lazy dog.";} else if (input.length() <= 4) {JOptionPane.showConfirmDialog(null, "The length of your input should be longer than 4 charactors!","Attention", JOptionPane.DEFAULT_OPTION);input = "The quick brown fox jumps over the lazy dog.";} elselabel.setText(input);pack();setLocation(screenSize.width / 2 - getSize().width / 2, screenSize.height / 2 - getSize().height / 2);});JToolBar tb = new JToolBar();JButton redButton = new JButton(new ImageIcon("red-ball.gif"));tb.add(redButton);JButton greenButton = new JButton(new ImageIcon("green-ball.gif"));tb.add(greenButton);JButton blueButton = new JButton(new ImageIcon("blue-ball.gif"));tb.add(blueButton);add(tb, BorderLayout.NORTH);redButton.addActionListener(e -> label.setForeground(Color.RED));greenButton.addActionListener(e -> label.setForeground(Color.GREEN));blueButton.addActionListener(e -> label.setForeground(Color.BLUE));JMenu editMenu = new JMenu("Edit");mb.add(editMenu);JMenu colorMenu = new JMenu("Color");editMenu.add(colorMenu);JMenuItem redItem = new JMenuItem("Red");JMenuItem greenItem = new JMenuItem("Green");JMenuItem blueItem = new JMenuItem("Blue");colorMenu.add(redItem);colorMenu.add(greenItem);colorMenu.add(blueItem);redItem.addActionListener(e -> label.setForeground(Color.RED));greenItem.addActionListener(e -> label.setForeground(Color.GREEN));blueItem.addActionListener(e -> label.setForeground(Color.BLUE));JMenu fontMenu = new JMenu("Font");editMenu.add(fontMenu);fontMenu.add(boldItem);fontMenu.add(italicItem);ButtonGroup bg = new ButtonGroup();bg.add(smallItem);bg.add(mediumItem);bg.add(largeItem);fontMenu.add(smallItem);fontMenu.add(mediumItem);fontMenu.add(largeItem);smallItem.setSelected(true);boldItem.addActionListener(e -> {changeFont();});italicItem.addActionListener(e -> {changeFont();});smallItem.addActionListener(e -> {changeFont();});mediumItem.addActionListener(e -> {changeFont();});largeItem.addActionListener(e -> {changeFont();});pack();setLocation(screenSize.width / 2 - getSize().width / 2, screenSize.height / 2 - getSize().height / 2);}void changeFont() {int mode = Font.PLAIN;if (boldItem.isSelected())mode += Font.BOLD;if (italicItem.isSelected())mode += Font.ITALIC;if (smallItem.isSelected())fontSize = 25;else if (mediumItem.isSelected())fontSize = 40;else if (largeItem.isSelected())fontSize = 60;label.setFont(new Font("Serif", mode, fontSize));pack();validate();setLocation(screenSize.width / 2 - getSize().width / 2, screenSize.height / 2 - getSize().height / 2);}
}

本文发布于:2024-02-04 08:25:54,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170703064053944.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:单词   大小   字体   风格   Java
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23