我正在尝试创建一个数字小键盘,它有一个清晰的按钮和一个显示点击数字的顶部。到目前为止我有

时间:2013-11-25 22:11:42

标签: java keypad

我很难弄清楚如何在整个面板上拉伸清晰按钮。我也很难找到如何创建面板上方显示用户点击的键的部分。请帮忙?这是我到目前为止...我试图改变清除按钮的大小,但代码的那部分什么也没做。

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TelephonePanel extends JPanel
{
    public TelephonePanel()
    {
        JPanel panel = new JPanel ();
        panel.setLayout(new BorderLayout());
        setLayout (new GridLayout (5, 3));
        setPreferredSize (new Dimension(300, 400));

        JButton b1 = new JButton ("1");
        JButton b2 = new JButton ("2");
        JButton b3 = new JButton ("3");
        JButton b4 = new JButton ("4");
        JButton b5 = new JButton ("5");
        JButton b6 = new JButton ("6");
        JButton b7 = new JButton ("7");
        JButton b8 = new JButton ("8");
        JButton b9 = new JButton ("9");
        JButton ba = new JButton ("*");
        JButton b0 = new JButton ("0");
        JButton bp = new JButton ("#");

        JButton bclear = new JButton ("Clear");
        bclear.setSize(new Dimension(300, 100));

        add (b1);
        add (b2);
        add (b3);
        add (b4);
        add (b5);
        add (b6);
        add (b7);
        add (b8);
        add (b9);
        add (ba);
        add (b0);
        add (bp);
        add (bclear);
    }   
}   

1 个答案:

答案 0 :(得分:1)

尝试运行此功能,看看它是否与您要查找的内容类似。

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TelephonePanel extends JPanel {

String numberString = "";

JTextField jtf = new JTextField();
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton ba = new JButton("*");
JButton b0 = new JButton("0");
JButton bp = new JButton("#");
JButton bclear = new JButton("Clear");

public TelephonePanel() {

    JPanel panel1 = new JPanel(new GridLayout(4, 3));

    panel1.add(b1);
    panel1.add(b2);
    panel1.add(b3);
    panel1.add(b4);
    panel1.add(b5);
    panel1.add(b6);
    panel1.add(b7);
    panel1.add(b8);
    panel1.add(b9);
    panel1.add(ba);
    panel1.add(b0);
    panel1.add(bp);

    ButtonListener listener = new ButtonListener();

    b1.addActionListener(listener);

    // add listener to all buttons

    setLayout(new BorderLayout());
    add(panel1, BorderLayout.CENTER);
    add(bclear, BorderLayout.SOUTH);
    add(jtf, BorderLayout.NORTH);

    jtf.setHorizontalAlignment(SwingConstants.RIGHT);
    jtf.setPreferredSize(new Dimension(300, 30));

}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            JFrame frame = new JFrame();
            frame.add(new TelephonePanel());
            frame.setSize(300, 400);
            frame.setVisible(true);

        }
    });
}

class ButtonListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b1) {
            numberString += "1";
            jtf.setText(numberString);
        } else if (e.getSource() == b2) {
            numberString += "2";
            jtf.setText(numberString);
        }

        // finish all the else ifs
    }

}
}