当前位置: 技术问答>java相关
请教:基于GUI的java小程序
来源: 互联网 发布时间:2015-06-15
本文导语: //File:SampleExample.java import java.awt.*; public class SampleFrame extends Frame { Panel panel1, panel2; Button button1, button2, button3, button4; Label label1; public SampleFrame() { setTitle(" SampleExample"); setLayout(new FlowLayout()); panel1 =...
//File:SampleExample.java
import java.awt.*;
public class SampleFrame extends Frame {
Panel panel1, panel2;
Button button1, button2, button3, button4;
Label label1;
public SampleFrame() {
setTitle(" SampleExample");
setLayout(new FlowLayout());
panel1 = new Panel();
panel1.setLayout(new BorderLayout());
button1 = new Button(" North");
button2 = new Button(" South");
button3 = new Button(" East");
button4 = new Button(" West");
panel1.add(button1,1);
panel1.add(button2,5);
panel1.add(button3,10);
panel1.add(button4,15);
panel2 = new Panel();
panel2.setLayout(new BorderLayout());
label1 = new Label(" which button");
panel2.add(" Center", label1);
add(panel1);
add(panel2);
pack();
show();
}public void paint(Graphics g) {
Point t1 = panel1.location();
Point t2 = panel2.location();
Dimension d1 = panel1.size();
Dimension d2 = panel2.size();
g.drawRect(t1.x + 1, t1.y + 1, d1.width - 1, d1.height - 1);
g.drawRect(t2.x + 1, t2.y + 1, d2.width - 1, d2.height - 1);
}public boolean action(Event e, Object arg) {
Object target = e.target;
if (target == button1) {
label1.setText("Button button1 is" + button1.getLabel());
return true;
}
if (target == button2) {
label1.setText("Button button2 is" + button2.getLabel());
return true;
}
if (target == button3) {
label1.setText("Button button3 is" + button3.getLabel());
return true;
}
if (target == button4) {
label1.setText("Button button4 is" + button4.getLabel());
return true;
}
return false;
}
public static void main(String arg[]) {
new SampleFrame();
}
}
请指教,我花了好长时间也通不过,问题好象出在add方法上,非常感谢
import java.awt.*;
public class SampleFrame extends Frame {
Panel panel1, panel2;
Button button1, button2, button3, button4;
Label label1;
public SampleFrame() {
setTitle(" SampleExample");
setLayout(new FlowLayout());
panel1 = new Panel();
panel1.setLayout(new BorderLayout());
button1 = new Button(" North");
button2 = new Button(" South");
button3 = new Button(" East");
button4 = new Button(" West");
panel1.add(button1,1);
panel1.add(button2,5);
panel1.add(button3,10);
panel1.add(button4,15);
panel2 = new Panel();
panel2.setLayout(new BorderLayout());
label1 = new Label(" which button");
panel2.add(" Center", label1);
add(panel1);
add(panel2);
pack();
show();
}public void paint(Graphics g) {
Point t1 = panel1.location();
Point t2 = panel2.location();
Dimension d1 = panel1.size();
Dimension d2 = panel2.size();
g.drawRect(t1.x + 1, t1.y + 1, d1.width - 1, d1.height - 1);
g.drawRect(t2.x + 1, t2.y + 1, d2.width - 1, d2.height - 1);
}public boolean action(Event e, Object arg) {
Object target = e.target;
if (target == button1) {
label1.setText("Button button1 is" + button1.getLabel());
return true;
}
if (target == button2) {
label1.setText("Button button2 is" + button2.getLabel());
return true;
}
if (target == button3) {
label1.setText("Button button3 is" + button3.getLabel());
return true;
}
if (target == button4) {
label1.setText("Button button4 is" + button4.getLabel());
return true;
}
return false;
}
public static void main(String arg[]) {
new SampleFrame();
}
}
请指教,我花了好长时间也通不过,问题好象出在add方法上,非常感谢
|
panel2.add(" Center", label1);写反了!
panel2.add( label1," Center");
panel2.add( label1," Center");
|
不要在jframe上直接add(), 用jframe.getContentPane().add()