あなたが抱えている問題は、GUIに入力された内容に応じてボタンの動作を変更する方法だと思います。GUI を使用すると、ユーザーは有効な GUI コンポーネントをいつでも任意の順序で操作できることに注意してください。重要なのは、ボタンの ActionListener で GUI の状態を確認し、この GUI の状態に応じてこのメソッドの動作を変更することです。たとえば、GUI に field1、field2、sumField の 3 つの JTextField と JButton addButton があるとします。
private JTextField field1 = new JTextField(5);
private JTextField field2 = new JTextField(5);
private JTextField sumField = new JTextField(5);
private JButton addButton = new JButton("Add");
また、addButton で field1 と field2 の数値を加算し、その結果を sumField に配置する必要がありました。いずれかのフィールドが空白のままになっている場合は、明らかに加算を行いたくないので、JButton でテストします。アクションリスナー:
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text1 = field1.getText().trim();
String text2 = field2.getText().trim();
if (text1.isEmpty() || text2.isEmpty()) {
// data not entered... so return the method and do nothing
return;
}
// if we've reached this point, the user has entered in text and so we handle it
ここにすべてがあります:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class WaitForInput extends JPanel {
private JTextField field1 = new JTextField(5);
private JTextField field2 = new JTextField(5);
private JTextField sumField = new JTextField(5);
private JButton addButton = new JButton("Add");
public WaitForInput() {
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text1 = field1.getText().trim();
String text2 = field2.getText().trim();
if (text1.isEmpty() || text2.isEmpty()) {
// data not entered... so return the method and do nothing
return;
}
try {
int number1 = Integer.parseInt(field1.getText());
int number2 = Integer.parseInt(field2.getText());
int sum = number1 + number2;
sumField.setText("" + sum);
} catch (NumberFormatException e1) {
// TODO: use JOptionPane to send error message
// clear the fields
field1.setText("");
field2.setText("");
}
}
});
add(field1);
add(new JLabel("+"));
add(field2);
add(new JLabel("="));
add(sumField);
add(addButton);
}
private static void createAndShowUI() {
JFrame frame = new JFrame("WaitForInput");
frame.getContentPane().add(new WaitForInput());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
EDIT 1
そうでなければ、絶対にループを使用する必要がある場合は、はい、Runnableで実行し、バックグラウンドスレッドで実行します。CPU を独り占めしないように、短い時間でもループ内で Thread.sleep(...) を呼び出すことを忘れないでください。例えば
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class HaltingProblem extends JPanel {
private static final int PANEL_HEIGHT = 400;
private static final int PANEL_WIDTH = 600;
private static final long SLEEP_DELAY = 100;
private Color[] colors = {Color.red, Color.orange, Color.yellow,
Color.green, Color.blue, Color.cyan};
private boolean halt = false;
private JButton haltButton = new JButton("Halt");
private int colorIndex = 0;
public HaltingProblem() {
setBackground(colors[colorIndex]);
haltButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
halt = !halt; // toggle it!
}
});
add(haltButton);
new Thread(new Runnable() {
public void run() {
while (true) {
keepDoingThis();
}
}
}).start();
}
private void keepDoingThis() {
try {
Thread.sleep(SLEEP_DELAY);
} catch (InterruptedException e) {}
if (halt) {
return;
}
colorIndex++;
colorIndex %= colors.length;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setBackground(colors[colorIndex]);
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PANEL_WIDTH, PANEL_HEIGHT);
}
private static void createAndShowUI() {
JFrame frame = new JFrame("HaltingProblem");
frame.getContentPane().add(new HaltingProblem());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}