编写一个应用程序,有一个标题为“计算”的窗口,窗口的布局为FlowLayout布局。设计4个按钮“加”“差”“积”“除”。单击相应的按钮,将两个文本框的数字做运算。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Dzj {
    public static void main(String[] args) {
        new DzjWin("计算");
    }
}
class DzjWin extends JFrame implements ActionListener {
    Box Vbox;
    JPanel panel;
    JLabel label;
    JTextField inputField_1,inputField_2,outputField;
    JButton[]buttons = new JButton[4];
    Font font = new Font("Kaiti",Font.BOLD,25);
    DzjWin(String s){
        init();
        setContentPane(panel);
        setTitle(s);
        setVisible(true);
        setSize(800,300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    void init(){
        panel = new JPanel();
        inputField_1 = new JTextField(10);
        inputField_2 = new JTextField(10);
        outputField = new JTextField(10);
        Vbox = Box.createVerticalBox();
        label = new JLabel("=");
        for(int i=0;i<buttons.length;i++){
            switch (i){
                case 0:buttons[i] = new JButton("加");
                break;
                case 1:buttons[i] = new JButton("差");
                    break;
                case 2:buttons[i] = new JButton("积");
                    break;
                case 3:buttons[i] = new JButton("除");
                    break;
            }
            buttons[i].addActionListener(this);
            Vbox.add(buttons[i]);
        }
        inputField_1.setFont(font);
        inputField_2.setFont(font);
        outputField.setFont(font);
        label.setFont(font);
        panel.add(inputField_1);
        panel.add(Vbox);
        panel.add(inputField_2);
        panel.add(label);
        panel.add(outputField);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==buttons[0]){
            try{
                double a = Double.parseDouble(inputField_1.getText());
                double b = Double.parseDouble(inputField_2.getText());
                double result = a+b;
                outputField.setText(""+result);
            }
            catch (NumberFormatException ex){
                JOptionPane.showMessageDialog(this,"您输入了非法数字","消息对话框",
                        JOptionPane.WARNING_MESSAGE);
            }
        }
        else if(e.getSource()==buttons[1]){
            try{
                double a = Double.parseDouble(inputField_1.getText());
                double b = Double.parseDouble(inputField_2.getText());
                double result = a-b;
                outputField.setText(""+result);
            }
            catch (NumberFormatException ex){
                JOptionPane.showMessageDialog(this,"您输入了非法数字","消息对话框",
                    JOptionPane.WARNING_MESSAGE);
            }
        }
        else if(e.getSource()==buttons[2]){
            try{
                double a = Double.parseDouble(inputField_1.getText());
                double b = Double.parseDouble(inputField_2.getText());
                double result = a*b;
                outputField.setText(""+result);
            }
            catch (NumberFormatException ex){
                JOptionPane.showMessageDialog(this,"您输入了非法数字","消息对话框",
                        JOptionPane.WARNING_MESSAGE);
            }
        }
        else if(e.getSource()==buttons[3]){
            try{
                double a = Double.parseDouble(inputField_1.getText());
                double b = Double.parseDouble(inputField_2.getText());
                double result = a/b;
                outputField.setText(String.format("%.3f",result));
            }
            catch (NumberFormatException ex){
                JOptionPane.showMessageDialog(this,"您输入了非法数字","消息对话框",
                        JOptionPane.WARNING_MESSAGE);
            }
        }
    }
}

《编写一个应用程序,有一个标题为“计算”的窗口,窗口的布局为FlowLayout布局。设计4个按钮“加”“差”“积”“除”。单击相应的按钮,将两个文本框的数字做运算。》

 

    原文作者:章鱼保罗海鲜
    原文地址: https://blog.csdn.net/weixin_54440432/article/details/121354091
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞