Java Chapter 11 Graphical User Interface Programming

Experiment content:

      1. Calculate the area of ​​the circle.

      2. Four arithmetic units.

Experimental steps:

1. Calculate the area of ​​the circle. Enter the radius of the circle in the first text box, then calculate the value of the circle area, and put this value into the second text box.

Source code:

package homework.实验11_图形用户界面;

import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class Circle extends Frame implements ActionListener {
    private static final long serialVersionUID = 1L;
    static TextField tf1 = new TextField();
    static TextField tf2 = new TextField();
    static Label lab1 = new Label("radius");
    static Button bt = new Button("area");

    public static void main(String[] args){
        Circle c = new Circle();
        c.setTitle("求面积");
        c.setLayout(null);
        c.setVisible(true);
        c.setSize(400,400);
        c.add(lab1);
        c.add(bt);
        c.add(tf1);
        c.add(tf2);
        lab1.setBounds(40, 90, 110, 35);
        tf1.setBounds(170, 90, 110, 35);
        bt.setBounds(40, 180, 110, 35);
        tf2.setBounds(170, 180, 110, 35);
        bt.addActionListener(c); //注册事件监听器
        c.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });  //添加一个监听器,重写windowClosing方法,才能关闭窗口

    }
    public void actionPerformed(ActionEvent e){
        int r = Integer.parseInt(tf1.getText());
        float s = (float) ((float)r*r*Math.PI);
        String str = String.valueOf(s);
        tf2.setText(str);

    }

}

 

Screenshot of running result:

 

2. Create 3 text boxes on the form, two are used to input the operation object, the other is used to store the calculation result, the drop-down list box selects four arithmetic symbols.

Source code:

package homework.实验11_图形用户界面;

import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;


public class calculated extends JFrame {
    private static final long serialVersionUID = 1L;
    JTextField num1 = new JTextField();
    JTextField num2 = new JTextField();
    JTextField total = new JTextField();
    static Choice ch = new Choice();

    public static void main(String[] args){
        calculated c = new calculated();
        c.setTitle("计算器");
        c.operation();
        c.setBackground(Color.cyan);
        c.setSize(400,300);
        c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //关闭窗口
    }

    public void operation(){
        ch.add("+");
        ch.add("-");
        ch.add("*");
        ch.add("/");
        num1.setColumns(5);  //设置此文本框的列数
        num2.setColumns(5);
        total.setColumns(5);
        setLayout(new FlowLayout());  //采用流式布局管理器
        Button bt = new Button("=");
        bt.setBackground(Color.cyan);
        bt.addActionListener(new MyListener(this));  //bt注册事件监听器
        ch.addItemListener(new ChoiceHandler());   //ch注册事件监听器
        add(num1);  //将文本框加入到窗体
        add(ch);
        add(num2);
        add(bt);
        add(total);
        setVisible(true); //设置窗体可见
    }

}
    class MyListener implements ActionListener {
    private calculated mulp;
    public MyListener(calculated mulp){
        this.mulp = mulp;
    }
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                String s1 = mulp.num1.getText();
                String s2 = mulp.num1.getText();
                int i1 = Integer.parseInt(s1);
                int i2 = Integer.parseInt(s2);
                String itm;
                itm = ChoiceHandler.itml;
                if(itm.equals("+")){
                    mulp.total.setText(String.valueOf(i1+i2));
                }else if(itm.equals("-")){
                    mulp.total.setText(String.valueOf(i1-i2));
                }else if(itm.equals("*")){
                    mulp.total.setText(String.valueOf(i1*i2));
                }else if(itm.equals("/")){
                    mulp.total.setText(String.valueOf(i1/i2));
                }
            }catch(Exception e2){
                JOptionPane.showMessageDialog(null,"除数不能为0");
            }

        }
    }


class ChoiceHandler implements ItemListener {
    static String itml;
    @Override
    public void itemStateChanged(ItemEvent e) {
        itml = calculated.ch.getSelectedItem();
    }
}

Screenshot of running result:

Experiment summary

     Swing: A set of graphical interface system established on the basis of awt, which belongs to the Javax extension package. More components are provided. And it is completely implemented by Java to enhance the portability, which is a lightweight control.

        The classes contained in the GUI API can be divided into three groups: component class, container class, and helper class. Their architecture is as follows:

 

Guess you like

Origin blog.csdn.net/qq_45176548/article/details/112393400