我试图在一个小程序中创建,当我选择一个复选框时,为特定按钮执行的操作将被更改为执行的另一个操作,让我们说按钮执行的动作给出了1 + 2的结果,我希望当我勾选复选框以使按钮动作执行时只是例如。
这是我的代码
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);;
}
}
答案 0 :(得分:0)
可以通过多种方式解决。
OR
我希望它有所帮助。
编辑1:如果您只想一次选择一个选项,最好使用单选按钮。
编辑2: 在这里,您可以使用单选按钮以及删除和添加侦听器来执行此操作。
public class frame extends JFrame { // Declaring the frame class
final JTextField text;
final JTextField text1;
final JTextField text2;
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
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
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
text2 = new JTextField();
getContentPane().add(text2);
text2.setHorizontalAlignment(SwingConstants.CENTER);
text2.setEditable(false);
final AddValuesActionListener actionListener = new AddValuesActionListener();
// Calculation button
final JButton btn = new JButton("Calculate");
btn.addActionListener(actionListener);
final JRadioButton addRadioButton = new JRadioButton("Add", true);
addRadioButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn.removeActionListener(actionListener);
btn.addActionListener(new AddValuesActionListener());
}
});
final JRadioButton subtractRadioButton = new JRadioButton("Subtract", true);
subtractRadioButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn.removeActionListener(actionListener);
btn.addActionListener(new SubtractValuesActionListener());
}
});
getContentPane().add(btn);
// Clear button
JButton clear = new JButton("Clear");
getContentPane().add(clear);
ButtonGroup radioButtons = new ButtonGroup();
radioButtons.add(addRadioButton);
radioButtons.add(subtractRadioButton);
getContentPane().add(addRadioButton);
getContentPane().add(subtractRadioButton);
}
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);
;
}
private class AddValuesActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text2.setText(text.getText() + "+" + text1.getText()); //Create add values method
}
}
private class SubtractValuesActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text2.setText(text.getText() + "-" + text1.getText()); //Create add values method
}
}
}
以下是您可以通过检查@JeriesHG
建议的单选按钮选择来做到的 public class frame extends JFrame { // Declaring the frame class
final JTextField text;
final JTextField text1;
final JTextField text2;
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
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
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
text2 = new JTextField();
getContentPane().add(text2);
text2.setHorizontalAlignment(SwingConstants.CENTER);
text2.setEditable(false);
final JRadioButton addRadioButton = new JRadioButton("Add", true);
final JRadioButton subtractRadioButton = new JRadioButton("Subtract", true);
// Calculation button
final JButton btn = new JButton("Calculate");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (addRadioButton.isSelected()) {
text2.setText(text.getText() + "+" + text1.getText()); //Create add values method
} else if (subtractRadioButton.isSelected()) {
text2.setText(text.getText() + "-" + text1.getText()); //Create subtract values method
}
}
});
getContentPane().add(btn);
// Clear button
JButton clear = new JButton("Clear");
getContentPane().add(clear);
ButtonGroup radioButtons = new ButtonGroup();
radioButtons.add(addRadioButton);
radioButtons.add(subtractRadioButton);
getContentPane().add(addRadioButton);
getContentPane().add(subtractRadioButton);
}
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);
;
}
}
答案 1 :(得分:0)
让我看看我是否完全理解,按钮的动作取决于复选框?如果这是正确的,那么你可以为复选框值设置一个布尔值,或者只检查" isSelected"函数和动作执行方法,我只是做一个布尔检查并执行我需要的方法。
JButton btn = new JButton("Calculate");
btn.addActionListener(new ActionListener() {
if(chckbxNewCheckBox.isSelected()){
//do something here
}else{
//do this
}
}
编辑: 我也会执行"动作"分离方法中的按钮,以便重用代码。 (在我要求在另一个按钮或类似的东西中使用相同的方法的情况下)。