如何停止从java中的另一个方法调用方法?

时间:2013-07-25 07:50:07

标签: java methods calculator

我有一个计算器代码,问题是:

 public void valueCheck(String value,JToggleButton button) {
        if(values.size()>2) 
        {
            processValue(value);
            button.setSelected(false);
            JOptionPane.showMessageDialog(this, "You cant click 3 buttons at the same time..");
        }
    }

有一个方法valueCheck和:

public boolean evenOdd(JToggleButton buton)
    {
        if(values.size()==2)
        {
        System.out.println((String)values.get(0));
        System.out.println((String)values.get(1));

        String number1 = new String(values.get(0));
        String number2 = new String(values.get(1));

        if(evenNumbers.contains(number1))
        {
            if(evenNumbers.contains(number2))
            {
                processValue(number1);
                processValue(number2);
            }
            else if(oddNumbers.contains(number2))
            {
                buton.setSelected(false);
                JOptionPane.showMessageDialog(this, "You cant click an even and an odd number at the same time..");

            }
        }
        else if(oddNumbers.contains(number1))
        {
            if(oddNumbers.contains(number2))
            {
                processValue(number1);
                processValue(number2);
            }
            else if(evenNumbers.contains(number2))
            {
                buton.setSelected(false);
                JOptionPane.showMessageDialog(this, "You cant click an even and an odd number at the same time..");
            }
        }
        }
        return false;
    }

有一种方法evenOdd。

我的问题是:我不希望valueCheck方法为evenOdd方法运行。当evenOdd方法运行时,valueCheck必须停止调用。是否有事件要执行此操作?我想在evenOdd运行时停止调用valueCheck

这是我的整个代码。

package tr.com.bites;
import com.sun.org.apache.xml.internal.utils.StopParseException;
import java.util.List;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JToggleButton;
public class CalculatorView_1 extends javax.swing.JFrame {
     JToggleButton[] buttons =  new JToggleButton[4];
    public CalculatorView_1() {
        initComponents();
        buttons[0]=jToggleButton12;
        buttons[1]=jToggleButton11;
        buttons[2]=jToggleButton10;
        buttons[3]=jToggleButton13;
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jToggleButton3 = new javax.swing.JToggleButton();
        jToggleButton4 = new javax.swing.JToggleButton();
        jToggleButton5 = new javax.swing.JToggleButton();
        jToggleButton6 = new javax.swing.JToggleButton();
        jToggleButton7 = new javax.swing.JToggleButton();
        jToggleButton8 = new javax.swing.JToggleButton();
        jToggleButton9 = new javax.swing.JToggleButton();
        jToggleButton10 = new javax.swing.JToggleButton();
        jToggleButton11 = new javax.swing.JToggleButton();
        jToggleButton12 = new javax.swing.JToggleButton();
        jToggleButton13 = new javax.swing.JToggleButton();
        jToggleButton14 = new javax.swing.JToggleButton();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToggleButton1.setText("1");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jToggleButton2.setText("2");
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton2ActionPerformed(evt);
            }
        });

        jToggleButton3.setText("3");
        jToggleButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton3ActionPerformed(evt);
            }
        });

        jToggleButton4.setText("5");
        jToggleButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton4ActionPerformed(evt);
            }
        });

        jToggleButton5.setText("4");
        jToggleButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton5ActionPerformed(evt);
            }
        });

        jToggleButton6.setText("6");
        jToggleButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton6ActionPerformed(evt);
            }
        });

        jToggleButton7.setText("8");
        jToggleButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton7ActionPerformed(evt);
            }
        });

        jToggleButton8.setText("7");
        jToggleButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton8ActionPerformed(evt);
            }
        });

        jToggleButton9.setText("9");
        jToggleButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton9ActionPerformed(evt);
            }
        });

        jToggleButton10.setText("*");
        jToggleButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton10ActionPerformed(evt);
            }
        });

        jToggleButton11.setText("-");
        jToggleButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton11ActionPerformed(evt);
            }
        });

        jToggleButton12.setText("+");
        jToggleButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton12ActionPerformed(evt);
            }
        });

        jToggleButton13.setText("/");
        jToggleButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton13ActionPerformed(evt);
            }
        });

        jToggleButton14.setText("=");
        jToggleButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton14ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jToggleButton8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton9))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jToggleButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton6))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jToggleButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton3)))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jToggleButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToggleButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToggleButton10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToggleButton13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToggleButton14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(78, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jToggleButton12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton10))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jToggleButton1)
                            .addComponent(jToggleButton2)
                            .addComponent(jToggleButton3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jToggleButton5)
                            .addComponent(jToggleButton4)
                            .addComponent(jToggleButton6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jToggleButton8)
                            .addComponent(jToggleButton7)
                            .addComponent(jToggleButton9))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToggleButton13)
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jToggleButton14)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(90, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                               
       processValue("1");
       valueCheck("1",jToggleButton1);
       evenOdd(jToggleButton1);
    }                                              

    private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                               
       processValue("2");
       valueCheck("2",jToggleButton2);
       evenOdd(jToggleButton2);
    }                                              

    private void jToggleButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                               
       processValue("3");
       valueCheck("3",jToggleButton3);
       evenOdd(jToggleButton3);
    }                                              

    private void jToggleButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                               
       processValue("4");
       valueCheck("4",jToggleButton5);
       evenOdd(jToggleButton5);
    }                                              

    private void jToggleButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                               
       processValue("5");
       valueCheck("5",jToggleButton4);
       evenOdd(jToggleButton4);
    }                                              

    private void jToggleButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                               
       processValue("6");
       valueCheck("6",jToggleButton6);
       evenOdd(jToggleButton6);
    }                                              

    private void jToggleButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                               
       processValue("7");
       valueCheck("7",jToggleButton8);
       evenOdd(jToggleButton8);
    }                                              

    private void jToggleButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                               
       processValue("8");
       valueCheck("8",jToggleButton7);
       evenOdd(jToggleButton7);
    }                                              

    private void jToggleButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                               
       processValue("9");
       valueCheck("9",jToggleButton9);
       evenOdd(jToggleButton9);
    }                                              

    private void jToggleButton12ActionPerformed(java.awt.event.ActionEvent evt) {                                                
       processValue("+");
       for(JToggleButton b: buttons)
           b.setSelected(false);
        if(values.contains("+"))
            jToggleButton12.setSelected(true);
    }                                               

    private void jToggleButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                                
       processValue("-");
       for(JToggleButton b: buttons)
           b.setSelected(false);
        if(values.contains("-"))
            jToggleButton11.setSelected(true);
    }                                               

    private void jToggleButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                                
       processValue("*");
       for(JToggleButton b: buttons)
           b.setSelected(false);
        if(values.contains("*"))
            jToggleButton10.setSelected(true);
    }                                               

    private void jToggleButton13ActionPerformed(java.awt.event.ActionEvent evt) {                                                
       processValue("/");
       for(JToggleButton b: buttons)
           b.setSelected(false);
        if(values.contains("/"))
            jToggleButton13.setSelected(true);
    }                                               

    private void jToggleButton14ActionPerformed(java.awt.event.ActionEvent evt) {                                                
        long total=-1;
        int firstNumber=-1;
        int secondNumber=-1;
        String process=null;
        for (String secilenler : values) {
            if(secilenler.equals("+"))
                process="+";  
            else if(secilenler.equals("-"))
                process="-";
            else if(secilenler.equals("*"))
                process="*"; 
            else if(secilenler.equals("/"))
                process="/";    
            else
            {
                if(firstNumber==-1)
                   firstNumber= Integer.parseInt(secilenler);
                else
                    secondNumber= Integer.parseInt(secilenler);
            }
        }
           if(process.equals("+"))
                total=firstNumber+secondNumber;
            else if(process.equals("-"))
                total=firstNumber-secondNumber;
            else if(process.equals("*"))
                total=firstNumber*secondNumber;
            else 
                total=firstNumber/secondNumber;
           jTextField1.setText(""+total);
    }                                               

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]){
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CalculatorView_1().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton10;
    private javax.swing.JToggleButton jToggleButton11;
    private javax.swing.JToggleButton jToggleButton12;
    private javax.swing.JToggleButton jToggleButton13;
    private javax.swing.JToggleButton jToggleButton14;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToggleButton jToggleButton3;
    private javax.swing.JToggleButton jToggleButton4;
    private javax.swing.JToggleButton jToggleButton5;
    private javax.swing.JToggleButton jToggleButton6;
    private javax.swing.JToggleButton jToggleButton7;
    private javax.swing.JToggleButton jToggleButton8;
    private javax.swing.JToggleButton jToggleButton9;
    // End of variables declaration                   
    private List<String> values = new ArrayList<String>();
    private List<String> evenNumbers = new ArrayList<String>();
    {
        evenNumbers.add("2");
        evenNumbers.add("4");
        evenNumbers.add("6");
        evenNumbers.add("8");
    }
    private List<String> oddNumbers = new ArrayList<String>();
    {
        oddNumbers.add("1");
        oddNumbers.add("3");
        oddNumbers.add("5");
        oddNumbers.add("7");
        oddNumbers.add("9");
    }
    public void processValue(String strValue)
    {
        if(values.contains(strValue))
            values.remove(strValue);
        else
            values.add(strValue);
    }

    public void valueCheck(String value,JToggleButton button) {
        if(values.size()>2) 
        {
            processValue(value);
            button.setSelected(false);
            JOptionPane.showMessageDialog(this, "You cant click 3 buttons at the same time..");
        }
    }
    public boolean evenOdd(JToggleButton buton)
    {
        if(values.size()==2)
        {
        System.out.println((String)values.get(0));
        System.out.println((String)values.get(1));

        String number1 = new String(values.get(0));
        String number2 = new String(values.get(1));

        if(evenNumbers.contains(number1))
        {
            if(evenNumbers.contains(number2))
            {
                processValue(number1);
                processValue(number2);
            }
            else if(oddNumbers.contains(number2))
            {
                buton.setSelected(false);
                JOptionPane.showMessageDialog(this, "You cant click an even and an odd number at the same time..");

            }
        }
        else if(oddNumbers.contains(number1))
        {
            if(oddNumbers.contains(number2))
            {
                processValue(number1);
                processValue(number2);
            }
            else if(evenNumbers.contains(number2))
            {
                buton.setSelected(false);
                JOptionPane.showMessageDialog(this, "You cant click an even and an odd number at the same time..");
            }
        }
        }
        return false;
    }
}

1 个答案:

答案 0 :(得分:1)

您的问题与多线程无关。 Multithreading涉及多个threads,因此单词本身。在您的情况下,您可以使用简单的if-else语句和相关方法的正确返回值来实现您想要做的事情。

我发现您的方法evenOdd()已经返回boolean。你可以利用它。在evenOdd()中显示错误消息后,将其返回false。然后,当按下切换按钮时,测试evenOdd()是否返回true然后调用valueCheck(),否则,如果返回false(显示错误消息),则不要调用它。

if(evenOdd(jToggleButton1))
   valueCheck("1", jToggleButton1);

此外,您当前的方法名称令人困惑。尝试使用您为方法命名的方式。应该以这样的方式命名方法,当你看到它时,你已经知道它的作用。