1
通過下面的代碼,我使用JTextFields和JLabels創建JPanel並將該面板添加到另一個JPanel。如何調整infoPanel上的JTextField之間的間距?在JPanel上填充JTextFields
我試過GridBagLayout和GridLayout有不同的和不希望的結果。現在的方式至少讓它們垂直對齊,但我似乎無法在它們上方和下方添加空間。這個問題上的任何大師能夠幫忙嗎?
public DrawPanelMain() {
JPanel btnPanel = new JPanel(); //Creates a new Panel for the buttons
JPanel infoPanel = new JPanel();
JPanel fields = new JPanel();
//Text boxes for infoPanel
JTextField textField1 = new JTextField(20);
JTextField textField2 = new JTextField(20);
JTextField textField3 = new JTextField(20);
JTextField textField4 = new JTextField(20);
JTextField textField5 = new JTextField(20);
JTextField textField6 = new JTextField(20);
//JLabels for infoPanel
JLabel jLabel1 = new JLabel("Serial Number: ");
JLabel jLabel2 = new JLabel("Information: ");
JLabel jLabel3 = new JLabel("Information: ");
JLabel jLabel4 = new JLabel("Information: ");
JLabel jLabel5 = new JLabel("Information: ");
JLabel jLabel6 = new JLabel("Information: ");
//These are the buttons that will be added to the btnPanel
btnPanel.add(new JButton(new AddSwitchAction("Add Switch Panel")));
btnPanel.add(new JButton(new PushConfigAction("Push Config")));
btnPanel.add(new JButton(new ActivateAllAction("Activate All")));
btnPanel.add(new JButton(new DeactivateAllAction("Deactivate All")));
//Fields that will be added to infoPanel
fields.add(jLabel1);
fields.add(textField1);
fields.add(jLabel2);
fields.add(textField2);
fields.add(jLabel3);
fields.add(textField3);
fields.add(jLabel4);
fields.add(textField4);
fields.add(jLabel5);
fields.add(textField5);
fields.add(jLabel6);
fields.add(textField6);
//Sets border padding for the infoPanel
fields.setBorder(new EmptyBorder(20, 20, 0, 20));
//Draws border for the infoPanel
infoPanel.setBorder(BorderFactory.createRaisedBevelBorder());
//Sets layout for the fields panel
fields.setLayout(new GridLayout(6, 1));
//Add fields to infoPanel
infoPanel.add(fields);
//Add panels to tabbedPane
setLayout(new BorderLayout());
add(tabbedPane, BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
add(infoPanel, BorderLayout.EAST);
}
請看看這篇文章,關於[在Swing GUI的提供空格](HTTP:// stackoverflow.com/q/17874717/1057230)。希望它對這個主題有一些幫助:-)簡而言之,對每個'Layout'關注使用重載的構造函數。 –
@ncEcOw你da man,這正是我一直在尋找的! – feltersnach