您好,这真的很长,很抱歉,我的代码有效,但是如何仅按一下按钮就可以在标签上打印多个数字。我的代码还不完整,但我只想朝着正确的方向前进。感谢您提供的任何帮助。

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

        }
    }
}

最佳答案

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

07-28 04:10