动态添加行时的JPanel / Component扩展 - Java Swing

时间:2012-03-01 18:00:35

标签: java swing

我创建了三个面板csvSrvrPanel,syncPanel和monitoringSrvrPanel。在monitoringSrvrPanel中,我有一个按钮来添加新的texbox。当我单击按钮时,新行正在创建,但我的屏幕不会显示它,直到我展开它。我希望窗口在添加行时自动调整大小。

以下是代码。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import test.FormUtility;

public class Config implements ActionListener {

JFrame frameConfig = null;

JPanel comp = null;
JPanel csvSrvrPanel, syncPanel, monitoringSrvrPanel = null;
JPanel row = null;

Border paneEdge = null;
TitledBorder tbCSVServer, tbMonitoringServer, tbSync = null;

FormUtility formUtility = null;

/**
 * @param args
 */
public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {

            new Config().showConfigGUI();

        }

    });

}

public void showConfigGUI() {

    frameConfig = new JFrame("Server Configurations");
    frameConfig.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    comp = new JPanel(new BorderLayout());

    paneEdge = BorderFactory.createEmptyBorder(0, 10, 10, 10);
    comp.setBorder(paneEdge);

    tbCSVServer = BorderFactory.createTitledBorder("CSV server details");
    tbSync = BorderFactory
            .createTitledBorder("Remote file synchronization details");
    tbMonitoringServer = new TitledBorder("Monitoring server details");

    csvSrvrPanel = new JPanel(new GridBagLayout());
    monitoringSrvrPanel = new JPanel(new GridBagLayout());
    syncPanel = new JPanel(new GridBagLayout());

    formUtility = new FormUtility();

    // Titled border
    csvSrvrPanel.setBorder(tbCSVServer);
    monitoringSrvrPanel.setBorder(tbMonitoringServer);
    syncPanel.setBorder(tbSync);

    // For CSV server details
    formUtility.addLabel("Hostname: ", csvSrvrPanel);
    formUtility.addLastField(new JTextField(30), csvSrvrPanel);

    formUtility.addLabel("Username: ", csvSrvrPanel);
    formUtility.addLastField(new JTextField(30), csvSrvrPanel);

    formUtility.addLabel("CSV Root Folder Path:  ", csvSrvrPanel);
    formUtility.addLastField(new JTextField(30), csvSrvrPanel);

    // For Monitoring server details
    JPanel columnNames = new JPanel(new GridLayout(1, 0));
    columnNames.add(new JLabel("Server name"));
    columnNames.add(new JLabel("Folder path"));

    JPanel buttonPanel = new JPanel(new GridBagLayout());
    JButton bAddNewField = new JButton("Add new row");
    bAddNewField.addActionListener(this);

    Dimension bAddNewFieldSize = bAddNewField.getPreferredSize();
    bAddNewFieldSize.width = 200;
    bAddNewField.setPreferredSize(bAddNewFieldSize);

    buttonPanel.add(bAddNewField);

    formUtility.addLastField(buttonPanel, monitoringSrvrPanel);
    formUtility.addLastField(columnNames, monitoringSrvrPanel);

    row = new JPanel(new GridLayout(1, 0));
    row.add(new JTextField(15));
    row.add(new JTextField(30));

    formUtility.addLastField(row, monitoringSrvrPanel);

    // For Sync panel
    formUtility.addLabel("Remote Folder Path:  ", syncPanel);
    formUtility.addLastField(new JTextField(30), syncPanel);

    formUtility.addLabel("Local Folder Path:  ", syncPanel);
    formUtility.addLastField(new JTextField(30), syncPanel);

    comp.add(csvSrvrPanel, BorderLayout.NORTH);
    comp.add(monitoringSrvrPanel, BorderLayout.CENTER);
    comp.add(syncPanel, BorderLayout.SOUTH);

    frameConfig.add(comp);

    frameConfig.setLocationRelativeTo(null);
    frameConfig.pack();
    frameConfig.setVisible(true);

}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

    JPanel row = new JPanel(new GridLayout(1, 0));
    row.add(new JTextField(15));
    row.add(new JTextField(30));

    formUtility.addLastField(row, monitoringSrvrPanel);
    monitoringSrvrPanel.revalidate();


}

}

谢谢, Tivakar

1 个答案:

答案 0 :(得分:3)

我会使用frame.pack()来触发重新调整大小。