【问题标题】:which layout manager would be appropriate to my design?哪个布局管理器适合我的设计?
【发布时间】:2014-08-10 19:23:51
【问题描述】:

我是 Java 的初学者。我希望在 Swings 中获得帮助来定位我的组件。

我无法决定应该使用哪个布局管理器来按以下顺序放置组件

+-----------------------------------+
|                                   |
|      Username  Text Field         |
|      Password  Password Field     |
|                                   |
|           Submit button           |
|                                   |
+-----------------------------------+

以下是我的代码

    package ssst;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

class Test implements ActionListener{



    JButton submit;
    JFrame j;
    JFrame jf;

    public Test()
    {
    j = new JFrame("PLAIN");
    j.setBounds(500,150,300,400);

    JPanel panel = new JPanel();
    j.add(panel);
    GridBagLayout gb = new GridBagLayout();
    panel.setLayout(gb);


    GridBagConstraints c = new GridBagConstraints();



    JLabel label = new JLabel("User Name");
    c.gridx=0;
    c.gridy=0;
    c.fill=GridBagConstraints.HORIZONTAL;
    c.anchor=GridBagConstraints.WEST;
    c.ipadx=5;
    c.ipady=5;
    c.insets= new Insets(7,7,7,7);

    panel.add(label,c);

    JTextField username = new JTextField(10);

    c.gridx=1;
    c.gridy=0;

    c.fill=GridBagConstraints.HORIZONTAL;
    c.anchor=GridBagConstraints.WEST;
    c.ipadx=5;
    c.insets= new Insets(7,7,7,7);

    panel.add(username,c);

    JLabel password= new JLabel("Password");
    c.gridx=0;
    c.gridy=1;
    c.fill=GridBagConstraints.HORIZONTAL;
    c.anchor=GridBagConstraints.WEST;
    c.ipadx=5;
    c.insets= new Insets(7,7,7,7);

    panel.add(password,c);

    JPasswordField pass = new JPasswordField(10);
    c.gridx=1;
    c.gridy=1;
    c.fill=GridBagConstraints.HORIZONTAL;
    c.anchor=GridBagConstraints.WEST;
    c.insets= new Insets(7,7,7,7);

    panel.add(pass,c);

    submit = new JButton("Submit");
    c.gridx=1;
    c.gridy=6;
    c.fill=GridBagConstraints.HORIZONTAL;
    c.anchor=GridBagConstraints.WEST;
    c.insets= new Insets(7,7,7,7);

    panel.add(submit,c);

    submit.addActionListener(this);
    j.setVisible(true);
    j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }


    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

        j.setVisible(false);
        jf = new JFrame("NEw Window");
        jf.setVisible(true);
        jf.setBounds(500,150,300,400);
        JPanel panel2 = new JPanel();
        panel2.setLayout(null);
        jf.add(panel2);

        JButton logout = new JButton("LOGOUT");
        logout.setBounds(100, 30, 400, 30);
        panel2.add(logout);
        logout.addActionListener(new Test2());
        jf.setDefaultCloseOperation(j.EXIT_ON_CLOSE);



    }


    class Test2 implements ActionListener{
        public void actionPerformed(ActionEvent e) {

            jf.dispose();
            j.setVisible(true);

        }


    }


public static void main(String args[])
{
    SwingUtilities.invokeLater(new Runnable()
    {
            public void run()
            {

                new Test();

            }

    }

            );


}




}

【问题讨论】:

  • 我认为这可以使用new GridLayout(3, 2) 轻松完成,但也可以使用GridBagLayoutSpringLayoutMigLayout(最后一个不是JDK 的一部分)。
  • @ZiaKhan,请不要使用空布局,请考虑使用 GridBagLayout 来满足您当前的需求,how to use
  • 完全是 Gridbag:madbean.com/anim/totallygridbag
  • 我会结合使用三个布局管理器:BorderLayout 作为父布局; CENTER 位置是带有 SpringLayout 的 JPanel(使用 Swing 教程中的实用程序代码轻松创建网格),用于带有标签的输入字段,PAGE_END 位置是 JPanel,按钮具有居中的 FlowLayout。
  • 希望thread 对这个话题有所帮助:-)

标签: java swing layout layout-manager


【解决方案1】:

您可以使用GridBagLayoutJOptionPane

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LoginPane extends JPanel {

    private JTextField userName;
    private JPasswordField password;

    public LoginPane() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(4, 4, 4, 4);
        add(new JLabel("Username:"), gbc);
        gbc.gridy++;
        add(new JLabel("Password:"), gbc);

        userName = new JTextField(10);
        password = new JPasswordField(10);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        add(userName, gbc);
        gbc.gridy++;
        add(password, gbc);
    }

    public String getUsername() {
        return userName.getText();
    }

    public char[] getPassword() {
        return password.getPassword();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                LoginPane loginPane = new LoginPane();
                int option = JOptionPane.showOptionDialog(
                        null, 
                        loginPane, 
                        "Login", 
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        new Object[]{"Submit"},
                        "Submit");
                if (option == 0) {
                    System.out.println("Happy");
                }

            }
        });
    }
}

您也可以将GridLayout 与此概念一起使用。

查看http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html 了解更多想法和其他布局管理器的链接

【讨论】:

  • 我们可以用 Flowlayout 做到这一点吗?
  • 不,FlowLayout 是为了简单化你想要实现的目标
【解决方案2】:

选择GridBagLayout,布局逻辑很简单。它适用于 X 和 Y 坐标。 here

你也应该去看看其他的布局,它会帮助你在未来的设计中做出决定。

【讨论】:

    【解决方案3】:

    最好使用 GridBagLayout,以防你添加新组件,如果你(最大化/恢复向下),布局管理器会注意将组件与屏幕匹配。

    【讨论】:

      【解决方案4】:

      说什么布局管理器适合 设计。对于MigLayout 经理来说,这种设计是显而易见的。

      为了比较,我提供了一个MigLayout的解决方案。

      package com.zetcode;
      
      import java.awt.EventQueue;
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPasswordField;
      import javax.swing.JTextField;
      import net.miginfocom.swing.MigLayout;
      
      
      public class MigLayoutLoginEx extends JFrame {
      
          public MigLayoutLoginEx() {
      
              initUI();
      
              setTitle("Log in");
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setLocationRelativeTo(null);
          }
      
          private void initUI() {
      
              setLayout(new MigLayout("ins 15, wrap 2", "[][grow]"));
      
              JLabel lbl1 = new JLabel("User name:");
              JTextField field1 = new JTextField(10);
      
              JLabel lbl2 = new JLabel("Password:");
              JPasswordField field2 = new JPasswordField(10);  
      
              JButton btn = new JButton("Submit");
      
              add(lbl1);
              add(field1, "growx");
              add(lbl2);
              add(field2, "growx");
              add(btn, "span 2, center, gaptop 20");
      
              pack();
          }
      
          public static void main(String[] args) {
      
              EventQueue.invokeLater(new Runnable() {
                  @Override
                  public void run() {
                      MigLayoutLoginEx ex = new MigLayoutLoginEx();
                      ex.setVisible(true);
                  }
              });
          }
      }
      

      布局是用六行布局代码实现的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-02
        • 2015-06-05
        • 2022-07-22
        • 2010-09-12
        • 2018-12-05
        • 2011-09-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多