我开始使用GUI制作Java计算器,但我遇到了问题。我是Java新手,如果我的问题很愚蠢,我很抱歉。我想知道如何正确编写Action Listener的代码,这样我就可以按下一个按钮,并在该按钮上写入的文本出现在文本区域。所有评论都是我试图做但却没有工作的。
package calculator;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class GUI extends JFrame {
JButton Radical;
JButton ImpartireLa1;
JButton Inmultire;
JButton Impartire;
JButton Scadere;
JButton Adunare;
JButton Egal;
JButton Zero;
JButton Sapte;
JButton Opt;
JButton Noua;
JButton Sase;
JButton Cinci;
JButton Patru;
JButton Trei;
JButton Doi;
JButton Unu;
StringBuilder tex;
JTextField display;
JLabel lol;
GUI (){
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1400,800);
this.setLayout(null);
Radical = new JButton("rad");
Radical.setBounds(100,100,60,60);
ImpartireLa1= new JButton("1/x");
ImpartireLa1.setBounds(200,100,60,60);
Inmultire= new JButton("*");
Inmultire.setBounds(300,100,60,60);
Impartire= new JButton("/");
Impartire.setBounds(400,100,60,60);
Scadere= new JButton("-");
Scadere.setBounds(500,100,60,60);
Adunare= new JButton("+");
Adunare.setBounds(500,200,60,60);
Egal= new JButton("=");
Egal.setBounds(500,300,60,60);
Sapte= new JButton("7");
Sapte.setBounds(100,200,80,80);
Opt= new JButton("8");
Opt.setBounds(230,200,80,80);
Noua= new JButton("9");
Noua.setBounds(360,200,80,80);
Patru= new JButton("4");
Patru.setBounds(100,300,80,80);
Cinci= new JButton("5");
Cinci.setBounds(230,300,80,80);
Sase= new JButton("6");
Sase.setBounds(360,300,80,80);
Unu= new JButton("1");
Unu.setBounds(100,400,80,80);
Doi= new JButton("2");
Doi.setBounds(230,400,80,80);
Trei= new JButton("3");
Trei.setBounds(360,400,80,80);
Zero= new JButton("0");
Zero.setBounds(480,400,80,80);
this.setLayout(null);
display= new JTextField();
display.setBounds(100,10, 465, 45);
add(Trei); add(Doi);add(Unu);add(Patru);add(Cinci); add(Sase);add(Sapte);add(Opt);add(Noua);add(Radical);add(ImpartireLa1);add(Inmultire); add(Impartire);add(Adunare);add(Scadere);add(Egal); add(Zero);add(display);setVisible(true);
class Patru implements ActionListener {
public void actionPerformed(ActionEvent e) {
//String s= text.getText()+ Patru.getText();
//text.setText(s);
/// text.setText( getText(4) );
/*JButton Patru = (JButton)e.getSource();
if(Patru.getText().equals("4"))
display.setText("");
lol.setVisible(true); */
/*if(e.getSource() == Patru)
{
String s = "4";
display.setText(s);
}*/
/* JButton Patru = (JButton)e.getSource();
String text = Patru.getText();
Object tex;
if (text.equals("4")) {
doMath math = new doMath();
int result = math.doMath1(tex.toString());
tex = new StringBuilder(32);
} else {
((StringBuilder) tex).append(text);
}*/
/* String text = "4";
if(e.getSource() == Patru)
{
text += "4";
display.setText(text);
}
}*/
}
}
class Cinci implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
public void ActionPerformed(ActionEvent e1)
{
String s1= display.getText()+ Cinci.getText();
display.setText(s1);
}
}
}
public static void main(String[] args)
{
GUI Calculator= new GUI();
}
}
答案 0 :(得分:0)
首先:定义构造函数方法的<{1}}类 。
第二次:为ActionListener
ActionListener
的实例
JButtons
第三:实施Radical.addActionListener(new Patru());
方法,如下所示:
actionPerformed
答案 1 :(得分:0)
答案很简单:
button.setText("sometext")
设置JButton中的文字,并使用String content = button.getText()
获取相同的文字。public class GUI extends JFrame implements ActionListener {
您只需将ActionListener添加到现有行。button.addActionListener(this)
将ActionListener添加到按钮中注意:在创建新Button对象后立即添加Action Listener非常重要。您需要做的最后一件事是告诉ActionListener单击按钮时他应该做什么。所以你需要在GUI类中创建一个方法:
@覆盖
public void actionPerformed(ActionEvent e){
textfield.setText(button.getText());
}
基本上只需要在单击按钮时更改JTextField中的Text即可。