用按钮设置文本

时间:2013-11-08 09:14:22

标签: java button layout border listener

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

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 ("  ");           
        }
    }





}

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

您可以通过执行以下操作将新文本连接到旧文本:

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

这将获取当前文本,然后在其末尾附加“3”。

答案 2 :(得分:0)

首先,你应该只有一个ButtonListener,因为每个ButtonListener都是相同的(除了它应该添加的数字。以下代码应该有效:

private class ButtonListener implements ActionListener
{
    private String value = "";

    public ButtonListner(String value) {
        this.value = value;
    }

    public void actionPerformed (ActionEvent event)
    {
        resultLabel.setText(resultLabel.getText() + value);           
    }
}

您可以通过以下方式初始化ButtonListeners:

button0.addActionListener(new ButtonListener("0"));