首页 > 代码库 > Java布局方式
Java布局方式
转载请注明原文出处,http://www.cnblogs.com/flyingcloude/p/6992371.html
Java为我们提供了几个常用的布局管理器类,例如:FlowLayout、BorderLayout、GridLayout、GridBagLayout等。
java.awt FlowLayout 将组件按从左到右而后从上到下的顺序依次排列,一行不能放完则折到下一行继续放置
java.awt GridLayout 形似一个无框线的表格,每个单元格中放一个组件
java.awt BorderLayout 将组件按东、南、西、北、中五个区域放置,每个方向最多只能放置一个组件
java.awt GridBagLayout 非常灵活,可指定组件放置的具体位置及占用单元格数目
[java] view plain copy
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public class CalcUI extends JFrame
- {
- JPanel p1 = new JPanel(new FlowLayout()); //默认组件从居中开始
- JButton backButton = new JButton("back");
- JButton clearButton = new JButton("clear");
- JPanel p2 = new JPanel(new GridLayout(2, 1)); //放置2行,每行1个组件
- JTextField displayField = new JTextField();
- JPanel p3 = new JPanel(new GridLayout(4, 5)); //放置4行,每行5个组件
- String buttonStr = "789/A456*B123-C0.D+=";
- private void addButton(Container c, String s)
- {
- JButton b = new JButton(s);
- if(s.equals("A"))
- {
- b.setText("sqrt");
- }
- else if(s.equals("B"))
- {
- b.setText("1/x");
- }
- else if(s.equals("C"))
- {
- b.setText("%");
- }
- else if(s.equals("D"))
- {
- b.setText("+/-");
- }
- b.setForeground(Color.BLUE);
- c.add(b);
- // b.addActionListener(this);
- }
- public CalcUI()
- {
- p1.add(backButton);
- p1.add(clearButton);
- p2.add(displayField);
- p2.add(p1);
- for(int i = 0; i < buttonStr.length(); i++)
- this.addButton(p3, buttonStr.substring(i, i + 1));
- setLayout(new BorderLayout());
- add(p2, "North");
- add(p3, "Center");
- setSize(600, 600);
- setVisible(true);
- }
- public static void main(String[] args)
- {
- new CalcUI();
- }
- }
[java] view plain copy
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Font;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- public class GridBagConstraintsTest extends JFrame
- {
- private JLabel fromLabel = new JLabel("发送者");
- private JTextField fromField = new JTextField(10);
- private JLabel receiveLabel = new JLabel("接收者");
- private JTextField receiveField = new JTextField(10);
- private JLabel ccLabel = new JLabel("抄送人");
- private JTextField ccField = new JTextField(10);
- private JLabel subjectLabel = new JLabel("主题");
- private JTextField subjectField = new JTextField(10);
- private JTextArea accessoryArea = new JTextArea();
- private JScrollPane accessoryScroll = new JScrollPane();
- private JTextArea mailArea = new JTextArea();
- private JScrollPane scroll = new JScrollPane();
- private JLabel accessoryLabel = new JLabel("附件");
- private GridBagConstraints c = new GridBagConstraints();
- private GridBagLayout gridbag = new GridBagLayout();
- private void add(Component c, GridBagConstraints gbc, GridBagLayout gridbag, int x, int y, int w, int h)
- {
- gbc.gridx = x;
- gbc.gridy = y;
- gbc.gridheight = h;
- gbc.gridwidth = w;
- gridbag.setConstraints(c, gbc);
- add(c);
- }
- public GridBagConstraintsTest()
- {
- setSize(600, 600);
- setVisible(true);
- setLayout(gridbag);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- c.fill = GridBagConstraints.HORIZONTAL;
- c.weightx = 0;
- c.weighty = 0;
- fromLabel.setForeground(Color.BLUE);
- fromLabel.setFont(new Font("Alias", Font.BOLD, 16));
- add(fromLabel, c, gridbag, 0, 1, 1, 1);
- receiveLabel.setForeground(Color.blue);
- receiveLabel.setFont(new Font("Alias", Font.BOLD, 16));
- add(receiveLabel, c, gridbag, 0, 2, 1, 1); //指定收信人标签位置及大小
- ccLabel.setForeground(Color.blue);
- ccLabel.setFont(new Font("Alias", Font.BOLD, 16));
- add(ccLabel, c, gridbag, 0, 3, 1, 1); //指定抄送人标签位置及大小
- subjectLabel.setForeground(Color.blue);
- subjectLabel.setFont(new Font("Alias", Font.BOLD, 16));
- add(subjectLabel, c, gridbag, 0, 4, 1, 1); //指定主题标签位置及大小
- accessoryLabel.setForeground(Color.blue);
- accessoryLabel.setFont(new Font("Alias", Font.BOLD, 16));
- add(accessoryLabel, c, gridbag, 0, 5, 1, 1); //指定附件标签位置及大小
- c.weightx = 100; //行自适应缩放
- c.weighty = 0;//列高不变
- fromField.setText("admin@watermelon.com");
- add(fromField, c, gridbag, 1, 1, 1, 1); //指定发信人文本域(JTextField)位置及大小
- add(receiveField, c, gridbag, 1, 2, 1, 1); //指定收信人文本域(JTextField)位置及大小
- add(ccField, c, gridbag, 1, 3, 1, 1); //指定抄送人文本域(JTextField)位置及大小
- add(subjectField, c, gridbag, 1, 4, 1, 1); //指定主题文本域(JTextField)位置及大小
- // accessoryArea.setEditable(false);
- //设置不显示水平滚动条(该JTextArea置于JScrollPane中)
- // accessoryScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
- add(accessoryScroll, c, gridbag, 1, 5, 1, 1); //指定附件文本区(JTextArea)位置及大小
- c.fill = GridBagConstraints.BOTH;//采用全填充方式布局
- c.weightx = 100;//行自适应缩放
- c.weighty = 100;//列自适应缩放
- mailArea.setBackground(Color.blue);
- mailArea.setForeground(Color.yellow);
- mailArea.setTabSize(4);
- //指定信件主体区(JTextArea)的位置及大小。(该JTextArea也置于JScrollPane中)
- add(scroll, c, gridbag, 0, 6, 2, 1);
- }
- public static void main(String[] args)
- {
- new GridBagConstraintsTest();
- }
- }
转载请注明原文出处,http://www.cnblogs.com/flyingcloude/p/6992371.html
Java布局方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。