今天刚刚学习的 JFrame 比较晕 现在有4种排布 怎么放到一起呢?
public class MyGridLayout extends JFrame{
private String name[]={"Button1","Button2","Button3","Button4"};
private JButton button[] = new JButton[name.length];
public MyGridLayout()
{
super("My GridLayou Test:Caculator");
setLayout(new GridLayout(2,2,4,4));
Container c = this.getContentPane();
for (int i = 0; i < name.length; i++) {
button[i]=new JButton(name[i]);
c.add(button[i]);
}
setSize(200,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
MyGridLayout app = new MyGridLayout();
}
}public class MyFlowLayout extends JFrame { private JButton leftButton, rightButton; public MyFlowLayout() {
super("My FlowLayout Test");
leftButton = new JButton("winson");
rightButton = new JButton("mybole");
setLayout(new FlowLayout(FlowLayout.CENTER));
Container c = this.getContentPane();
c.add(leftButton);
c.add(rightButton);
setSize(200, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public static void main(String[] args) {
MyFlowLayout app = new MyFlowLayout();
}
}
public class MyBorderLayout extends JFrame{
private String name[] ={"North","South","West","East","Center"};
private JButton button[] = new JButton[name.length];
public MyBorderLayout()
{
super("My BorderLayout Test");
for (int i = 0; i < name.length; i++) {
button[i]=new JButton(name[i]);
}
add(button[0],BorderLayout.EAST);
add(button[1],BorderLayout.SOUTH);
add(button[2],BorderLayout.WEST);
add(button[3],BorderLayout.NORTH);
add(button[4],BorderLayout.CENTER);
setSize(300,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
MyBorderLayout app = new MyBorderLayout();
}
}最后那个我不大明白什么意思 图贴不上来我自己画个吧
--------------------------------------------
| N | |---| |----| |
|-------------------| |win| | my | |
| | | | |---| |----| |
| | | | |
| W | C | E | |
| | | | |
| | | | |
|-------------------| |
| S | |
|------------------------------------------
| | | |
| | | |
| b1 | b2 | |
| | | |
|---------|---------| 黑桃A |
| | | |
| | | |
| b3 | b4 | |
| | | |
|--------------------------------------------
public class MyGridLayout extends JFrame{
private String name[]={"Button1","Button2","Button3","Button4"};
private JButton button[] = new JButton[name.length];
public MyGridLayout()
{
super("My GridLayou Test:Caculator");
setLayout(new GridLayout(2,2,4,4));
Container c = this.getContentPane();
for (int i = 0; i < name.length; i++) {
button[i]=new JButton(name[i]);
c.add(button[i]);
}
setSize(200,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
MyGridLayout app = new MyGridLayout();
}
}public class MyFlowLayout extends JFrame { private JButton leftButton, rightButton; public MyFlowLayout() {
super("My FlowLayout Test");
leftButton = new JButton("winson");
rightButton = new JButton("mybole");
setLayout(new FlowLayout(FlowLayout.CENTER));
Container c = this.getContentPane();
c.add(leftButton);
c.add(rightButton);
setSize(200, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public static void main(String[] args) {
MyFlowLayout app = new MyFlowLayout();
}
}
public class MyBorderLayout extends JFrame{
private String name[] ={"North","South","West","East","Center"};
private JButton button[] = new JButton[name.length];
public MyBorderLayout()
{
super("My BorderLayout Test");
for (int i = 0; i < name.length; i++) {
button[i]=new JButton(name[i]);
}
add(button[0],BorderLayout.EAST);
add(button[1],BorderLayout.SOUTH);
add(button[2],BorderLayout.WEST);
add(button[3],BorderLayout.NORTH);
add(button[4],BorderLayout.CENTER);
setSize(300,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
MyBorderLayout app = new MyBorderLayout();
}
}最后那个我不大明白什么意思 图贴不上来我自己画个吧
--------------------------------------------
| N | |---| |----| |
|-------------------| |win| | my | |
| | | | |---| |----| |
| | | | |
| W | C | E | |
| | | | |
| | | | |
|-------------------| |
| S | |
|------------------------------------------
| | | |
| | | |
| b1 | b2 | |
| | | |
|---------|---------| 黑桃A |
| | | |
| | | |
| b3 | b4 | |
| | | |
|--------------------------------------------
CardLayout允许我们在更复杂的拥有真正的文件夹卡片与一条边相遇的环境里创建大致相同于“卡片式对话框”的布局,我们必须压下一个卡片使不同的对话框带到前面来。在AWT里不是这样的:CardLayout是简单的空的空格,我们可以自由地把新卡片带到前面来。(JFC/Swing库包括卡片式的窗格看起来非常的棒,且可以我们处理所有的细节。)
BorderLayout当我们增加一些事物到使用的面板上时我们必须使用add()方法将一个字符串对象作为它的第一个自变量,并且字符串必须指定(正确的大写)“North”(上),“South”(下),“west”(左),“East”(右)或者“Center”。如果我们拼写错误或没有大写,就会得到一个编译时的错误,并且程序片不会像你所期望的那样运行。
FlowLayout。这个简单的“Flow”的组件安装在窗体中,从左到右,直到顶部的空格全部再移去一行,并继续循环这些组件。这些是总结的java swing里面布局,希望楼主多多阅读
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class JobFJ extends JFrame {
private String name[] = { "北", "南", "西", "东", "中" };
private JButton button[] = new JButton[name.length];
private JButton leftButton, rightButton;
private String name1[] = { "button1", "button2", "button3", "button4" };
private JButton buttonn[] = new JButton[name1.length];
private String name2[] = { "黑桃A","红桃A" };
private JButton button2[] = new JButton[name.length];
private JButton button3[] = new JButton[4]; public JobFJ() { super("My BorderLayout Test");
setLayout(new GridLayout(2,2,4,4));
Container c = this.getContentPane();
JPanel Panel1 = new JPanel(new BorderLayout());
JPanel Panel2 = new JPanel(new FlowLayout());
JPanel Panel3 = new JPanel(new GridLayout(2, 2));
JPanel Panel4 = new JPanel(new GridLayout(1, 2));
//button3[0] = new JButton(Panel1.getName());
//button3[1] = new JButton(Panel2.getName());
//button3[2] = new JButton(Panel3.getName());
//button3[3] = new JButton(Panel4.getName());
c.add(Panel1);
c.add(Panel2);
c.add(Panel3);
c.add(Panel4);
for (int i = 0; i < name.length; i++) {
button[i] = new JButton(name[i]);
}
Panel1.add(button[0], BorderLayout.EAST);
Panel1.add(button[1], BorderLayout.SOUTH);
Panel1.add(button[2], BorderLayout.WEST);
Panel1.add(button[3], BorderLayout.NORTH);
Panel1.add(button[4], BorderLayout.CENTER);
//getContentPane().add(Panel1); leftButton = new JButton("winson");
rightButton = new JButton("nybole");
setLayout(new FlowLayout(FlowLayout.CENTER));
Panel2.add(leftButton);
Panel2.add(rightButton);
//getContentPane().add(Panel2); for (int i = 0; i < name1.length; i++) {
buttonn[i] = new JButton(name1[i]);
Panel3.add(buttonn[i]);
}
//getContentPane().add(Panel3);
for (int i = 0; i < name2.length; i++) {
button2[i] = new JButton(name2[i]);
Panel4.add(button2[i]);
}
//getContentPane().add(Panel4);
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public static void main(String[] args) { JobFJ app = new JobFJ();
}
}
我自己做了下 是这个样子的 但是 不是预期的效果 因为4个Panel 应该整体放在一个gridlayout 里 不大明白怎么弄了 而且写的比较乱…… 应该是把4个Panel放在 一个GridLayout 的4个 button里