AB347 2021-06-30 08:31 采纳率: 100%
浏览 52
已采纳

javaswing编程?

用java

img 设计一个图形用户界面。界面中包括三个标签、三个文本框和一个按钮。三个标 签分别是【商品1数量】、【商品2数量】、【总量】。按钮的标题为【计算总量】。要求在文本框中输入商品1数量、商品2数量,单击计算总量按钮后在文本框中显示总量。

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-06-30 08:33
    关注

    用swing画窗体,加上几个组件。以及事件处理。 如有帮助,请采纳。

    img

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    public class SummationForm extends JFrame implements ActionListener {
        private static final long serialVersionUID = -3059616600875760053L;
        JLabel labMathematics, labEnglish, labTotal;
        JTextField textMathematics, textEnglish, textTotal;
        static JButton butSummation;
        public SummationForm() {
            labMathematics = new JLabel("商品数量1");
            labEnglish = new JLabel("商品数量2");
            labTotal = new JLabel("总量");
            textMathematics = new JTextField("", 10);
            textEnglish = new JTextField("", 10);
            textTotal = new JTextField("", 10);
            butSummation = new JButton("计算总量");
            labMathematics.setBounds(30, 30, 120, 30);
            textMathematics.setBounds(180, 30, 120, 30);
            labEnglish.setBounds(30, 80, 120, 30);
            textEnglish.setBounds(180, 80, 120, 30);
            labTotal.setBounds(30, 130, 120, 30);
            textTotal.setBounds(180, 130, 120, 30);
            butSummation.setBounds(160, 180, 120, 30);
            add(labMathematics);
            add(textMathematics);
            add(labEnglish);
            add(textEnglish);
            add(labTotal);
            add(textTotal);
            add(butSummation);
            setLayout(null);
            setBounds(200, 100, 400, 300);
            // 窗口在屏幕中间显示
            setLocationRelativeTo(null);
            setTitle("求总量");
            setResizable(false);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SummationForm summationForm = new SummationForm();
            butSummation.addActionListener(summationForm);
            summationForm.add(butSummation);
        }
    
        public void actionPerformed(ActionEvent e) {
            JButton jb = (JButton) e.getSource();
            if (jb == butSummation) {
                String Mathematics = textMathematics.getText();
                String English = textEnglish.getText();
                try {
                    int a = Integer.parseInt(Mathematics);
                    int b = Integer.parseInt(English);
                    int t = a + b;
                    textTotal.setText(String.valueOf(t));
                } catch (Exception e2) {
                    JOptionPane.showMessageDialog(this, "输入的格式错误!", "提示对话框", JOptionPane.DEFAULT_OPTION);
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • Gemini_Kanon 2021-06-30 08:37
    关注
    import java.awt.Button;
    import java.awt.Label;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    public class Calculator extends JFrame implements ActionListener {
        //创建按钮、文本框
        Button b1, b2, b3, b4;
        JTextField text, text1, res;
        public Calculator() {
            super("一个简单的加法器");
            // 设置布局(设置没有布局,才能自己定位)
            setLayout(null);
            ///操作数1标签
            Label uname = new Label("操作数1:");
            // 加入窗口中
            add(uname);
            // 设置组件位置 (组件的x坐标,组件的y坐标,组件的长,组件的高)
            uname.setBounds(20, 50, 40, 20);
            // 操作数1文本框
            text = new JTextField();
            add(text);
            text.setBounds(65, 50, 200, 20);
            // 操作数2标签
            Label pwd = new Label("操作数2:");
            add(pwd);
            pwd.setBounds(20, 80, 40, 20);
            // 操作数2文本框
            text1 = new JTextField();
            add(text1);
            text1.setBounds(65, 80, 200, 20);
            // 结果文字标签
            Label rest = new Label("计算结果:");
            add(rest);
            rest.setBounds(20, 110, 40, 20);
            // 结果文本框
            res = new JTextField();
            add(res);
            res.setBounds(65, 110, 200, 20);
            // 按钮1
            b1 = new Button("加+");
            add(b1);
            b1.setBounds(300, 80, 80, 30);
            b1.addActionListener(this);
            //设置窗口大小
            setSize(450, 250);
            //设置窗口是否可见,设为false的话看不到窗体
            setVisible(true);
            //设置窗体居中
            setLocationRelativeTo(null);
            //点击关闭按钮解除程序
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        //main方法,new Calculator对象执行程序
        public static void main(String[] args) {
            new Calculator();
        }
        //执行加减乘除方法
        public void actionPerformed(ActionEvent e) {
            //获取两个操作数文本框内容,转成double格式参与准备运算
            double t1 = Double.parseDouble( text.getText() );
            double t2 = Double.parseDouble( text1.getText() );
            res.setText((t1+t2)+"");
        }
    }
    

    把文字换成你图片里的直接运行就可以了

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 I350 Gigabit Network
  • ¥15 关于#abap#的问题,请各位专家解答!
  • ¥20 内网通过公网访问外网问题
  • ¥20 谁有这个东西 继续教育的
  • ¥15 怎么使请求通过cors
  • ¥15 WDM 驱动ACPI 相关疑问
  • ¥15 prism 跨窗体共享数据绑定 wpf
  • ¥15 hdl designer突然用不了系统的moduleware组件,请问有人遇到或者怎么解决吗?
  • ¥15 0基础计算机毕设,应该从哪开始?
  • ¥60 使用DKT40脑图划分ROI区域