簡體   English   中英

出於某種原因,我的 JPanel 沒有出現

[英]For some reason, my JPanel is not showing up

這是我的代碼

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)。 為什么?

要在這里回答它,請添加如下一行:

    startQuestion.setBounds(150, 150, 200, 20);
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();
    }
}

筆記:

你需要評論setlayout(null);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM