java 中的 swing 五种布局
在界面设计中,一个容器要放置许多组件,为了美观,将组件安排在容器的不同位置这就是布局设计,常用有以下布局类1.FlowLayoutimport java.awt.Container;import java.awt.FlowLayout;import javax.swing.JButton;import javax.swing.JFrame;/*** 将组件已加入顺序从左到右排序...
·
在界面设计中,一个容器要放置许多组件,为了美观,将组件安排在容器的不同位置这就是布局设计,常用有以下布局类
1.FlowLayout
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* 将组件已加入顺序从左到右排序,一行
* 排满就会去第二行。
* 每行组件剧中排列
* @author 持剑的龙套
*
*/
public class FlowLayoutDemo {
private JFrame frame;
private JButton button1,button2,button3,button4,button5;
public static void main(String[] args) {
FlowLayoutDemo fd = new FlowLayoutDemo();
fd.go();
}
private void go() {
frame = new JFrame("flowlayout");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
button1 = new JButton("OK");
button2 = new JButton("OPEN");
button3 = new JButton("CLOSE");
button4 = new JButton("aaa");
button5 = new JButton("CLOSEccc");
contentPane.add(button1);
contentPane.add(button2);
contentPane.add(button3);
contentPane.add(button4);
contentPane.add(button5);
frame.setSize(300,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
运行结果如下
2.BorderLayout
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* 由BorderLayout管理的容器被划分为5个区域
* North south west east center
* @author 持剑的龙套
*
*/
public class BorderLayoutDemo {
private JFrame frame;
private JButton be,bw,bn,bs,bc;
public static void main(String[] args) {
BorderLayoutDemo bd = new BorderLayoutDemo();
bd.go();
}
private void go() {
frame = new JFrame("BorderLayoutDemo");
be = new JButton("east");
bw = new JButton("west");
bn = new JButton("north");
bs = new JButton("south");
bc = new JButton("center");
frame.getContentPane().add(be,BorderLayout.EAST);
frame.getContentPane().add(bw,BorderLayout.WEST);
frame.getContentPane().add(bn,BorderLayout.NORTH);
frame.getContentPane().add(bs,BorderLayout.SOUTH);
frame.getContentPane().add(bc,BorderLayout.CENTER);
frame.setSize(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
运行结果如下
3.GridLayout
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* 将容器分为若干行和列,组件依次放入其中
* 每个组件占据一格。
* @author 持剑的龙套
*
*/
public class GridLayoutDemo {
public static void main(String[] args) {
MyWindow that = new MyWindow();
that.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
@SuppressWarnings("serial")
class MyWindow extends JFrame {
private JButton b1,b2,b3,b4,b5,b6;
public MyWindow() {
setTitle("grid");
Container contentPane = getContentPane();
contentPane.setPreferredSize(new Dimension(400,300));
contentPane.setLayout(new GridLayout(3,2));//3行2列
b1 =new JButton("b1");
b2 =new JButton("b2");
b3 =new JButton("b3");
b4 =new JButton("b4");
b5 =new JButton("b5");
b6 =new JButton("b6");
contentPane.add(b1);
contentPane.add(b2);
contentPane.add(b3);
contentPane.add(b4);
contentPane.add(b5);
contentPane.add(b6);
pack();
setVisible(true);
}
}
运行结果如下
4.BoxLayout
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Label;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 将容器中的组件水平或者竖直排成一行或一列
* @author 持剑的龙套
*
*/
public class BoxLayoutDemo {
private JFrame jFrame;
private JPanel jPanel,jPanel2;
public static void main(String[] args) {
BoxLayoutDemo boxLayoutDemo = new BoxLayoutDemo();
boxLayoutDemo.go();
}
private void go () {
jFrame = new JFrame("box lay");
Container contentPane = jFrame.getContentPane();
jPanel = new JPanel();
jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
jPanel.add(new Label("1"));
jPanel.add(new Label("2"));
jPanel.add(new Label("3"));
contentPane.add(jPanel, BoxLayout.X_AXIS);
jPanel2 = new JPanel();
jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.X_AXIS));
jPanel2.add(new Label("yes"));
jPanel2.add(new Label("no"));
jPanel2.add(new Label("cancel"));
//contentPane.add(jPanel2,BorderLayout.SOUTH);
//contentPane.add(jPanel2,FlowLayout.CENTER);
jFrame.pack();
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
运行结果如下
5.CradLayout
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* 卡片布局 每时每刻只显示一张
* @author 持剑的龙套
*
*/
@SuppressWarnings("serial")
public class CardlayoutDemo extends JFrame {
public static void main(String[] args) {
new CardlayoutDemo();
}
public CardlayoutDemo() {
this.setTitle("Cardlayout");
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = this.getContentPane();
JButton jbtn1 = new JButton("上一个");
c.add(jbtn1, BorderLayout.NORTH);
final JPanel mainJPanel = new JPanel();
//匿名内部类只能访问final变量
final CardLayout cardLayout = new CardLayout();
mainJPanel.setLayout(cardLayout);
mainJPanel.add(new JLabel("第1张"), "one");
mainJPanel.add(new JLabel("第2张"), "two");
mainJPanel.add(new JLabel("第3张"), "three");
mainJPanel.add(new JLabel("第4张"), "four");
c.add(mainJPanel, BorderLayout.CENTER);
JButton jbtn2 = new JButton("下一个");
c.add(jbtn2, BorderLayout.SOUTH);
jbtn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.previous(mainJPanel);
}
});
jbtn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.next(mainJPanel);
}
});
//cardLayout.show(mainJPanel, "three");
this.setVisible(true);
}
}
虽然没人用,但是考试会有….
更多推荐
已为社区贡献1条内容
所有评论(0)