如果我按下一个按钮,按钮中的文本将出现在我的文本区域
if i press a button, the text from the button to appear on my text area
我开始制作带有 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();
}
}
首先:定义你的ActionListener
类在你的构造方法之外。
其次: 将您的 ActionListener
实例添加到您的每个 JButtons
Radical.addActionListener(new Patru());
第三: 像这样实现 actionPerformed
方法:
public void actionPerformed(ActionEvent e) {
JButton src = (JButton)e.getSource();
display.setText(src.getText());
}
这个问题的答案很简单:
- 可以使用
button.setText("sometext")
设置 JButton 中的文本,并且可以使用 String content = button.getText()
. 获取相同的文本
- 要使用 ActionListener,您可以将接口添加到您的 JFrame,如下所示:
public class GUI extends JFrame implements ActionListener {
您只需将 implements ActionListener 添加到现有行。
- 现在您需要使用
button.addActionListener(this)
将 ActionListener 添加到您的按钮 注意:在创建新的 Button 对象后立即添加 Action Listener 很重要。
您需要做的最后一件事是告诉 ActionListener 在单击按钮时他应该做什么。所以你需要在你的 GUI 中创建一个方法 class:
@Override
public void actionPerformed(ActionEvent e) {
textfield.setText(button.getText());
}
这基本上就是您在单击按钮时更改 JTextField 中的文本所需的全部内容。
我开始制作带有 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();
}
}
首先:定义你的ActionListener
类在你的构造方法之外。
其次: 将您的 ActionListener
实例添加到您的每个 JButtons
Radical.addActionListener(new Patru());
第三: 像这样实现 actionPerformed
方法:
public void actionPerformed(ActionEvent e) {
JButton src = (JButton)e.getSource();
display.setText(src.getText());
}
这个问题的答案很简单:
- 可以使用
button.setText("sometext")
设置 JButton 中的文本,并且可以使用String content = button.getText()
. 获取相同的文本
- 要使用 ActionListener,您可以将接口添加到您的 JFrame,如下所示:
public class GUI extends JFrame implements ActionListener {
您只需将 implements ActionListener 添加到现有行。 - 现在您需要使用
button.addActionListener(this)
将 ActionListener 添加到您的按钮 注意:在创建新的 Button 对象后立即添加 Action Listener 很重要。 您需要做的最后一件事是告诉 ActionListener 在单击按钮时他应该做什么。所以你需要在你的 GUI 中创建一个方法 class:
@Override
public void actionPerformed(ActionEvent e) {
textfield.setText(button.getText());
}
这基本上就是您在单击按钮时更改 JTextField 中的文本所需的全部内容。