我有一個計算器代碼和問題是:如何停止從java中的另一種方法調用方法?
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 method.When EVENODD方法運行運行valueCheck必須有停止calling.Is一個事件要做到這一點我想停止呼籲valueCheck時EVENODD運行
?這是我的整個代碼。
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;
}
}
你似乎沒有多線程,所以當'evenOdd'運行時,沒有其他任何東西。所以你是什麼意思? –
我的意思是; valueCheck方法的工作並不是同時點擊3個按鈕.evenOdd方法的工作是不同時點擊一個偶數和一個奇數。例如,我點擊了2,然後是3。 「你不能單擊一個單一的奇偶」然後我想單擊一個偶數如4,6,8。現在會有一個警告「你不能單擊3個按鈕」。然後我不能點擊一個均勻因此這些方法不能同時正確運行。 – cansukartal
你能分享一下你需要通過這個實現目標嗎? –