java程序在netbeans中运行不起来,没有显示错误,但界面不是做的效果。求大神指导
importjava.awt.*;importjava.awt.event.*;publicclassCalculatorextendsFrame{privateText...
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame {
private TextField input = new TextField();
private Button num0 = new Button("0");
private Button num1 = new Button("1");
private Button num2 = new Button("2");
private Button num3 = new Button("3");
private Button num4 = new Button("4");
private Button num5 = new Button("5");
private Button num6 = new Button("6");
private Button num7 = new Button("7");
private Button num8 = new Button("8");
private Button num9 = new Button("9");
private Button point = new Button(".");
private Button add = new Button("+");
private Button minus = new Button("-");
private Button plus = new Button("*");
private Button divide = new Button("/");
private Button equal = new Button("=");
private class WindowCloser extends WindowAdapter {
@Override
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
public void setup() {
Panel buttons = new Panel();
buttons.setLayout(new GridLayout(4, 4, 10, 10));
buttons.add(num0);
buttons.add(num1);
buttons.add(num2);
buttons.add(num3);
buttons.add(num4);
buttons.add(num5);
buttons.add(num6);
buttons.add(num7);
buttons.add(num8);
buttons.add(num9);
buttons.add(point);
buttons.add(add);
buttons.add(minus);
buttons.add(plus);
buttons.add(divide);
buttons.add(equal);
Panel top=new Panel();
top.setLayout(new GridLayout(2,1));
top.add(input);
top.add(buttons);
}
public Calculator() {
setTitle("计算器");
setSize(300,400);
setup();
setVisible(true);
}
public static void main(String []args) {
Calculator c = new Calculator();
}
} 展开
import java.awt.event.*;
public class Calculator extends Frame {
private TextField input = new TextField();
private Button num0 = new Button("0");
private Button num1 = new Button("1");
private Button num2 = new Button("2");
private Button num3 = new Button("3");
private Button num4 = new Button("4");
private Button num5 = new Button("5");
private Button num6 = new Button("6");
private Button num7 = new Button("7");
private Button num8 = new Button("8");
private Button num9 = new Button("9");
private Button point = new Button(".");
private Button add = new Button("+");
private Button minus = new Button("-");
private Button plus = new Button("*");
private Button divide = new Button("/");
private Button equal = new Button("=");
private class WindowCloser extends WindowAdapter {
@Override
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
public void setup() {
Panel buttons = new Panel();
buttons.setLayout(new GridLayout(4, 4, 10, 10));
buttons.add(num0);
buttons.add(num1);
buttons.add(num2);
buttons.add(num3);
buttons.add(num4);
buttons.add(num5);
buttons.add(num6);
buttons.add(num7);
buttons.add(num8);
buttons.add(num9);
buttons.add(point);
buttons.add(add);
buttons.add(minus);
buttons.add(plus);
buttons.add(divide);
buttons.add(equal);
Panel top=new Panel();
top.setLayout(new GridLayout(2,1));
top.add(input);
top.add(buttons);
}
public Calculator() {
setTitle("计算器");
setSize(300,400);
setup();
setVisible(true);
}
public static void main(String []args) {
Calculator c = new Calculator();
}
} 展开
展开全部
不知道你要的效果具体是怎么样的。
但是这里
Panel top=new Panel();
top.setLayout(new GridLayout(2,1));
top.add(input);
top.add(buttons);
改为:
Panel top=new Panel();
top.setLayout(new BorderLayout());
top.add(input,BorderLayout.NORTH);
top.add(buttons,BorderLayout.CENTER);
this.add(top);//应该要加上这一句吧。
这样计算器的效果应该更好。
但是这里
Panel top=new Panel();
top.setLayout(new GridLayout(2,1));
top.add(input);
top.add(buttons);
改为:
Panel top=new Panel();
top.setLayout(new BorderLayout());
top.add(input,BorderLayout.NORTH);
top.add(buttons,BorderLayout.CENTER);
this.add(top);//应该要加上这一句吧。
这样计算器的效果应该更好。
更多追问追答
追问
你这的改法是布局上的区别,我觉得最后把两个面板合并时用BorderLayout和用我那个其实是差不多,现在问题是无论我用哪个,计算器界面总是显示一片空白,什么效果都没有
追答
this.add(top);//这句加没有。
还有:
public Calculator() {
setTitle("计算器");
setup();
setSize(300,400);
setVisible(true);
}
//这里最好这样。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询