实验四 Java图形界面与事件处理

发布时间 2023-05-09 22:04:01作者: 梦羽儿

实验目的

1. 掌握Java语言中AWT和Swing组件的基本用法
2. 掌握Java语言中的事件处理方法
3. 掌握Java语言中事件源、监视器和处理事件的接口的概念
图形用户界面设计程序(ArtFont.java)

要求:设计一个文字字体设置窗体,在该窗体中可以设置要显示文字的字体内容,包括字体名称、字体大小、粗体和斜体等字体风格。并模拟在不同操作系统下的显示效果。添加事件处理机制,要求实现如下功能:

当在文本框中输入文字后回车,在文本域中显示输入的文字。
当分别选择粗体和斜体复选框时,文本域中的文字分别显示粗体和斜体样式。
当点击颜色按钮时,出现颜色选择对话框,选择需要的颜色,按确定按钮后,按钮的前景色和文本域的前景色设置为选定的颜色。
当选择字体样式下拉框中的某一字体样式时,文本域中的文字设置为指定的字体样式。
当选择字体大小下拉框中的某一字体大小时,文本域中的文字设置为指定的字体大小。
当选择窗体样式下拉框中的某一窗体效果时,窗体外观改变为指定的窗体外观。

整个窗体继承于JFrame,采用BorderLayout布局。可以在窗体中添加三个面板,分别位于窗体的北部、中部和南部,然后分别在各个面板中添加其它组件,并逐步完善程序功能。

程序源代码

package lab4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class ArtFont extends JFrame implements  ActionListener, ItemListener {
       JComboBox fontType;//字体样式下拉框
       JComboBox fontSize;//字体大小下拉框
       JComboBox windowStyle;//窗体样式下拉框
       JCheckBox boldBx;// 粗体按钮
       JCheckBox italicBx;// 斜体按钮
       JButton colorBtn;// 颜色按钮;
       String[] fontNames;// 字体名称;
       String[] fontSizes;// 字体大小;
       JLabel label;// 输入提示标签;
       JTextField inputText;// 文字输入框;
       JTextArea txtArea;// 文字显示区;
       JPanel northPanel;// 字体设置;
       JPanel centerPanel;// 显示效果区
       JPanel southPanel;//样式设置
       Font font;
       int boldStyle, italicStyle, underlineStyle;
       int fontSizeStyle;
       String fontNameStyle;
       Color colorStyle = Color.black;// 设置字体的默认颜色为黑色;
       String[] style = { "默认显示效果", "Windows显示效果", "Unix显示效果" };
       public ArtFont() {
           super("字体设置");
           // 设置默认字体
           boldStyle = 0;
           italicStyle = 0;
           underlineStyle = 0;
           fontSizeStyle = 20;
           fontNameStyle="宋体";
           font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);
           northPanel = getNorthPanel();
           centerPanel = getCenterPanel();
           southPanel = getSouthPanel();
           // 设置容器;
           Container container = getContentPane();
           container.setLayout(new BorderLayout());
//           centerPanel.setLayout(new FlowLayout());//流式布局
           //将northPanel添加到窗体的北部
           container.add(northPanel,BorderLayout.NORTH);
           //将centerPanel添加到窗体的中部
           container.add(centerPanel,BorderLayout.CENTER);
           //将southPanel添加到窗体的南部
           container.add(southPanel,BorderLayout.SOUTH);
           setSize(500, 300);
           //将窗体位于屏幕的中央
           container.add(northPanel,BorderLayout.NORTH);
           setLocationRelativeTo(null);
           setVisible(true);
       }
       private JPanel getNorthPanel() {
           JPanel panel = new JPanel();
           label = new JLabel("输入:");
           label.setFont(new Font("微软雅黑", Font.PLAIN, 20)); // 设置字体为微软雅黑,普通样式,大小为20
           JLabel name =new JLabel("计z2006 202062216 张驰");
           name.setForeground(Color.RED);
           inputText=new JTextField(5);
           boldBx =new JCheckBox("粗体");
           colorBtn=new JButton("颜色");
           italicBx=new JCheckBox("斜体");
           //添加监听事件:
           inputText.addActionListener(this);
           colorBtn.addActionListener(this);
           boldBx.addItemListener(this);
           italicBx.addItemListener(this);
           panel.add(label);
           panel.add(inputText);
           panel.add(boldBx);
           panel.add(italicBx);
           panel.add(colorBtn);
           panel.add(name);
//           // 将颜色按钮的大小设置为原来的两倍
//           Dimension buttonSize = colorBtn.getPreferredSize();
//           buttonSize.width *= 2;
//           colorBtn.setPreferredSize(buttonSize);
           // 将按钮的字体大小设置为20
           Font buttonFont = new Font(colorBtn.getFont().getName(), Font.PLAIN, 20);
           colorBtn.setFont(buttonFont);
           boldBx.setFont(buttonFont);
           italicBx.setFont(buttonFont);
           return panel;
       }
       private JPanel getCenterPanel() {
           JPanel panel = new JPanel();
           txtArea=new JTextArea(10,10);
           panel.setLayout(new BorderLayout());
           panel.add(new JScrollPane(txtArea),BorderLayout.CENTER);
           return panel;
       }
       private JPanel getSouthPanel() {
           JPanel panel = new JPanel();
           //获得系统默认字体
           GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
           fontNames = ge.getAvailableFontFamilyNames();
           fontType = new JComboBox(fontNames);
           //设置字体大小
           fontSizes = new String[63];
           for (int i = 0; i < fontSizes.length; i++) {
               fontSizes[i] = Integer.toString(i+24);
           }
           fontSize = new JComboBox(fontSizes);
           windowStyle = new JComboBox(style);
           fontSize.addItemListener(this);
           fontType.addItemListener(this);
           panel.add(fontType);
           panel.add(fontSize);
           panel.add(windowStyle);
           return panel;
       }
       public static void main(String args[]) {
           ArtFont artFont = new ArtFont();
           artFont.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
       @Override
       public void actionPerformed(ActionEvent e) {
           if (e.getSource() == inputText) {
               txtArea.setText(inputText.getText());
           }
           else if (e.getSource() == colorBtn) {
               colorStyle=JColorChooser.showDialog(this,"请选择一种颜色",colorStyle);
               colorBtn.setForeground(colorStyle);
               txtArea.setForeground(colorStyle);
           }
       }
       @Override
       public void itemStateChanged(ItemEvent e) {
           if (e.getSource() == boldBx) {
               if (boldBx.isSelected()){
                   boldStyle=Font.BOLD;
               }
               else{
                   boldStyle=Font.PLAIN;
               }
           } else if (e.getSource() == italicBx) {
               if (italicBx.isSelected()){
                   italicStyle=Font.ITALIC;
               }
               else{
                   italicStyle=Font.PLAIN;
               }
               txtArea.setFont(font);
           } else if (e.getSource() == fontType) {
               fontNameStyle=(String) e.getItem();
           } else if (e.getSource() == fontSize) {
               fontSizeStyle=Integer.parseInt((String) e.getItem());
           } else if (e.getSource() == windowStyle) {
               String s = (String) e.getItem();
               String className = "";
               if (s.equals("Windows显示效果"))
                   className = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
               else if (s.equals("Unix显示效果"))
                   className = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
               else if (s.equals("默认显示效果"))
                   className = UIManager.getCrossPlatformLookAndFeelClassName();
               try {
                   UIManager.setLookAndFeel(className);
                   SwingUtilities.updateComponentTreeUI(this);
               } catch (Exception de) {
                   System.out.println("Exception happened!");
               }
           }
           font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);
           txtArea.setFont(font);
       }
    }

程序运行截图

总结

这次实验令我感到有些挑战和困难,在这个过程中,我遇到不少错误和障碍,但这也是学习过程中不可避免的一部分,毕竟编程需要细心和耐心。好在最终磨了一下午+一晚上还是完成了,好耶~