import java.awt.*;
import javax.swing.*;
public class calculator extends JFrame{
String buttonname[] = {"0","1","2","3","4","5","6","7","8","9"};
String button[];
public calculator(){
//get a container and set its layout
Container box = getContentPane();
//now apply some attributes
setTitle("Calculator");
setSize(250,350);
setLayout(new GridLayout(3,1));
setResizable(false);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//begin to create components
//3+3+2 panels
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p31 = new JPanel();
JPanel p32 = new JPanel();
JPanel p33 = new JPanel();
JPanel p311 = new JPanel();
JPanel p312 = new JPanel();
//10 buttons with numbers 0-9
JButton[] btn = new JButton[10];
for(int i =0;i<10;i++){
btn[i] = new JButton(buttonname[i]);
}
//another 8 buttons with operators
JButton btnjia = new JButton("+");
JButton btnjian = new JButton("-");
JButton btnchen = new JButton("*");
JButton btnchu = new JButton("/");
JButton btndian = new JButton(".");
JButton btntui = new JButton("←");
JButton btnfu = new JButton("C");
JButton btndeng = new JButton("=");
//an text field to show the result
JTextField txtresult = new JTextField("0",230);
//components create over
//add the components to the panels and set layout
//add the first 3 panels
box.add(p1);
box.add(p2);
box.add(p3);
box.setLayout(new GridLayout(3,1,10,10));
//add the text field to p1 and set layout
p1.add(txtresult);
p1.setLayout(new GridLayout(1,1));
}
//create the main method
public static void main (String[] args){
new calculator();
}
}
java问题,为什么这段代码运行后窗体里面没有文本框显示?
答案:1 悬赏:70 手机版
解决时间 2021-03-17 01:15
- 提问者网友:却不属于对方
- 2021-03-16 19:12
最佳答案
- 五星知识达人网友:你哪知我潦倒为你
- 2021-03-16 20:23
1. JTextField txtresult = new JTextField("0",230);这里230改成30先,230太长了。(可以不用改)
2. 在 p1.setLayout(new GridLayout(1,1));这段后面加上pack();
搞定!
2. 在 p1.setLayout(new GridLayout(1,1));这段后面加上pack();
搞定!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯