import java.awt.*;
public class Text {
public static void main(String[] args) {
Frame f1=new Frame("myframewithPanel");
f1.setLayout(null);
f1.setBackground(Color.green);
f1.setBounds(200,200,300,300);
f1.setVisible(true);
f1.setResizable(true);
//Panel p1=new Panel(
Button bb=new Button("Button1");
bb.setBounds(50,50,50,50);
f1.add(new Button("Button3"),FlowLayout.LEFT);//这行为什么不起作用呀??????
f1.add(bb);
Button b1=new Button("Button2");
b1.setBackground(Color.blue);
b1.setBounds(100,100,50,50);
f1.add(b1);
}
}
public class Text {
public static void main(String[] args) {
Frame f1=new Frame("myframewithPanel");
f1.setLayout(null);
f1.setBackground(Color.green);
f1.setBounds(200,200,300,300);
f1.setVisible(true);
f1.setResizable(true);
//Panel p1=new Panel(
Button bb=new Button("Button1");
bb.setBounds(50,50,50,50);
f1.add(new Button("Button3"),FlowLayout.LEFT);//这行为什么不起作用呀??????
f1.add(bb);
Button b1=new Button("Button2");
b1.setBackground(Color.blue);
b1.setBounds(100,100,50,50);
f1.add(b1);
}
}
你是说left不起作用对吧?那是因为你自己都设定了位置(50,50).
查看各种布局,最好不要混用
Frame 默认的布局管理器是Borderlayout
Panel 默认的布局管理器是FlowLayout所以你那句要起作用必须先new一个FlowLayout
修改后的代码import java.awt.*;
public class Text {
public static void main(String[] args) {
Frame f1=new Frame("myframewithPanel");
FlowLayout ff = new FlowLayout();f1.setLayout(ff);
f1.setBackground(Color.green);
f1.setBounds(200,200,300,300);
f1.setVisible(true);
f1.setResizable(true); //Panel p1=new Panel(
Button bb=new Button("Button1");
bb.setBounds(50,50,50,50);
f1.add(new Button("Button3"),ff.LEFT);//这样就起作用了
f1.add(bb);
Button b1=new Button("Button2");
b1.setBackground(Color.blue);
b1.setBounds(100,100,50,50);
f1.add(b1);
}
}