java - 由于某种原因,我的 JPanel 没有显示

标签 java

这是我的代码

import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
public class Test extends JFrame{
    private JButton by;
    private JButton bn;
    JLabel startQuestion;

    public Test() {
        JPanel p = new JPanel();
        by = new JButton("Yes");
        bn = new JButton("No");
        startQuestion = new JLabel("Would you like to start?");
        p.setLayout(null);
        by.setBounds(180, 250, 70, 45);
        bn.setBounds(250, 250, 70, 45);
        startQuestion.setLocation(195, 240);
        p.add(by);
        p.add(bn);
        add(startQuestion);
        add(p);
        setDefaultCloseOperation(3);
        setSize(500, 500);
        setVisible(true);
    }

    public static void main(String...args) {
        new Test();
    }
}

没有语法错误,但是我的 Jlabel 没有添加(按钮工作正常)。我不想将布局从 null 更改,因为我希望能够定义按钮和标签的位置。有谁知道为什么这不起作用?

最佳答案

这个问题类似于JLabel won't show with JPanel.setLayout(null). Why?

要在这里回答这个问题,请添加如下行:

    startQuestion.setBounds(150, 150, 200, 20);

关于java - 由于某种原因,我的 JPanel 没有显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33084089/

相关文章:

Java - 递归

java - 如何解决 SSLHandshake 异常?

java - 在java中哪种方式更好?

java - Swing:从另一个类获取组件的父级

java.awt.image.BufferedImage.getRBG 未返回预期值

java - 接收 webElement 列表 - 转换错误

java - Android - Appium 向下滑动不工作

javascript - 使用 Vaadin 将 Google map API key 实现到 Java 项目中

java - Gson 反序列化为 Map 通用类型

java - 选择 JCheckBox 时添加到变量