1
我已經創建了三個面板csvSrvrPanel,syncPanel和monitoringSrvrPanel。在monitoringSrvrPanel中,我有一個按鈕來添加新的texboxes行。當我點擊按鈕時,新行已經創建,但我的屏幕直到我展開它才顯示。我希望窗口在添加行時自動調整大小。JPanel /組件擴展,當一個行動態添加 - Java Swing
這是它的代碼。
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
調用'validate()'。如果沒有成功,請發佈[SSCCE](http://sscce.org/)。 – 2012-03-01 18:26:05
難道你不能使用Pack()來觸發重新調整大小嗎? – Halfwarr 2012-03-01 18:28:41
使用frame.pack()修復了這個問題。謝謝Halfwarr。你能回答我的問題,以便我可以選擇正確的答案嗎? – Tivakar 2012-03-01 18:32:11