如何使用键盘按钮将多个数字打印到标签上

时间:2013-04-10 03:19:49

标签: java swing jbutton jlabel

你好,这真的很长我很抱歉我的代码可以工作,但如何只需按下按钮就可以在我的标签上打印多个数字。我的代码不完整,但我只想推动正确的方向。感谢您提供任何帮助。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;

public class NumericKeyPadPanel extends JPanel {

    private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, clear;
    private JLabel label1;
    private JPanel primary, panel2, panel3, panel4;

    public NumericKeyPadPanel() {
        label1 = new JLabel();
        primary = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel4 = new JPanel();
//Set up for the center grid
        panel2.setLayout(new GridLayout(4, 3));
        panel2.setBackground(Color.gray);
        panel2.setBorder(BorderFactory.createLineBorder(Color.black, 4));
//set up for the top box
        panel3.setBackground(Color.white);
        panel3.setBorder(BorderFactory.createEtchedBorder());
        panel3.add(label1);
//set up for the bottom
        panel4.setBackground(Color.gray);
//set up for the buttons for center and bottom
        ButtonListener listener1 = new ButtonListener();
        ButtonListener listener2 = new ButtonListener();
        ButtonListener listener3 = new ButtonListener();
        ButtonListener listener4 = new ButtonListener();
        ButtonListener listener5 = new ButtonListener();
        ButtonListener listener6 = new ButtonListener();
        ButtonListener listener7 = new ButtonListener();
        ButtonListener listener8 = new ButtonListener();
        ButtonListener listener9 = new ButtonListener();
        ButtonListener listener10 = new ButtonListener();
        ButtonListener listener11 = new ButtonListener();
        ButtonListener listener12 = new ButtonListener();
        ButtonListener listener13 = new ButtonListener();

        b1 = new JButton("1");
        b1.addActionListener(listener1);

        b2 = new JButton("2");
        b2.addActionListener(listener2);

        b3 = new JButton("3");
        b3.addActionListener(listener3);

        b4 = new JButton("4");
        b4.addActionListener(listener4);

        b5 = new JButton("5");
        b5.addActionListener(listener5);

        b6 = new JButton("6");
        b6.addActionListener(listener6);

        b7 = new JButton("7");
        b7.addActionListener(listener7);

        b8 = new JButton("8");
        b8.addActionListener(listener8);

        b9 = new JButton("9");
        b9.addActionListener(listener9);

        b10 = new JButton("*");
        b10.addActionListener(listener10);

        b11 = new JButton("0");
        b11.addActionListener(listener11);

        b12 = new JButton("#");
        b12.addActionListener(listener12);

        clear = new JButton("Clear");
        clear.addActionListener(listener13);

        panel2.add(b1);
        panel2.add(b2);
        panel2.add(b3);
        panel2.add(b4);
        panel2.add(b5);
        panel2.add(b6);
        panel2.add(b7);
        panel2.add(b8);
        panel2.add(b9);
        panel2.add(b10);
        panel2.add(b11);
        panel2.add(b12);
        panel4.add(clear);
//set up for main panel
        primary.setLayout(new BorderLayout());
        primary.setBackground(Color.gray);

        primary.add(panel2, BorderLayout.CENTER);
        primary.add(panel3, BorderLayout.NORTH);
        primary.add(panel4, BorderLayout.SOUTH);
        add(primary);
    }
    // this is the listener

    private class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == b1) {
                label1.setText("1");
            }

        }
    }
}

2 个答案:

答案 0 :(得分:1)

将要打印的文本存储在单独的实例变量(String)中的标签上。每按一次按钮,将数字附加到此文本并将其设置在标签上。

答案 1 :(得分:1)

只需将结果附加到标签已包含的内容

label.setText(label.getText() + "1");