我的软件包中有两个文件: GUI.java 和 Dialog.java
GUI有两个文本字段和一个按钮。当我按下该按钮时,对话框变为可见。对话框中还有另一个按钮,用于将文本字段的值保存到.ini文件中。问题来了,它不保存更新的值,而是保存程序首次启动时存在的值。
在Google上进行了一些搜索之后,我发现我需要使用“动作监听器”,但是似乎根本无法正确使用它。
GUI.java:
import java.io.File;
import java.io.IOException;
import org.ini4j.Wini;
public class GUI extends javax.swing.JFrame {
public GUI(){
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
textField1 = new javax.swing.JTextField();
textField2 = new javax.swing.JTextField();
openDialog = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
textField1.setText("1");
textField2.setText("2");
openDialog.setText("Open Dialog");
openDialog.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openDialogActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(openDialog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(openDialog)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void openDialogActionPerformed(java.awt.event.ActionEvent evt) {
Dialog dialog = new Dialog(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
dialog.setVisible(false);
}
});
dialog.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new GUI().setVisible(true);
}
});
}
void save(int i){
try {
Wini ini = new Wini(new File("C:/Users/Paul/Desktop/Configs.ini"));
int firstValue = Integer.parseInt(textField1.getText());
int secondValue = Integer.parseInt(textField2.getText());
ini.put("Configuration " + i, "First Value", firstValue);
ini.put("Configuration " + i, "Second Value", secondValue);
ini.store();
}
catch (IOException ex) {}
}
private javax.swing.JButton openDialog;
private javax.swing.JTextField textField1;
private javax.swing.JTextField textField2;
}
Dialog.java:
public class Dialog extends javax.swing.JDialog {
GUI GUI = new GUI();
public Dialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
save = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
save.setText("Save");
save.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(save)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(save)
.addContainerGap())
);
pack();
}// </editor-fold>
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
GUI.save(1);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Dialog dialog = new Dialog(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
private javax.swing.JButton save;
}
答案 0 :(得分:0)
Dialog
不应创建GUI
的新实例,在屏幕上显示的实例与Dialog
所使用的实例之间没有任何关系。
相反,GUI
应该将自身的引用传递给Dialog
public class Dialog extends javax.swing.JDialog {
private GUI gui;
public Dialog(GUI parent) {
super(parent, true);
this.gui = gui;
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
save = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
save.setText("Save");
save.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(save)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(save)
.addContainerGap())
);
pack();
}// </editor-fold>
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
gui.save(1);
}
private javax.swing.JButton save;
}
这是非常基本的Java,在着手进行GUI开发之类的复杂操作之前,您应该熟悉一下。有关更多详细信息,请参见Passing Information to a Method or a Constructor
您的GUI
类可能看起来像...
public class GUI extends javax.swing.JFrame {
public GUI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
textField1 = new javax.swing.JTextField();
textField2 = new javax.swing.JTextField();
openDialog = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
textField1.setText("1");
textField2.setText("2");
openDialog.setText("Open Dialog");
openDialog.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openDialogActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(openDialog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(openDialog)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void openDialogActionPerformed(java.awt.event.ActionEvent evt) {
Dialog dialog = new Dialog(this);
dialog.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new GUI().setVisible(true);
}
});
}
void save(int i) {
// Code I can't execute
// Also, don't ignore the exception, you should probably
// rethrow it...
}
private javax.swing.JButton openDialog;
private javax.swing.JTextField textField1;
private javax.swing.JTextField textField2;
}
尽管,我认为如果要GUI
保存数据,它应该检查对话框的结果,而不是将自身暴露于可能的虐待之下(removeAll
?)。在这种情况下,我会考虑使用JOptionPane
有关更多详细信息,请参见How to Make Dialogs。
此外,您越早放弃表单编辑器,您就会越好
我会很期待,看看是否可以通过JOptionPane使它工作
private void openDialogActionPerformed(java.awt.event.ActionEvent evt) {
int response = JOptionPane.showOptionDialog(this, "Do you wish to save", "Save", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
if (response == JOptionPane.YES_OPTION) {
save(1);
}
}