package awt;import java.awt.*;
import java.awt.event.*;
public class ExGui{
private Frame f;
private Button b1;
private Button b2;
public ExGui(){
// this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
//这段code放在下面的函数里也不能起到,一点窗口的叉子就关闭窗口的作用。
}
private void addWindowListener(WindowAdapter windowAdapter) {
// TODO Auto-generated method stub
}
public static void main(String args[]){
ExGui that = new ExGui();
that.go();
} public void go(){
f = new Frame("GUI example");
f.setLayout(new FlowLayout());
//设置布局管理器为FlowLayout
b1 = new Button("Press Me");
//按钮上显示字符"Press Me"
b2 = new Button("Don't Press Me");
f.add(b1);
f.add(b2);
f.pack();
//紧凑排列,其作用相当于setSize(),即让窗口尽量小,小到刚刚能够包容住b1、b2两个按钮
f.setVisible(true);
}
}
import java.awt.event.*;
public class ExGui{
private Frame f;
private Button b1;
private Button b2;
public ExGui(){
// this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
//这段code放在下面的函数里也不能起到,一点窗口的叉子就关闭窗口的作用。
}
private void addWindowListener(WindowAdapter windowAdapter) {
// TODO Auto-generated method stub
}
public static void main(String args[]){
ExGui that = new ExGui();
that.go();
} public void go(){
f = new Frame("GUI example");
f.setLayout(new FlowLayout());
//设置布局管理器为FlowLayout
b1 = new Button("Press Me");
//按钮上显示字符"Press Me"
b2 = new Button("Don't Press Me");
f.add(b1);
f.add(b2);
f.pack();
//紧凑排列,其作用相当于setSize(),即让窗口尽量小,小到刚刚能够包容住b1、b2两个按钮
f.setVisible(true);
}
}
// this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
//这段code放在下面的函数里也不能起到,一点窗口的叉子就关闭窗口的作用。
}
-------------------------------------------------------------------
你这段代码没有错,我看了,你把this换成f就可以了.
import java.awt.event.*;
public class ExGui{
private Frame f;
private Button b1;
private Button b2;
public ExGui(){
Frame f = new Frame();
f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
}
private void addWindowListener(WindowAdapter windowAdapter) {
// TODO Auto-generated method stub
}
public static void main(String args[]){
ExGui that = new ExGui();
that.go();
} public void go(){
Frame f = new Frame("GUI example");
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
//设置布局管理器为FlowLayout
b1 = new Button("Press Me");
//按钮上显示字符"Press Me"
b2 = new Button("Don't Press Me");
f.add(b1);
f.add(b2);
f.pack();
f.setSize(555,666);
//紧凑排列,其作用相当于setSize(),即让窗口尽量小,小到刚刚能够包容住b1、b2两个按钮
f.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
public class ExGui extends Frame implements ActionListener{
private Frame f= new Frame("GUI example");
private Button b1;
private Button b2;
public ExGui(){
Frame f = new Frame();
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
//设置布局管理器为FlowLayout
b1 = new Button("Press Me");
//按钮上显示字符"Press Me"
b2 = new Button("Don't Press Me");
f.add(b1);
f.add(b2);
f.pack();
f.setSize(555,666);
//紧凑排列,其作用相当于setSize(),即让窗口尽量小,小到刚刚能够包容住b1、b2两个按钮
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
}
public void actionPerformed(ActionEvent ae)
{
}
public static void main(String args[]){
ExGui that = new ExGui();
}
}
import java.awt.*;
import java.awt.event.*;public class ExGui extends Frame {
private Button b1;
private Button b2;
public ExGui() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public static void main(String args[]) {
ExGui that = new ExGui();
that.go();
} public void go() {
setLayout(new FlowLayout(FlowLayout.RIGHT));
b1 = new Button("Press Me");
b2 = new Button("Don't Press Me");
add(b1);
add(b2);
setSize(555, 666);
setVisible(true);
}}// 组合方式使用Frame
import java.awt.*;
import java.awt.event.*;public class ExGui {
private Frame f;
private Button b1;
private Button b2;
public ExGui() {
f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public static void main(String args[]) {
ExGui that = new ExGui();
that.go();
} public void go() {
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
b1 = new Button("Press Me");
b2 = new Button("Don't Press Me");
f.add(b1);
f.add(b2);
f.setSize(555, 666);
f.setVisible(true);
}}另外,setSize()和pack()用其中一个就可以了。