gpt4 book ai didi

java - 使用按钮设置文本

转载 作者:行者123 更新时间:2023-12-02 06:33:45 25 4
gpt4 key购买 nike

我下面有以下代码。我的目标是允许每个按钮添加到文本字段,以便用户可以输入电话号码。我似乎唯一无法工作的是允许多个文本输入的字段。单击另一个按钮后,它会在文本字段中替换它,因此一次只显示一位数字。我该如何解决这个问题,以便每个按钮都添加其编号而不只是完全替换它?另外,为什么我的边境经理不适用该代码?谢谢!!

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class Keys
{

private JPanel phone;

public static void main (String[] args)
{

JFrame frame = new JFrame ("Keys");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel phone = new JPanel();
phone.setBorder (BorderFactory.createRaisedBevelBorder());

JTabbedPane tp = new JTabbedPane();
tp.addTab ("KeyPad", new KeyPad());

frame.getContentPane().add(phone);
frame.getContentPane().add(tp);
frame.pack();
frame.pack();frame.setVisible(true);

}
}






import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class KeyPad extends JPanel
{
private JLabel resultLabel;


private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonclear;

public KeyPad()
{

setLayout (new GridLayout (5, 3));

resultLabel = new JLabel ("---");
button0 = new JButton ("0");
button1 = new JButton ("1");
button2 = new JButton ("2");
button3 = new JButton ("3");
button4 = new JButton ("4");
button5 = new JButton ("5");
button6 = new JButton ("6");
button7 = new JButton ("7");
button8 = new JButton ("8");
button9 = new JButton ("9");
buttonclear = new JButton ("Clear");

button0.addActionListener (new ButtonListener0());
button1.addActionListener (new ButtonListener1());
button2.addActionListener (new ButtonListener2());
button3.addActionListener (new ButtonListener3());
button4.addActionListener (new ButtonListener4());
button5.addActionListener (new ButtonListener5());
button6.addActionListener (new ButtonListener6());
button7.addActionListener (new ButtonListener7());
button8.addActionListener (new ButtonListener8());
button9.addActionListener (new ButtonListener9());
buttonclear.addActionListener(new ButtonListenerC());


add (resultLabel);
add (button0);
add (button1);
add (button2);
add (button3);
add (button4);
add (button5);
add (button6);
add (button7);
add (button8);
add (button9);
add (buttonclear);


setPreferredSize (new Dimension(250,250));
setBackground (Color.green);
}





private class ButtonListener0 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{


resultLabel.setText ("0");
}
}


private class ButtonListener1 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{


resultLabel.setText ("1");
}
}

private class ButtonListener2 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{


resultLabel.setText ("2");
}
}


private class ButtonListener3 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{


resultLabel.setText ("3");
}
}

private class ButtonListener4 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{


resultLabel.setText ("4");
}
}

private class ButtonListener5 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{


resultLabel.setText ("5");
}
}



private class ButtonListener6 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{


resultLabel.setText ("6");
}
}





private class ButtonListener7 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{


resultLabel.setText ("7");
}
}






private class ButtonListener8 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{


resultLabel.setText ("8");
}
}




private class ButtonListener9 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{


resultLabel.setText ("9");
}
}


private class ButtonListenerC implements ActionListener
{
public void actionPerformed (ActionEvent event)
{


resultLabel.setText (" ");
}
}





}

最佳答案

在每个 actionPerformed 方法中,如果应该是:

resultLabel.setText (resultLabel.getText()+"theNumber");  

您的代码中似乎有很多复制/粘贴。尝试看看是否可以让它变得更简单。

关于java - 使用按钮设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19855295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com