私はこの読み込み方法がわからないというデバッグエラーがあります。テキストフィールドの入力が整数に解析されていないという事実と関係していると思います。デバッグエラーを表示するにはどうすればよいですか?
状況によっては、2つのスレッドを使用してファイルを書き込むGUIプログラムを作成しています。各スレッドは、指定されたメッセージをユーザーが選択した特定のファイルに特定の回数だけ書き込みます。たとえば、スレッド1はabc.txtに「Hi」を10回書き込むことができ、スレッド2は「Bye」を同じファイルに10回書き込むことができます。私は参照のためのデバッグの下に私のGUIコードを持っています。前もって感謝します :)。
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "javax.swing.JTextField[,506,5,92x20,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,bo[email protected]7ec75020,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=8,columnWidth=11,command=,horizontalAlignment=LEADING]"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at assignment11.JFrameExt$1.actionPerformed(JFrameExt.java:107)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
GUIコード:
// GUI components
private JFrame main;
private JPanel topPane;
private JPanel midPane;
private JPanel botPane;
private JLabel tmsg1;
private JLabel tmsg2;
private JLabel tcount;
private JLabel tfname;
private JTextField msg1;
private JTextField msg2;
private JTextField count;
private JTextField fName;
private JButton write;
private JButton sWrite;
private JButton cWrite;
private JButton display;
private JButton clear;
private JTextArea stuff;
private JScrollPane scroll;
// local variables
private String message1;
private String message2;
private String fileName;
private int vCount;
public JFrameExt() {
main = new JFrame();
topPane = new JPanel();
midPane = new JPanel();
botPane = new JPanel();
// creating components
tmsg1 = new JLabel("Msg 1:");
tmsg2 = new JLabel("Msg 2:");
tcount = new JLabel("Count:");
tfname = new JLabel("File Name:");
msg1 = new JTextField(8);
msg2 = new JTextField(8);
count = new JTextField(8);
fName = new JTextField(10);
write = new JButton("Write");
sWrite = new JButton("Sync Write");
cWrite = new JButton("Coop Write");
display = new JButton("Display");
clear = new JButton("Clear");
stuff = new JTextArea(45, 70);
scroll = new JScrollPane(stuff);
stuff.setEditable(false);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
// adding components
main.add(topPane, BorderLayout.PAGE_START);
main.add(midPane, BorderLayout.CENTER);
main.add(botPane, BorderLayout.PAGE_END);
topPane.add(tmsg1);
topPane.add(msg1);
topPane.add(tmsg2);
topPane.add(msg2);
topPane.add(tcount);
topPane.add(count);
topPane.add(tfname);
topPane.add(fName);
midPane.add(scroll);
botPane.add(write);
botPane.add(sWrite);
botPane.add(cWrite);
botPane.add(display);
botPane.add(clear);
// changing colors
topPane.setBackground(Color.RED);
midPane.setBackground(Color.CYAN);
botPane.setBackground(Color.GREEN);
// setting sizes
main.setSize(1000, 900);
// setting main jFrame parameters
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setLocationRelativeTo(null);
;
main.setVisible(true);
// write ActionListener
write.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
message1 = msg1.getText().toString();
message2 = msg2.getText().toString();
fileName = fName.getText().toString();
vCount = Integer.parseInt(count.getText().toString().trim());
NoSyncRunnable noSync1 = new NoSyncRunnable(message1, fileName, vCount);
NoSyncRunnable noSync2 = new NoSyncRunnable(message2, fileName, vCount);
Thread t1 = new Thread(noSync1);
Thread t2 = new Thread(noSync2);
t1.start();
t2.start();
}
});
// sWrite ActionListener
sWrite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
message1 = msg1.toString();
message2 = msg2.toString();
fileName = fName.toString();
vCount = Integer.parseInt(count.toString());
Object obj = new Object();
CompSyncRunnable compSync1 = new CompSyncRunnable(message1, fileName, vCount, obj);
CompSyncRunnable compSync2 = new CompSyncRunnable(message2, fileName, vCount, obj);
Thread t1 = new Thread(compSync1);
Thread t2 = new Thread(compSync2);
t1.start();
t2.start();
}
});
// cWrite ActionListener
cWrite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
message1 = msg1.toString();
message2 = msg2.toString();
fileName = fName.toString();
vCount = Integer.parseInt(count.toString());
Object obj = new Object();
CoopSyncRunnable compSync1 = new CoopSyncRunnable(message1, fileName, vCount, obj);
CoopSyncRunnable compSync2 = new CoopSyncRunnable(message2, fileName, vCount, obj);
Thread t1 = new Thread(compSync1);
Thread t2 = new Thread(compSync2);
t1.start();
t2.start();
}
});
display.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
FileReader reader = new FileReader(fileName);
BufferedReader br = new BufferedReader(reader);
stuff.read(br, null);
br.close();
stuff.requestFocus();
}
catch (Exception e2) {
System.out.println(e2);
}
}
});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
msg1.setText("");
msg2.setText("");
count.setText("");
fName.setText("");
stuff.setText("");
}
});
}
を使用する必要があり、そのデータを取得するために 'のjava.lang .NumberFormatException:入力文字列の場合: "javax.swing.JTextField ...")。その文字列はJTextFieldのtoStringのように見えるので、toStringが直接(テキストを取得するのではなく)toStringをparseIntに渡したJTextFieldを持っていることがわかります。 'getText()'を忘れた 'count.toString()'から来ているようです。 – yshavit
'toString()'メソッドのワイルドコールでコードをスプレーすることは、一般的には悪い考えです。 'JTextField.getText()'は既にStringを返します。この場合は完全に不要です。 'JTextField.toString()'の呼び出しは悪いです。なぜなら、 'toString()'がなければ、コンパイラはあなたのコードが間違っている場所を正確に教えてくれるからです。 –
ありがとう、皆さん!私はちょっと疑問に思っていました。なぜgetText()の代わりにtoString()を使うのは悪いのですか? – Brandon