0

以下のコードがあります。私の目的は、ユーザーが電話番号を入力できるように、各ボタンをテキスト フィールドに追加できるようにすることです。私がうまくいかないように見える唯一のものは、複数のテキスト入力を許可するフィールドです。別のボタンをクリックすると、テキスト フィールドでそのボタンが置き換えられ、一度に 1 桁だけが表示されます。各ボタンを完全に置き換えるだけでなく、その番号を追加するようにするにはどうすればよいですか? また、国境管理者がコードに適用されないのはなぜですか? ありがとうございました!!

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





}
4

3 に答える 3

1

actionPerformedメソッドで、次のようにする必要があります。

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

あなたのコードには多くのコピー/貼り付けがあるようです。もっと簡単にできるか試してみてください。

于 2013-11-08T09:16:12.420 に答える
1

次のようにして、新しいテキストを古いテキストに連結できます。

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

これは現在のテキストを取得し、その末尾に「3」を追加します。

于 2013-11-08T09:16:19.537 に答える
0

まず、すべての ButtonListener が同じことを行うため (追加する必要がある数を除いて)、ButtonListener を 1 つだけ持つ必要があります。次のコードが機能するはずです。

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"));
于 2013-11-08T09:19:57.887 に答える