psogos 2022-05-12 20:15 采纳率: 83.7%
浏览 33
已结题

java程序,想问问为什么窗口内没有内容

java程序,想问问为什么窗口内没有内容

img


package exp_08;

public class Ques9_2 {
    public static void main(String[] args) {
        WindowFrameCal cal=new   WindowFrameCal();
        cal.setTitle("计算");
        cal.setBounds(100,100,500,300);
    }
}

package exp_08;

import javax.swing.*;
import java.awt.*;
public class WindowFrameCal extends JFrame {
    Box box;
    JTextField input1,input2,output;
    JButton[]buttons = new JButton[4];
    Cal ca;
    JPanel panel;
    JLabel label;
    WindowFrameCal()
    {
        init();
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    void init()
    {

        box = Box.createVerticalBox();
        panel =new JPanel();
        input1=new JTextField(10);
        input2=new JTextField(10);
        output=new JTextField(10);
        buttons[0] = new JButton("加");
        buttons[1] = new JButton("减");
        buttons[2] = new JButton("乘");
        buttons[3] = new JButton("除");
        Font f = new Font("宋体",Font.BOLD,28);
        SetFont.setFont
                (f,input1,input2,output,buttons[0],buttons[1],buttons[2],buttons[3]);
        panel.add(input1);
        panel.add(input2);
        panel.add(output);
        panel.add(box);
        for(int i=0;i<buttons.length;i++)
        {
            buttons[i].addActionListener(ca);
            box.add(buttons[i]);
        }
    }
}


package exp_08;

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

public class Cal implements ActionListener {

    WindowFrameCal view;
    public void setView(WindowFrameCal view)
    {
        this.view=view;
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==view.buttons[0]) {
            try {
                double a = Double.parseDouble(view.input1.getText());
                double b = Double.parseDouble(view.input2.getText());
                double ans = a+b;
                view.output.setText("" + ans);
            } catch (NumberFormatException ex) {
                System.out.println("输入非法字符 " + ex.getMessage());
            }
        }
        else if(e.getSource()==view.buttons[1])
        {
            try {
                double a = Double.parseDouble(view.input1.getText());
                double b = Double.parseDouble(view.input2.getText());
                double ans = a-b;
                view.output.setText("" + ans);
            } catch (NumberFormatException ex) {
                System.out.println("输入非法字符 " + ex.getMessage());
            }
        }
        else if(e.getSource()==view.buttons[2])
        {
            try {
                double a = Double.parseDouble(view.input1.getText());
                double b = Double.parseDouble(view.input2.getText());
                double ans = a*b;
                view.output.setText("" + ans);
                }
            catch (NumberFormatException ex) {
                System.out.println("输入非法字符 " + ex.getMessage());
            }

        }
        else if(e.getSource()==view.buttons[3])
        {
            try {
                double a = Double.parseDouble(view.input1.getText());
                double b = Double.parseDouble(view.input2.getText());
                double ans = a/b;
                view.output.setText("" + ans);
            }
            catch (NumberFormatException ex) {
                System.out.println("输入非法字符 " + ex.getMessage());
            }

        }




    }
}


  • 写回答

1条回答 默认 最新

  • 为兴趣而学 2022-05-12 23:49
    关注

    img


    在WindowFrameCal类下的init方法底部添加一行代码:add(panel);

    
     void init()
        {
            box = Box.createVerticalBox();
            panel =new JPanel();
            input1=new JTextField(10);
            input2=new JTextField(10);
            output=new JTextField(10);
            buttons[0] = new JButton("加");
            buttons[1] = new JButton("减");
            buttons[2] = new JButton("乘");
            buttons[3] = new JButton("除");
            Font f = new Font("宋体",Font.BOLD,28);
           // SetFont.setFont
                 //   (f,input1,input2,output,buttons[0],buttons[1],buttons[2],buttons[3]);
            panel.add(input1);
            panel.add(input2);
            panel.add(output);
            panel.add(box);
            for(int i=0;i<buttons.length;i++)
            {
                buttons[i].addActionListener(ca);
                box.add(buttons[i]);
            }
            // 加入此行代码即可显示
            add(panel);
        }
    

    研究不易,望采纳!

    评论
    1人已打赏

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月13日
  • 赞助了问题酬金10元 5月12日
  • 创建了问题 5月12日

悬赏问题

  • ¥15 c++ gmssl sm2验签demo
  • ¥15 关于模的完全剩余系(关键词-数学方法)
  • ¥15 有没有人懂这个博图程序怎么写,还要跟SFB连接,真的不会,求帮助
  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?