2016-02-19 55 views
0

我想在一個小程序中創建,當一個複選框被選中時,爲特定按鈕執行的動作被改變爲執行的另一個動作,讓我們假設執行的按鈕動作給出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);; 
} 

}

回答

0

它可以以多種方式來解決。

  1. 您可以有一個整型字段(成員變量或靜態),並在選擇不同的複選框時更新它的值。通過查看靜態整數的值,爲每個要執行的動作定義不同的方法,並在按鈕動作偵聽器中調用它們。

OR

  • 保持加入的按鈕的ActionListener的引用,並上的複選框的選擇從按鈕移除現有的動作偵聽器,並添加新的和更新引用新的監聽者。
  • 我希望它有幫助。

    編輯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); 
          ; 
         } 
    
        } 
    
    +0

    @Parkash Jat 請給我一個第二個選項的例子,我沒有看到它的例子:) –

    +0

    我已經編輯了我的回覆,並附上我和@JeriesHG的建議樣本代碼 – user3190129

    +0

    Thaaaaaaaaaaaank你這麼多,我會檢查他們,再次感謝你@ user3190129 –

    0

    讓我看看,如果我完全明白,一個按鈕的操作取決於在一個複選框?如果這是正確的,那麼你可以有一個布爾值的複選框的值或只是檢查「isSelected」功能,並在行動執行方法,我只是做一個布爾檢查並執行我需要的方法。

    JButton btn = new JButton("Calculate"); 
    btn.addActionListener(new ActionListener() { 
    if(chckbxNewCheckBox.isSelected()){ 
        //do something here 
    }else{ 
        //do this 
    } 
    } 
    

    編輯: 我也將具有在一個分離的方法「進行動作」按鈕,以便重複使用的代碼。 (如果我需要在另一個按鈕或類似的東西中使用相同的方法)。

    +0

    你的想法是真棒,但是當你上面提到的它的使用chckbxNewCheckBox無法識別,因爲它是幹訓局後和之前未聲明,對此有何建議? –

    +0

    看來@ user3190129回答了這個問題,對不起,我花了一點時間回答。希望它有效! – JeriesHG