我正在開發一個swing應用程序,並在決定什麼可能是保存大量JTextField的值的最佳方法時發生。 應用程序從某個網絡位置讀取屬性文件。屬性文件對每個特定用戶都有大約10-12個屬性,稱propertyA_1是用戶1的屬性,用戶2是propertA_2。屬性中將有大約10-12個用戶,因此它將使大約80個JTextField顯示值。每個用戶的屬性將顯示在JTabbedPane上。 現在我想要做的是,當用戶爲每個JTabbedPane中顯示的任何用戶更改任何JTextField的值並單擊「Save」buttton時,應該保存該屬性的值。 告訴我什麼可能是處理大量JtextField的最佳方法。我想這樣做2種方式捕獲大量JTextFields的文本更改
- 點擊「保存」按鈕保存每個值的屬性,從所有的JTextFields獲取值。
- 某些方法可以僅獲取文本被更改並保存其值的JTextField。
我不知道如何做選項2的事情,但我認爲這是最好的方法。
這裏是我的代碼:
public class IOSAutomationTool1 implements ActionListener {
JButton saveButton = new JButton("Click to Save");
/**
* @param args the command line arguments
*/
public void Test() throws IOException {
File file = new File("C:\\Documents and Settings\\test\\Desktop\\test.properties");
if(file != null){
Properties property = new Properties();
property.load(new FileInputStream(file));
JFrame frame = new JFrame("IOSAutomationToolFourthPage");
frame.setLayout(new FlowLayout());
JPanel framePanel = new JPanel();
JPanel buttonPanel = new JPanel();
framePanel.setSize(700,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String numberOfClients = property.getProperty("numberOfClients");
System.out.println("number of clients: "+numberOfClients);
saveButton.addActionListener(this);
buttonPanel.add(saveButton);
if (!numberOfClients.isEmpty()) {
int numberOfClientNumb = Integer.parseInt(numberOfClients);
System.out.println("numberOfClientNumb ::" + numberOfClientNumb);
JTabbedPane tab = new JTabbedPane();
// JTabbedPane tab2 = new JTabbedPane();
for(int i=1; i<=numberOfClientNumb; i++){
JPanel panel = new JPanel();
panel.setSize(400, 400);
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(1, 1, 1, 1);
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0;
gbc.gridx = 0;
//Remote IP
JLabel remoteIPLbl = new JLabel("Remote IP : ");
panel.add(remoteIPLbl,gbc);
JLabel userNameLbl = new JLabel("User Name : ");
panel.add(userNameLbl,gbc);
JLabel remotePasswordLbl = new JLabel("Remote Password : ");
panel.add(remotePasswordLbl,gbc);
JLabel remotePathLbl = new JLabel("Remote Path : ");
panel.add(remotePathLbl,gbc);
JLabel deviceOrSimulatorLbl = new JLabel("Device or Simulator : ");
panel.add(deviceOrSimulatorLbl,gbc);
gbc.gridx = 1;
gbc.weightx = 1;
String remoteIPVal = property.getProperty("remoteIP_"+i);
JTextField remoteIPField = new JTextField(remoteIPVal);
//remoteIPLbl.setBounds(50, 100, 2, 2);
remoteIPField.setBounds(200, 100, 2, 2);
remoteIPField.setColumns(30);
panel.add(remoteIPField,gbc);
//Remote User Name
String userNameVal = property.getProperty("remoteUserName_"+i);
JTextField userNameField = new JTextField(userNameVal);
// userNameLbl.setBounds(50, 200, 2, 2);
userNameField.setBounds(200, 200, 2, 2);
userNameField.setColumns(20);
panel.add(userNameField,gbc);
//Remote Password
String remotePasswordVal = property.getProperty("remotePassword_"+i);
JTextField remotePasswordField = new JTextField(remotePasswordVal);
// remotePasswordLbl.setBounds(50, 300, 2, 2);
remotePasswordField.setBounds(200, 300, 2, 2);
remotePasswordField.setColumns(20);
panel.add(remotePasswordField,gbc);
//Remote Path
String remotePathVal = property.getProperty("remotePath_"+i);
JTextField remotePathField = new JTextField(remotePathVal);
// remotePathLbl.setBounds(50, 400, 2, 2);
remotePathField.setBounds(200, 400, 2, 2);
remotePathField.setColumns(100);
panel.add(remotePathField,gbc);
//deviceOrSimulator
String deviceOrSimulatorVal = property.getProperty("deviceOrSimulator_"+i);
JTextField deviceOrSimulatorField = new JTextField(deviceOrSimulatorVal);
//deviceOrSimulatorLbl.setBounds(50, 500, 2, 2);
deviceOrSimulatorField.setBounds(200, 500, 2, 2);
deviceOrSimulatorField.setColumns(1);
panel.add(deviceOrSimulatorField,gbc);
tab.add("Client "+i,panel);
System.out.println(""+i);
}
framePanel.add(tab);
frame.add(framePanel);
// frame.setLayout(new Lay);
}
//frame.add(saveButton);
frame.add(buttonPanel);
frame.setSize(800, 800);
frame.pack();
frame.setVisible(true);
}
}
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
if(e.getSource() == saveButton){
System.out.print("button clicked...........");
}
}
public static void main(String[] args) throws IOException{
IOSAutomationTool1 obj = new IOSAutomationTool1();
obj.Test();
}
}
我不確定,我找到了答案,雖然正如你描述你的情況,你也回答了我的問題。當你從屬性文件中讀取數據時,我想你是通過一個鍵的幫助來獲得與該鍵相關聯的值,爲什麼你不能簡單地將它作爲你的選項2應用,把這個鍵設置爲' JTextField「,只需按下按鈕即可更新此鍵的值。 –
你如何編碼?我怎麼知道JTextField的值是否在點擊Jbutton(按鈕點擊事件)時發生了變化。由於我只想更新那些已更改的值,因此其餘部分將保持原樣。 –
還有一種方法可以讓你查看,讓一個班級說'USER',現在當你從'DATABASE'檢索值時,把它們放在這個班級裏,現在按下Save按鈕時,這個班級就成爲這個班級的另一個對象說'updatedUser'並將'JTextField'的當前值複製到這個新實例,現在比較這兩個值,只更改修改的那個,剩下的就剩下了。 AFAIK,在相應的系統上計算值要快得多,然後更新整個數據庫,只需一次修改。 –