我正在构建一个更新txt文件的工具。在JOptionPane中,我希望文件更新如果选择单选按钮“添加魅力”并单击JOptionPane.Ok_Option按钮。代码编译得很好,但是当程序运行时,txt文件中没有发生任何变化。
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.*;
public class test extends JFrame implements ActionListener {
public static void main (String[]args) {
test gui=new test();
}
JPanel pnl=new JPanel(new BorderLayout());
JButton addBtn;
String path=null;
Container contentPane=getContentPane();
JPanel grid=new JPanel(new GridLayout(1,1));
Border border1=BorderFactory.createLineBorder(Color.BLACK,5);
Border border2=BorderFactory.createLineBorder(Color.GRAY,5);
ImageIcon add=new ImageIcon("add.jpg");
String username=(System.getProperty("user.name"));
JRadioButton radd;
JRadioButton rrem;
JTextField charmnum;
JTextField charmn;
JTextField charmdd;
public test() {
super("TEST");
setSize(200,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(grid);
setVisible(true);
setAlwaysOnTop(false);
setResizable(false);
addBtn=new JButton("ADD",add);
addBtn.addActionListener(this);
grid.add(addBtn);
grid.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
contentPane.add("North",grid);
grid.revalidate();
}
public void actionPerformed(ActionEvent event) {
if(event.getSource()==addBtn) {
try {
JPanel acpnl = new JPanel(new GridLayout(5,1));
/*JRadioButton */radd=new JRadioButton("Add Charm");
/*JRadioButton */rrem=new JRadioButton("Remove Charm",false);
//rrem.setEnabled(false);
/*JTextField */charmnum = new JTextField(12);
/*JTextField */charmn = new JTextField(12);
/*JTextField */charmdd = new JTextField(12);
ButtonGroup radios=new ButtonGroup();
charmnum.setEditable(false);
charmn.setEditable(false);
charmdd.setEditable(false);
radd.addActionListener(this);
rrem.addActionListener(this);
radios.add(radd);
radios.add(rrem);
add(acpnl);
acpnl.add(radd);
acpnl.add(rrem);
acpnl.add(Box.createHorizontalStrut(15)); // a spacer
acpnl.add(new JLabel("Charm #:"));
acpnl.add(charmnum);
acpnl.add(Box.createHorizontalStrut(15)); // a spacer
acpnl.add(new JLabel("Charm Name:"));
acpnl.add(charmn);
acpnl.add(Box.createHorizontalStrut(15)); // a spacer
acpnl.add(new JLabel("Charm DueDate:"));
acpnl.add(charmdd);
boolean test=radd.isSelected();
int result = JOptionPane.showConfirmDialog(null,acpnl,"Fill in the Charm Information",JOptionPane.OK_CANCEL_OPTION);
if (result==JOptionPane.OK_OPTION)while(test) {
String strfile, midfile, finalfile;
File batchfile=new File("test.txt");
BufferedReader br=new BufferedReader(new FileReader(batchfile));
File newfile=new File("temp.txt");
newfile.createNewFile();
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(newfile)));
while ((strfile = br.readLine()) !=null) {
String[]fileinput=new String[2];
String A=(charmnum.getText());
fileinput[0]=(A);
String B=(charmn.getText());
fileinput[1]=(B);
String PART1NEW=
"If exist \"C:%desktop%\\"+fileinput[0]+" Testing\" GOTO copy"+fileinput[0]+" >> %log%.txt\r\n"+
"set charm#="+fileinput[0]+" >> %log%.txt\r\n"+
"GOTO errormissing1 >> %log%.txt\r\n"+
":"+fileinput[0]+"check >> %log%.txt\r\n"+
"::p1end";
/*String PART1REM=
"If exist \"C:%desktop%\\"+fileinput[0]+" Testing\" GOTO copy"+fileinput[0]+" >> %log%.txt\r\n"+
"set charm#="+fileinput[0]+" >> %log%.txt\r\n"+
"GOTO errormissing1 >> %log%.txt\r\n"+
":"+fileinput[0]+"check >> %log%.txt\r\n"; */
String PART2NEW=
":copy"+fileinput[0]+" >> %log%.txt\r\n"+
"echo. >> %log%.txt\r\n"+
"echo "+fileinput[0]+" >> %log%.txt\r\n"+
"xcopy \"C:%desktop%\\"+fileinput[0]+" Testing\" \"X:%mycharmsopen%\\"+fileinput[0]+" - "+fileinput[1]+"\\\" /D /E /C /R /I /K /Y >> %log%.txt\r\n"+
"GOTO "+fileinput[0]+"check >> %log%.txt\r\n"+
"::p2end";
/*String PART2REM=
":copy"+fileinput[0]+" >> %log%.txt\r\n"+
"echo. >> %log%.txt\r\n"+
"echo "+fileinput[0]+" >> %log%.txt\r\n"+
"xcopy \"C:%desktop%\\"+fileinput[0]+" Testing\" \"X:%mycharmsopen%\\"+fileinput[0]+" - "+fileinput[1]+"\\\" /D /E /C /R /I /K /Y >> %log%.txt\r\n"+
"GOTO "+fileinput[0]+"check >> %log%.txt\r\n"; */
String PART1END="::p1end";
String PART2END="::p2end";
midfile=(strfile.replace(PART1END,PART1NEW));
finalfile=(midfile.replace(PART2END,PART2NEW));
if (finalfile != null) {
pw.println(finalfile);
}
}
pw.close();
br.close();
batchfile.delete();
newfile.renameTo(batchfile);
}
}
catch (Exception e6) {
JOptionPane.showMessageDialog(this,"Error has occurred in \"addBtn\" actionEvent.","Error",JOptionPane.ERROR_MESSAGE);
e6.printStackTrace();
}
}
if (radd.isSelected()) {
charmnum.setEditable(true);
charmn.setEditable(true);
charmdd.setEditable(false);
}
if (rrem.isSelected()) {
charmnum.setEditable(true);
charmn.setEditable(false);
charmdd.setEditable(false);
}
}
}
txt文件与程序位于同一个目录中,它包含以下内容(更新前):
null
null
null
null
::p1end
null
null
null
null
::p2end
null
null
null
null
我在第92行缩小了问题:
if (result==JOptionPane.OK_OPTION)while(test) {
当 while(test)被注释掉时,程序成功地对txt文件进行了更改,但没有考虑选择了哪个单选按钮。 我知道我可以在主JFrame中创建另一个Jbutton来替换JOptionPane中的2x单选按钮,但我真的想让这个设置工作。
有什么东西要替换而(测试)以使该程序按需运行?
(我提前为嵌套不良的代码和类组织道歉。)
答案 0 :(得分:0)
交换线条:
int result = JOptionPane.showConfirmDialog(null,acpnl,"Fill in the Charm Information",JOptionPane.OK_CANCEL_OPTION);
boolean test=radd.isSelected();
您目前的方式,存储在test
中,是否在向用户显示其父面板之前选择了radd
,因此它始终为false
。
另外,请使用if (result == JOptionPane.OK_OPTION && test)
。