gpt4 book ai didi

java - 我正在尝试创建一个数字键盘,它有一个清晰的按钮,顶部有一个显示单击的数字的部分。到目前为止我已经

转载 作者:太空宇宙 更新时间:2023-11-04 07:14:19 28 4
gpt4 key购买 nike

我很难弄清楚如何使清除按钮延伸到整个面板。我也很难找到如何创建面板上方显示用户单击的键的部分。请帮忙?这是我到目前为止所做的...我尝试更改清除按钮的大小,但该部分代码没有执行任何操作。

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

最佳答案

尝试运行它,看看它是否是您正在寻找的东西。

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
}

}
}

关于java - 我正在尝试创建一个数字键盘,它有一个清晰的按钮,顶部有一个显示单击的数字的部分。到目前为止我已经,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20204437/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com