gpt4 book ai didi

java - JFrame GUI 上未显示任何组件

转载 作者:行者123 更新时间:2023-12-02 04:20:56 24 4
gpt4 key购买 nike

我对 Swing 和 GUI 还很陌生,到目前为止,只出现窗口,但没有任何组件可见。对此我能做什么?是可见性有问题还是容器有问题?

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

public class PhoneCaller
{
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JButton buttonDash;
JButton button0;
JButton dialButton;

String phoneNum = "";

public static void main (String[] args)
{
new PhoneCaller();
}

public PhoneCaller()
{
JFrame myFrame = new JFrame();
myFrame.setTitle("Dialer");
myFrame.setSize(200, 250);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


JPanel myPanel = new JPanel(new BorderLayout(10,10));
myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
myFrame.add(myPanel);


JPanel myPanel2 = new JPanel();
myPanel2.setLayout(new FlowLayout());
JLabel lab = new JLabel("Enter the number to dial");
myPanel2.add(lab);

JPanel myPanel3 = new JPanel();
myPanel3.setLayout(new GridLayout(4,3,5,5));
button1 = new JButton ("1");
myPanel3.add(button1);
button2 = new JButton ("2");
myPanel3.add(button2);
button3 = new JButton ("3");
myPanel3.add(button3);
button4 = new JButton ("4");
button5 = new JButton ("5");
button6 = new JButton ("6");
button7 = new JButton ("7");
button8 = new JButton ("8");
button9 = new JButton ("9");
button0 = new JButton ("0");
buttonDash = new JButton ("-");

myPanel3.add(button4);
myPanel3.add(button5);
myPanel3.add(button6);
myPanel3.add(button7);
myPanel3.add(button8);
myPanel3.add(button9);
myPanel3.add(button0);
myPanel3.add(buttonDash);

myFrame.setVisible(true);
}
}

最佳答案

我认为您忘记在 myPanel 内添加 myPanel2myPanel3

myPanel.add(myPanel2);
myPanel.add(myPanel3);

关于java - JFrame GUI 上未显示任何组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32771912/

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