2013年10月22日星期二

Seeking help see why this program does not respond to button events !

import java.awt. *;

import javax.swing. *;
import java.awt.event. *; / / do a simple calculator program , a calculator interface
/ / but not yet fully realized when the function button to add the event to find the response time to press the button , but no response , please help look it .
public class window {
JFrame aq;
JPanel all;
JPanel al2;
int shu1 = 0;
int shu2 = 0;
String res = "0", jj = "0";
JButton a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16; / / 16 a calculator button
JTextField t1;
public static void main (String args [])
{

window ll = new window (); / / Driver
ll.go ();
}


public void go ()
{
ActionListener ajj = new ActionListener () {/ / Add an event response

@ Override
public void actionPerformed (ActionEvent e) {

String rn = e.getActionCommand ();
if (rn == "0") / / If you press the Zero key, the text speaks the text box is set to SASAG
/ / Zero button click , but did not respond
{

res = (shu1 + shu2) + "";
shu2 = 0;
jj = shu2 + "";
t1.setText ("sasaga");

}
else if (rn == "1")
{
shu1 = shu2;
shu2 = 1;
jj = shu2 + "";
t1.setText (jj);
}
else if (rn == "2")
{
shu1 = shu2;
shu2 = 2;
}
else if (rn == "3")
{
shu1 = shu2;
shu2 = 3;
}
else if (rn == "4")
{
shu1 = shu2;
shu2 = 4;
}
else if (rn == "5")
{
shu1 = shu2;
shu2 = 5;
}
else if (rn == "6")
{
shu1 = shu2;
shu2 = 6;}

else if (rn == "7")
{
shu1 = shu2;
shu2 = 7;
}
else if (rn == "8")
{
shu1 = shu2;
shu2 = 8;
}
else if (rn == "9")
{
shu1 = shu2;
shu2 = 9;
}
else if (rn == "+")
{
res = (shu1 + shu2) + "";
shu1 = shu1 + shu2;
t1.setText (res);
}
else if (rn == "-")
{}
else if (rn == "*")
{}
else if (rn == "/")
{}
else if (rn == ".")
{}
else
{}


}
};
JPanel all = new JPanel ();
JPanel al2 = new JPanel ();
JTextField t1 = new JTextField ("0", 30);
JFrame aq = new JFrame ("ssss");
a1 = new JButton ("0");
a2 = new JButton ("1");
a3 = new JButton ("2");
a4 = new JButton ("3");
a5 = new JButton ("4");
a6 = new JButton ("5");
a7 = new JButton ("6");
a8 = new JButton ("7");
a9 = new JButton ("8");
a10 = new JButton ("9");
a11 = new JButton ("+");
a12 = new JButton ("-");
a13 = new JButton ("*");
a14 = new JButton ("/");
a15 = new JButton (".");
a16 = new JButton ("=");

Container af = aq.getContentPane ();
af.setLayout (new GridLayout (2,1));
all.add (t1);
al2.setLayout (new GridLayout (4,4));
al2.add (a8);
al2.add (a9);
al2.add (a10);
al2.add (a11);
al2.add (a5);
al2.add (a6);
al2.add (a7);
al2.add (a12);
al2.add (a2);
al2.add (a3);
al2.add (a4);
al2.add (a13);
al2.add (a1);
al2.add (a15);
al2.add (a16);
al2.add (a14);
a1.addActionListener (ajj);
a2.addActionListener (ajj);
a3.addActionListener (ajj);
a4.addActionListener (ajj);
a5.addActionListener (ajj);
a6.addActionListener (ajj);
a7.addActionListener (ajj);
a8.addActionListener (ajj);
a9.addActionListener (ajj);
a10.addActionListener (ajj);
a11.addActionListener (ajj);
a12.addActionListener (ajj);
a13.addActionListener (ajj);
a14.addActionListener (ajj);
a15.addActionListener (ajj);
a16.addActionListener (ajj);
af.add (all);
af.add (al2);

aq.pack ();
aq.setVisible (true);


};



};



------ Solution ------------------------------------ --------

landlord such as using debug testing will know if there is actually reaction .
Your mistake is not listening , but rather
if (rn == "0")
such judgments, 0 is the string
string comparison , then turned into
if (rn. equals ("0"))
------ Solution -------------------- ------------------------
throw anomaly .
help you change the next, pay attention to the class name to uppercase , Window


import java.awt.*;

import javax.swing.*;
import java.awt.event.*; //做的事一个简单的计算器程序,有计算器的界面
//但是还没有完全实现功能的时候就发现给按钮添加了事件响应但按按钮的时候去没有反应,请帮忙看下吧。

public class Window {
JFrame aq;
JPanel all;
JPanel al2;
int shu1 = 0;
int shu2 = 0;
String res = "0", jj = "0";
JButton a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15,
a16; // 16个计算器按钮
JTextField t1;

public static void main(String args[]) {
Window ll = new Window(); // 驱动程序
}

public Window(){
JPanel all = new JPanel();
JPanel al2 = new JPanel();
final JTextField t1 = new JTextField("0", 30);
JFrame aq = new JFrame("ssss");
a1 = new JButton("0");
a2 = new JButton("1");
a3 = new JButton("2");
a4 = new JButton("3");
a5 = new JButton("4");
a6 = new JButton("5");
a7 = new JButton("6");
a8 = new JButton("7");
a9 = new JButton("8");
a10 = new JButton("9");
a11 = new JButton("+");
a12 = new JButton("-");
a13 = new JButton("*");
a14 = new JButton("/");
a15 = new JButton(".");
a16 = new JButton("=");

Container af = aq.getContentPane();
af.setLayout(new GridLayout(2, 1));
all.add(t1);
al2.setLayout(new GridLayout(4, 4));
al2.add(a8);
al2.add(a9);
al2.add(a10);
al2.add(a11);
al2.add(a5);
al2.add(a6);
al2.add(a7);
al2.add(a12);
al2.add(a2);
al2.add(a3);
al2.add(a4);
al2.add(a13);
al2.add(a1);
al2.add(a15);
al2.add(a16);
al2.add(a14);
ActionListener ajj = new ActionListener() { // 添加事件响应

@Override
public void actionPerformed(ActionEvent e) {

String rn = e.getActionCommand();
System.out.println(rn);
if (rn == "0") // 如果按下零号键,则讲文本框里的文字设置为SASAG
// 可是点击零号键却没有反应
{

res = (shu1 + shu2) + "";
shu2 = 0;
jj = shu2 + "";
t1.setText("sasaga");

} else if (rn == "1") {
shu1 = shu2;
shu2 = 1;
jj = shu2 + "";
t1.setText(jj);
} else if (rn == "2") {
shu1 = shu2;
shu2 = 2;
} else if (rn == "3") {
shu1 = shu2;
shu2 = 3;
} else if (rn == "4") {
shu1 = shu2;
shu2 = 4;
} else if (rn == "5") {
shu1 = shu2;
shu2 = 5;
} else if (rn == "6") {
shu1 = shu2;
shu2 = 6;
}

else if (rn == "7") {
shu1 = shu2;
shu2 = 7;
} else if (rn == "8") {
shu1 = shu2;
shu2 = 8;
} else if (rn == "9") {
shu1 = shu2;
shu2 = 9;
} else if (rn == "+") {
res = (shu1 + shu2) + "";
shu1 = shu1 + shu2;
t1.setText(res);
} else if (rn == "-") {
} else if (rn == "*") {
} else if (rn == "/") {
} else if (rn == ".") {
} else {
}

}
};
a1.addActionListener(ajj);
a2.addActionListener(ajj);
a3.addActionListener(ajj);
a4.addActionListener(ajj);
a5.addActionListener(ajj);
a6.addActionListener(ajj);
a7.addActionListener(ajj);
a8.addActionListener(ajj);
a9.addActionListener(ajj);
a10.addActionListener(ajj);
a11.addActionListener(ajj);
a12.addActionListener(ajj);
a13.addActionListener(ajj);
a14.addActionListener(ajj);
a15.addActionListener(ajj);
a16.addActionListener(ajj);
af.add(all);
af.add(al2);

aq.pack();
aq.setVisible(true);
}
};

------ For reference only ----------------------------------- ----
! ! ! Seeking help ah ! !
------ For reference only -------------------------------------- -

   else if(rn=="-")
    {}
    else if(rn=="*")
    {}
    else if(rn=="/")
    {}
    else if(rn==".")
    {}
    else
    {}


- * /. button events where there is no logical , of course, does not respond . Add some logic , or write a print statement to know if there was triggered
------ For reference only ----------------------- ----------------

landlord such as using debug testing will know if there is actually reaction .   
Your mistake is not listening , but rather   
if (rn == "0")   
such judgments, 0 is the string   
string comparison , then turned into   
if (rn. equals ("0"))   Thank you for taking the time
First, look at me wrong ! ! Grateful , but I do it according to your statement , and any course no response , then you can help me look at it ! ! Thank you
------ For reference only ---------------------------------------




I first thank the judge sentences in logical statements , and should respond to the problems yet to be resolved , so we only had a key first want to try , the results did not react well tangled oh ~ ~ ~ ~ ~
------ For reference only ---------------------------------------

landlord such as the use debug testing will know if there is actually reaction .     
Your mistake is not listening , but rather     
if (rn == "0")     
such judgments, 0 is the string     
string comparison , then turned into     
if (rn. equals ("0"))          Thank you for taking the time
First, look at me wrong ! ! Grateful , but I do it according to your statement , and any course no response , then you can help me look at it ! ! Thank you     
changed, have responded . Of course, just press 0 to react to the other you change it shining . .
your mistakes I wrote in comments , and ! ! beginning

package com;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Test3 {
JFrame aq;
JPanel all;
JPanel al2;
int shu1 = 0;
int shu2 = 0;
String res = "0", jj = "0";
JButton a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15,
a16; // 16个计算器按钮
JTextField t1;

public static void main(String args[]) {

Test3 ll = new Test3(); // 驱动程序
ll.go();
}

public void go() {
t1 = new JTextField("0", 30);//!!放到这里,要不报空指针。

ActionListener ajj = new ActionListener() { // 添加事件响应

@Override
public void actionPerformed(ActionEvent e) {

String rn = e.getActionCommand();
System.out.println("rn:"+rn);
if (rn .equals( "0")) // 如果按下零号键,则讲文本框里的文字设置为SASAG//!!改成equals形式
// 可是点击零号键却没有反应
{
System.out.println("点击了0");
res = (shu1 + shu2) + "";
shu2 = 0;
jj = shu2 + "";
t1.setText("sasaga");

} else if (rn == "1") {
shu1 = shu2;
shu2 = 1;
jj = shu2 + "";
t1.setText(jj);
} else if (rn == "2") {
shu1 = shu2;
shu2 = 2;
} else if (rn == "3") {
shu1 = shu2;
shu2 = 3;
} else if (rn == "4") {
shu1 = shu2;
shu2 = 4;
} else if (rn == "5") {
shu1 = shu2;
shu2 = 5;
} else if (rn == "6") {
shu1 = shu2;
shu2 = 6;
}

else if (rn == "7") {
shu1 = shu2;
shu2 = 7;
} else if (rn == "8") {
shu1 = shu2;
shu2 = 8;
} else if (rn == "9") {
shu1 = shu2;
shu2 = 9;
} else if (rn == "+") {
res = (shu1 + shu2) + "";
shu1 = shu1 + shu2;
t1.setText(res);
} else if (rn == "-") {
} else if (rn == "*") {
} else if (rn == "/") {
} else if (rn == ".") {
} else {
}

}
};
JPanel all = new JPanel();
JPanel al2 = new JPanel();

JFrame aq = new JFrame("ssss");
a1 = new JButton("0");
a2 = new JButton("1");
a3 = new JButton("2");
a4 = new JButton("3");
a5 = new JButton("4");
a6 = new JButton("5");
a7 = new JButton("6");
a8 = new JButton("7");
a9 = new JButton("8");
a10 = new JButton("9");
a11 = new JButton("+");
a12 = new JButton("-");
a13 = new JButton("*");
a14 = new JButton("/");
a15 = new JButton(".");
a16 = new JButton("=");

Container af = aq.getContentPane();
af.setLayout(new GridLayout(2, 1));
all.add(t1);
al2.setLayout(new GridLayout(4, 4));
al2.add(a8);
al2.add(a9);
al2.add(a10);
al2.add(a11);
al2.add(a5);
al2.add(a6);
al2.add(a7);
al2.add(a12);
al2.add(a2);
al2.add(a3);
al2.add(a4);
al2.add(a13);
al2.add(a1);
al2.add(a15);
al2.add(a16);
al2.add(a14);
a1.addActionListener(ajj);
a2.addActionListener(ajj);
a3.addActionListener(ajj);
a4.addActionListener(ajj);
a5.addActionListener(ajj);
a6.addActionListener(ajj);
a7.addActionListener(ajj);
a8.addActionListener(ajj);
a9.addActionListener(ajj);
a10.addActionListener(ajj);
a11.addActionListener(ajj);
a12.addActionListener(ajj);
a13.addActionListener(ajj);
a14.addActionListener(ajj);
a15.addActionListener(ajj);
a16.addActionListener(ajj);
af.add(all);
af.add(al2);

aq.pack();
aq.setVisible(true);
}
}

------ For reference only ----------------------------------- ----




Thank you ! ! A success ! ! ! !

没有评论:

发表评论