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