Java Swing面板布局

时间:2012-10-26 21:35:05

标签: java swing jframe jpanel layout-manager

我正在为大学开展Java项目,涉及我们设置TCP服务器和客户端。我有那个部分工作,现在为我的项目添加更多的感觉,我想添加一个GUI。

我们还没有开始学习Java中的GUI。但是我想尝试,因为我认为这将是一个有用的练习。我有一个非常基本的GUI设置和为该按钮设置的相应ActionListener。我的下一个问题是定位我的面板,使它们在框架上看起来整洁,整洁......

目前我将所有组件放在一个面板中,如下所示:

public ClientGUI(){

    //Initialise Frame
    frame = new JFrame("TCP Client");

    //Initialise Panel 1 & Components
    p1 = new JPanel();

    //Set Layout
    p1.setLayout(new GridLayout(1,2));

    //Label 1 - For TextArea
    l1 = new JLabel("Chat Log");
    p1.add(l1);

    //TextArea - To display conversation
    t1 = new JTextArea(10,10);
    p1.add(t1);

    //Label 2 - For TextField
    l2 = new JLabel("Message");
    p1.add(l2);

    //Message Box - For user input
    t2 = new JTextField(10);
    p1.add(t2);

    //Button 1 - To send message
    b1 = new JButton("Send");
    p1.add(b1);

    //Add panels to frame
    frame.add(p1);

    //Frame properties...
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,400);
    frame.setVisible(true);

    //Add Event listener to button
    b1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ev){
            //do something
            t1.setText(t2.getText());
        }
    });

我希望它看起来像下面粗糙的线框。

Desired Layout

我很感激任何人可能提供的任何反馈!非常感谢。

2 个答案:

答案 0 :(得分:3)

您想要的是 BoxLayout ,它以列或行的形式提供UI元素。然后你可以将它们嵌套在另一个中,例如有一个水平框布局面板作为另一个垂直的元素(有点像嵌套的HTML表格)。所以你的所有元素都会进入顶层垂直BoxLayout,而拥有JLabel2和JTextField的行将是它自己的水平BoxLayout嵌套在顶层垂直布局中。 Here是关于布局管理器的相当不错的教程,它包括BoxLayout。

答案 1 :(得分:3)

有许多不同的方法,可以使用许多不同的LayoutManager。 在这里阅读更多内容:

以下是我使用GridBagLayout创建的示例:

enter image description here

//necessary imports
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class Test {

    /**
     * Default constructor for Test.class
     */
    public Test() {
        initComponents();
    }

    public static void main(String[] args) {
        /**
         * Set look and feel of app
         */
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            // If Nimbus is not available, you can set the GUI to another look and feel.
        }
        /**
         * Create GUI and components on Event-Dispatch-Thread
         */
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Test test = new Test();
            }
        });
    }

    /**
     * Initialize GUI and components (including ActionListeners etc)
     */
    private void initComponents() {

        JFrame jFrame = new JFrame("Chat Test");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setResizable(false);

        //Initialise Panel 1 & Components
        JPanel p1 = new JPanel(new GridBagLayout());
        JPanel p2 = new JPanel(new GridBagLayout());
        //Label 1 - For TextArea
        JLabel l1 = new JLabel("Chat Log");
        //TextArea - To display conversation
        final JTextArea t1 = new JTextArea(10, 10);
        JScrollPane pane = new JScrollPane(t1);
        //Label 2 - For TextField
        JLabel l2 = new JLabel("Message");
        //Message Box - For user input
        final JTextField t2 = new JTextField(10);
        //Button 1 - To send message
        JButton b1 = new JButton("Send");

        GridBagConstraints gc = new GridBagConstraints();

        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.weightx = 1;
        gc.gridx = 0;
        gc.gridy = 0;
        p1.add(l1, gc);

        gc.gridx = 0;
        gc.gridy = 1;
        p1.add(pane, gc);

        GridBagConstraints gc2 = new GridBagConstraints();
        gc2.fill = GridBagConstraints.HORIZONTAL;
        gc2.weightx = 1;
        gc2.gridx = 0;
        gc2.gridy = 0;
        gc2.ipadx = 10;
        p2.add(l2, gc2);

        gc2.gridx = 1;
        gc2.gridy = 0;
        p2.add(t2, gc2);

        gc2.gridx = 1;
        gc2.gridy = 1;
        p2.add(b1, gc2);

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ev) {
                //do something
                t1.setText(t2.getText());
            }
        });

        jFrame.add(p1, BorderLayout.CENTER);
        jFrame.add(p2, BorderLayout.SOUTH);

        //pack frame (size JFrame to match preferred sizes of added components and set visible
        jFrame.pack();
        jFrame.setVisible(true);
    }
}
相关问题