在点击按钮时,怎么能同时在标签里获得按钮名称,java
publicclassCalculator{privateFrameframe;privateTextFieldtextField;privatePanelp1;priv...
public class Calculator
{
private Frame frame;
private TextField textField;
private Panel p1;
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
private Button b7;
private Button b8;
private Button b9;
private Button b10;
private Button b11;
private Button b12;
private Button b13;
private Button b14;
private Button b15;
private Button b16;
public void calculate()
{ //界面实现
frame = new Frame("计算器");//生成面板
textField = new TextField(30);//定义文本框长
frame.add(textField,BorderLayout.NORTH);
//按钮面板p1
p1 = new Panel();
p1.setSize(300,900);
p1.setBackground(Color.blue);
frame.add(p1);
//设置按钮
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b10 = new Button("0");
b11 = new Button("+");
b12 = new Button("-");
b13 = new Button("*");
b14 = new Button("/");
b15 = new Button("=");
b16 = new Button("C");
//给按钮加监听器
b1.addActionListener(new Action());
b2.addActionListener(new Action());
b3.addActionListener(new Action());
b4.addActionListener(new Action());
b5.addActionListener(new Action());
b6.addActionListener(new Action());
b7.addActionListener(new Action());
b8.addActionListener(new Action());
b9.addActionListener(new Action());
b10.addActionListener(new Action());
b11.addActionListener(new Action());
b12.addActionListener(new Action());
b13.addActionListener(new Action());
b14.addActionListener(new Action());
b15.addActionListener(new Action());
b16.addActionListener(new Action());
//把按钮加入面板
p1.setLayout(new GridLayout(4,4));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b10);
p1.add(b11);
p1.add(b12);
p1.add(b13);
p1.add(b14);
p1.add(b15);
p1.add(b16);
frame.setSize(300,400);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s1 = e.getActionCommand();
s1 = textField.getText();
}
public static void main(String[] args)
{
Calculator cal = new Calculator();
cal.calculate();
}
}
class Action implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
String s1 = arg0.getActionCommand();
System.out.println(s1);//怎么在textField里显示出s1来
}
} 展开
{
private Frame frame;
private TextField textField;
private Panel p1;
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
private Button b7;
private Button b8;
private Button b9;
private Button b10;
private Button b11;
private Button b12;
private Button b13;
private Button b14;
private Button b15;
private Button b16;
public void calculate()
{ //界面实现
frame = new Frame("计算器");//生成面板
textField = new TextField(30);//定义文本框长
frame.add(textField,BorderLayout.NORTH);
//按钮面板p1
p1 = new Panel();
p1.setSize(300,900);
p1.setBackground(Color.blue);
frame.add(p1);
//设置按钮
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b10 = new Button("0");
b11 = new Button("+");
b12 = new Button("-");
b13 = new Button("*");
b14 = new Button("/");
b15 = new Button("=");
b16 = new Button("C");
//给按钮加监听器
b1.addActionListener(new Action());
b2.addActionListener(new Action());
b3.addActionListener(new Action());
b4.addActionListener(new Action());
b5.addActionListener(new Action());
b6.addActionListener(new Action());
b7.addActionListener(new Action());
b8.addActionListener(new Action());
b9.addActionListener(new Action());
b10.addActionListener(new Action());
b11.addActionListener(new Action());
b12.addActionListener(new Action());
b13.addActionListener(new Action());
b14.addActionListener(new Action());
b15.addActionListener(new Action());
b16.addActionListener(new Action());
//把按钮加入面板
p1.setLayout(new GridLayout(4,4));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b10);
p1.add(b11);
p1.add(b12);
p1.add(b13);
p1.add(b14);
p1.add(b15);
p1.add(b16);
frame.setSize(300,400);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s1 = e.getActionCommand();
s1 = textField.getText();
}
public static void main(String[] args)
{
Calculator cal = new Calculator();
cal.calculate();
}
}
class Action implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
String s1 = arg0.getActionCommand();
System.out.println(s1);//怎么在textField里显示出s1来
}
} 展开
3个回答
展开全部
这个很好搞啊,你的问题就在于不知道在ACTION类中如何历历芦获得JtextField ;
第一种:
你可以将监听方法加到自己的类中:
public class Calculator implements ActionListener
{
private Frame frame;
private TextField textField;
private Panel p1;
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
private Button b7;
private Button b8;
private Button b9;
private Button b10;
private Button b11;
private Button b12;
private Button b13;
private Button b14;
private Button b15;
private Button b16;
public void calculate()
{ //界面实现
frame = new Frame("计算器");//生成面板
textField = new TextField(30);//定烂银义文本框长
frame.add(textField,BorderLayout.NORTH);
//肢带按钮面板p1
p1 = new Panel();
p1.setSize(300,900);
p1.setBackground(Color.blue);
frame.add(p1);
//设置按钮
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b10 = new Button("0");
b11 = new Button("+");
b12 = new Button("-");
b13 = new Button("*");
b14 = new Button("/");
b15 = new Button("=");
b16 = new Button("C");
//给按钮加监听器
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
//把按钮加入面板
p1.setLayout(new GridLayout(4,4));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b10);
p1.add(b11);
p1.add(b12);
p1.add(b13);
p1.add(b14);
p1.add(b15);
p1.add(b16);
frame.setSize(300,400);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s1 = e.getActionCommand();
s1 = textField.getText() +s1;
textField.setText(s1);
}
public static void main(String[] args)
{
Calculator cal = new Calculator();
cal.calculate();
}
}
第二种:你可以将监听类放在外部,构造方法中将组建作为参数传进去,可以参考
nihaonihaoquo 的回答;
这二种都没测,但目测 都没有问题;你自己在研究一下;小程序,自己多动手练习练习;
第一种:
你可以将监听方法加到自己的类中:
public class Calculator implements ActionListener
{
private Frame frame;
private TextField textField;
private Panel p1;
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
private Button b7;
private Button b8;
private Button b9;
private Button b10;
private Button b11;
private Button b12;
private Button b13;
private Button b14;
private Button b15;
private Button b16;
public void calculate()
{ //界面实现
frame = new Frame("计算器");//生成面板
textField = new TextField(30);//定烂银义文本框长
frame.add(textField,BorderLayout.NORTH);
//肢带按钮面板p1
p1 = new Panel();
p1.setSize(300,900);
p1.setBackground(Color.blue);
frame.add(p1);
//设置按钮
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b10 = new Button("0");
b11 = new Button("+");
b12 = new Button("-");
b13 = new Button("*");
b14 = new Button("/");
b15 = new Button("=");
b16 = new Button("C");
//给按钮加监听器
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
//把按钮加入面板
p1.setLayout(new GridLayout(4,4));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b10);
p1.add(b11);
p1.add(b12);
p1.add(b13);
p1.add(b14);
p1.add(b15);
p1.add(b16);
frame.setSize(300,400);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s1 = e.getActionCommand();
s1 = textField.getText() +s1;
textField.setText(s1);
}
public static void main(String[] args)
{
Calculator cal = new Calculator();
cal.calculate();
}
}
第二种:你可以将监听类放在外部,构造方法中将组建作为参数传进去,可以参考
nihaonihaoquo 的回答;
这二种都没测,但目测 都没有问题;你自己在研究一下;小程序,自己多动手练习练习;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼主,我帮你修改了下,可以了,希望对你有帮助。import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator {
private Frame frame;
private TextField textField;
private Panel p1;
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
private Button b7;
private Button b8;
private Button b9;
private Button b10;
private Button b11;
private Button b12;
private Button b13;
private Button b14;
private Button b15;
private Button b16;
private Action action = new Action(this);//帮你修改的
public void calculate() { // 界面实现
frame = new Frame("计算器");// 生成面板
textField = new TextField(30);// 定义文本框长
frame.add(textField, BorderLayout.NORTH);
// 按钮面板p1
p1 = new Panel();
p1.setSize(300, 900);
p1.setBackground(Color.blue);
frame.add(p1);
// 设置按钮
b1 = new Button("悔猛悔1");
b2 = new Button("2");
b3 = new Button("碧正3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b10 = new Button("0");
b11 = new Button("+");
b12 = new Button("-");
b13 = new Button("*");
b14 = new Button("/");
b15 = new Button("=");
b16 = new Button("C");
// 给按钮加监听器
b1.addActionListener(action);//帮你修改的
b2.addActionListener(action);//帮你修改的
b3.addActionListener(action);//帮你修改的
b4.addActionListener(action);//帮你修知悔改的
b5.addActionListener(action);//帮你修改的
b6.addActionListener(action);//帮你修改的
b7.addActionListener(action);//帮你修改的
b8.addActionListener(action);//帮你修改的
b9.addActionListener(action);//帮你修改的
b10.addActionListener(action);//帮你修改的
b11.addActionListener(action);//帮你修改的
b12.addActionListener(action);//帮你修改的
b13.addActionListener(action);//帮你修改的
b14.addActionListener(action);//帮你修改的
b15.addActionListener(action);//帮你修改的
b16.addActionListener(action);//帮你修改的
// 把按钮加入面板
p1.setLayout(new GridLayout(4, 4));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b10);
p1.add(b11);
p1.add(b12);
p1.add(b13);
p1.add(b14);
p1.add(b15);
p1.add(b16);
frame.setSize(300, 400);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1 = e.getActionCommand();
s1 = textField.getText();
}
public static void main(String[] args) {
Calculator cal = new Calculator();
cal.calculate();
}
public TextField getTextField() {//帮你增加的
return textField;
}
}
class Action implements ActionListener {
private Calculator ca;
public Action(Calculator ca) {
this.ca = ca;
}
public void actionPerformed(ActionEvent arg0) {
String s1 = arg0.getActionCommand();
System.out.println(s1);// 怎么在textField里显示出s1来
ca.getTextField().setText(s1);//帮你增加的
}
}
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator {
private Frame frame;
private TextField textField;
private Panel p1;
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
private Button b7;
private Button b8;
private Button b9;
private Button b10;
private Button b11;
private Button b12;
private Button b13;
private Button b14;
private Button b15;
private Button b16;
private Action action = new Action(this);//帮你修改的
public void calculate() { // 界面实现
frame = new Frame("计算器");// 生成面板
textField = new TextField(30);// 定义文本框长
frame.add(textField, BorderLayout.NORTH);
// 按钮面板p1
p1 = new Panel();
p1.setSize(300, 900);
p1.setBackground(Color.blue);
frame.add(p1);
// 设置按钮
b1 = new Button("悔猛悔1");
b2 = new Button("2");
b3 = new Button("碧正3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b10 = new Button("0");
b11 = new Button("+");
b12 = new Button("-");
b13 = new Button("*");
b14 = new Button("/");
b15 = new Button("=");
b16 = new Button("C");
// 给按钮加监听器
b1.addActionListener(action);//帮你修改的
b2.addActionListener(action);//帮你修改的
b3.addActionListener(action);//帮你修改的
b4.addActionListener(action);//帮你修知悔改的
b5.addActionListener(action);//帮你修改的
b6.addActionListener(action);//帮你修改的
b7.addActionListener(action);//帮你修改的
b8.addActionListener(action);//帮你修改的
b9.addActionListener(action);//帮你修改的
b10.addActionListener(action);//帮你修改的
b11.addActionListener(action);//帮你修改的
b12.addActionListener(action);//帮你修改的
b13.addActionListener(action);//帮你修改的
b14.addActionListener(action);//帮你修改的
b15.addActionListener(action);//帮你修改的
b16.addActionListener(action);//帮你修改的
// 把按钮加入面板
p1.setLayout(new GridLayout(4, 4));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b10);
p1.add(b11);
p1.add(b12);
p1.add(b13);
p1.add(b14);
p1.add(b15);
p1.add(b16);
frame.setSize(300, 400);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1 = e.getActionCommand();
s1 = textField.getText();
}
public static void main(String[] args) {
Calculator cal = new Calculator();
cal.calculate();
}
public TextField getTextField() {//帮你增加的
return textField;
}
}
class Action implements ActionListener {
private Calculator ca;
public Action(Calculator ca) {
this.ca = ca;
}
public void actionPerformed(ActionEvent arg0) {
String s1 = arg0.getActionCommand();
System.out.println(s1);// 怎么在textField里显示出s1来
ca.getTextField().setText(s1);//帮你增加的
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询