簡體   English   中英

帶有回轉組件JAVA的GUI布局

[英]GUI Layout with swing components JAVA

這就是我想要實現的 在此處輸入圖片說明

我使用了網格布局,這就是 請忽略文本字段中顯示的文本

我的代碼就行了(如果需要,不是完整的代碼可以提供一個)。

       components.setLayout(new GridLayout(4,0));

       components.setBorder(BorderFactory.createTitledBorder("Personal Data"));

       //Lable to display
       name.setText("Resident Name");
       roomNo.setText("Room Number");
       age.setText("Age");
       gender.setText("Gender");
       careLvl.setText("Care Level");


       components.add(name);
       components.add(textFieldForName);
       components.add(roomNo);
       components.add(textFieldForAge);

       components.add(age);
       components.add(coForAge);
       components.add(gender);
       components.add(coForGender);
       components.add(careLvl);
       components.add(coForCareLvl);

任何注意將不勝感激。

GridLayout就是這樣做的,它將組件布置在一個網格中,其中每個單元格都是基於需求(即寬度/列和高度/行)的可用空間的百分比。

查看基本版式管理器及其功能的示例的《版式管理器視覺指南》

我建議您改為查看GridBagLayout 它是默認庫中可用的最靈活(也是最復雜)的布局管理器。

例如

在此處輸入圖片說明

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout31 {

    public static void main(String[] args) {
        new TestLayout31();
    }

    public TestLayout31() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            JLabel lblRes = new JLabel("Resident Name");
            JLabel lblRoomNo = new JLabel("RoomNo");
            JLabel lblAge = new JLabel("Age");
            JLabel lblGender = new JLabel("Gender");
            JLabel lblCare = new JLabel("Care level");

            JTextField fldRes = new JTextField("john smith", 20);
            JTextField fldRoomNo = new JTextField(10);
            JComboBox cmbAge = new JComboBox(new Object[]{51});
            JComboBox cmbGener = new JComboBox(new Object[]{"M", "F"});
            JComboBox cmbCare = new JComboBox(new Object[]{"Low"});

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.insets = new Insets(1, 1, 1, 1);
            add(lblRes, gbc);

            gbc.gridx++;
            gbc.gridwidth = 4;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(fldRes, gbc);

            gbc.gridx = 7;
            gbc.gridwidth = 1;
            gbc.fill = GridBagConstraints.NONE;
            add(lblRoomNo, gbc);

            gbc.gridx++;
            add(fldRoomNo, gbc);

            gbc.gridy++;
            gbc.gridx = 1;
            add(lblAge, gbc);
            gbc.gridx++;
            add(cmbAge, gbc);
            gbc.gridx++;
            add(lblGender, gbc);
            gbc.gridx++;
            add(cmbGener, gbc);
            gbc.gridx++;
            gbc.gridwidth = 2;
            add(lblCare, gbc);
            gbc.gridx += 2;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(cmbCare, gbc);

        }
    }

}

復合布局示例

另一種選擇是使用復合布局。 就是說,您將UI的每個部分划分為單獨的容器,着重於它們各自的布局要求。

例如,您有兩排字段,每行之間並沒有真正的聯系,因此您可以嘗試分別關注每一行,而不是試圖弄清楚如何使這些字段排成一行。

在此處輸入圖片說明

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout31 {

    public static void main(String[] args) {
        new TestLayout31();
    }

    public TestLayout31() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            JPanel topPane = new JPanel(new GridBagLayout());

            JLabel lblRes = new JLabel("Resident Name");
            JLabel lblRoomNo = new JLabel("RoomNo");
            JLabel lblAge = new JLabel("Age");
            JLabel lblGender = new JLabel("Gender");
            JLabel lblCare = new JLabel("Care level");

            JTextField fldRes = new JTextField("john smith", 20);
            JTextField fldRoomNo = new JTextField(10);
            JComboBox cmbAge = new JComboBox(new Object[]{51});
            JComboBox cmbGener = new JComboBox(new Object[]{"M", "F"});
            JComboBox cmbCare = new JComboBox(new Object[]{"Low"});

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.insets = new Insets(1, 1, 1, 1);
            topPane.add(lblRes, gbc);

            gbc.gridx++;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            topPane.add(fldRes, gbc);

            gbc.gridx++;
            topPane.add(lblRoomNo, gbc);

            gbc.gridx++;
            topPane.add(fldRoomNo, gbc);

            JPanel bottomPane = new JPanel(new GridBagLayout());

            gbc.gridx = 0;
            bottomPane.add(lblAge, gbc);
            gbc.gridx++;
            bottomPane.add(cmbAge, gbc);
            gbc.gridx++;
            bottomPane.add(lblGender, gbc);
            gbc.gridx++;
            bottomPane.add(cmbGener, gbc);
            gbc.gridx++;
            bottomPane.add(lblCare, gbc);
            gbc.gridx++;
            bottomPane.add(cmbCare, gbc);

            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(topPane, gbc);
            gbc.gridy++;
            add(bottomPane, gbc);

        }
    }        
}

如果需要,這將使以后更容易修改UI。

JFrame frame = new JFrame();
JPanel contentPane = new JPanel();
JPanel northPane   = new JPanel();
JPanel centerPane  = new JPanel();
JPanel southPane   = new JPanel();
contentPane.setLayout(new BorderLayout());
northPane.setLayout(  new GridLayout(1, 6));
southPane.setLayout(  new GridLayout(1, 7));
contentPane.add(northPane,  BorderLayout.NORTH );
contentPane.add(centerPane, BorderLayout.CENTER);
contentPane.add(southPane,  BorderLayout.SOUTH );
frame.setContentPane(contentPane);
JLabel            residentNameLabel = new JLabel("Resident name ");
JTextField        residentNameText  = new JTextField();
JLabel            roomNoLabel       = new JLabel("RoomNo ");
JTextField        roomNoText        = new JTextField();
JLabel            emptyLabel0       = new JLabel("   ");
JLabel            emptyLabel1       = new JLabel("   ");
JLabel            emptyLabel2       = new JLabel("   ");
JLabel            emptyLabel3       = new JLabel("   ");
JLabel            ageLabel          = new JLabel("Age ");
JComboBox<String> ageComboBox       = new JComboBox<String>();
ageComboBox.addItem("50");
ageComboBox.addItem("51");
ageComboBox.addItem("52");
ageComboBox.addItem("53");
ageComboBox.addItem("54");
ageComboBox.addItem("55");
JLabel            genderLabel       = new JLabel("Gender ");
JComboBox<String> genderComboBox    = new JComboBox<String>();
genderComboBox.addItem("M");
genderComboBox.addItem("F");
JLabel            careLevelLabel    = new JLabel("Care Level ");
JComboBox<String> careLevelComboBox = new JComboBox<String>();
genderComboBox.addItem("low");;
genderComboBox.addItem("medium");
genderComboBox.addItem("high");
residentNameLabel.setHorizontalAlignment(JLabel.RIGHT);
roomNoLabel.setHorizontalAlignment(JLabel.RIGHT);
ageLabel.setHorizontalAlignment(JLabel.RIGHT);
genderLabel.setHorizontalAlignment(JLabel.RIGHT);
careLevelLabel.setHorizontalAlignment(JLabel.RIGHT);
northPane.add(emptyLabel0      );
northPane.add(residentNameLabel);
northPane.add(residentNameText );
northPane.add(roomNoLabel      );
northPane.add(roomNoText       );
northPane.add(emptyLabel1      );
centerPane.add(emptyLabel2     );
southPane.add(ageLabel         );
southPane.add(ageComboBox      );
southPane.add(genderLabel      );
southPane.add(genderComboBox   );
southPane.add(careLevelLabel   );
southPane.add(careLevelComboBox);
southPane.add(emptyLabel3      );
contentPane.setBorder(BorderFactory.createTitledBorder("Personal Data"));
frame.addWindowListener(new WindowAdapter() {
@Override
    public void windowClosing(WindowEvent evt) {
        System.exit(0);
    }
});
frame.setVisible(true);
frame.pack();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM