我想在一個小程序中創建,當一個複選框被選中時,爲特定按鈕執行的動作被改變爲執行的另一個動作,讓我們假設執行的按鈕動作給出1 + 2的結果,我希望當我檢查複選框,使按鈕操作執行1-3例如。複選框來改變按鈕動作偵聽器
這裏是我的代碼
import java.awt.*;
public class frame extends JFrame { // Declaring the frame class
public frame() { // crating a constructor
getContentPane().setLayout(new GridLayout(5,2)); // Creating 5 lines and 2 rows
// first label for adding the first number
JLabel label = new JLabel("First Number");
getContentPane().add(label);
label.setHorizontalAlignment(SwingConstants.CENTER);
// first text field
final JTextField text = new JTextField(8);
getContentPane().add(text);
text.setHorizontalAlignment(SwingConstants.CENTER);
// second label for adding second number
JLabel label1 = new JLabel("Second Number");
getContentPane().add(label1);
label1.setHorizontalAlignment(SwingConstants.CENTER);
// second text field
final JTextField text1 = new JTextField();
getContentPane().add(text1);
text1.setHorizontalAlignment(SwingConstants.CENTER);
// third label for the result
JLabel label2 = new JLabel("Result is");
getContentPane().add(label2);
label2.setHorizontalAlignment(SwingConstants.CENTER);
// third text field to show the result
final JTextField text2 = new JTextField();
getContentPane().add(text2);
text2.setHorizontalAlignment(SwingConstants.CENTER);
text2.setEditable(false);
// Calculation button
JButton btn = new JButton("Calculate");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number1, number2, result, ans;
try {
/*number1 = Double.parseDouble(text.getText());
number2 = Double.parseDouble(text1.getText());
result = number1*number2;
text2.setText(Double.toString(result));*/
if (text.getText().isEmpty() || text1.getText().isEmpty()) {
throw new Exception();
}
number1 = text.getText();
number2 = text1.getText();
result = number1.concat(number2);
text2.toString();
text2.setText(result);
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "Please add a letter");
}
}
});
getContentPane().add(btn);
// Clear button
JButton clear = new JButton("Clear");
getContentPane().add(clear);
final JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
getContentPane().add(chckbxNewCheckBox);
final JCheckBox chckbxNewCheckBox_1 = new JCheckBox("New check box");
getContentPane().add(chckbxNewCheckBox_1);
chckbxNewCheckBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (chckbxNewCheckBox.isSelected()) {
chckbxNewCheckBox_1.setSelected(false);
}
}
});
chckbxNewCheckBox_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (chckbxNewCheckBox_1.isSelected()) {
chckbxNewCheckBox.setSelected(false);
}
}
});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
text.setText("");
text1.setText("");
text2.setText("");
} catch(Exception e1) {
}
}
});
//setVisible(true);
//setSize(400,200);
//setResizable(false);
//setLocationRelativeTo(null);
//setTitle("Calculator");
}
public static void main (String args[]) { // Creating the main method
frame frame = new frame(); // creating an object of the ShowGridLayout class, object is frame.
frame.setVisible(true);
frame.setSize(400, 200);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setTitle("Calculator");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);;
}
}
@Parkash Jat 請給我一個第二個選項的例子,我沒有看到它的例子:) –
我已經編輯了我的回覆,並附上我和@JeriesHG的建議樣本代碼 – user3190129
Thaaaaaaaaaaaank你這麼多,我會檢查他們,再次感謝你@ user3190129 –