简体   繁体   English

JButton没有出现在JPanel上

[英]JButton not appearing on JPanel

I have been trying to learn how to use layout manages in java, I have the following code that trys to add the class "StartButton", startbutton is just a class that extends JButton 我一直在努力学习如何在java中使用布局管理,我有以下代码试图添加类“StartButton”,startbutton只是一个扩展JButton的类

When I run the code my JFrame is loaded properly and the JPanel is created, I also added a print to layoutItems() to insure that the function is being called. 当我运行代码时,我的JFrame正确加载并创建了JPanel,我还向layoutItems()添加了一个print,以确保调用该函数。

This is my JPannel code; 这是我的JPannel代码;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JPanel;

public class menuPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    private static final Color BG_COLOR = new Color(0xfaf8ef);


    private startButton startbutton;

    public menuPanel(){
        this.setOpaque(false);
        this.setPreferredSize(new Dimension(300, 400));
        this.setBackground(new Color(107, 106, 104));
        this.setVisible(true);
        setLayout(new GridBagLayout());

        startbutton = new startButton();

        layoutItems();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(BG_COLOR);
        g.fillRect(0, 0, this.getSize().width, this.getSize().height);
    }


    public void layoutItems(){
        GridBagConstraints c = new GridBagConstraints();
        System.out.println("lol");
        c.weightx = 0.25;
        c.weighty = 0.175;
        c.gridwidth = 2;
        c.gridx = 1;
        c.gridy = 6;
        this.add(startbutton, c);
    }
}

Ignore the recommendation to call pack() on the class above as it does not work for JPanels. 忽略在上面的类上调用pack()的建议,因为它不适用于JPanels。 Instead, don't set your JPanel's size or preferred size -- let the components themselves and your layout managers do that. 相反,不要设置JPanel的大小或首选大小 - 让组件本身和布局管理器执行此操作。 Then add all items to the JFrame, call pack() on it after adding all items, and then call setVisible(true) on it, in that order. 然后将所有项添加到JFrame,在添加所有项后调用pack() ,然后按顺序调用setVisible(true)

Oh, you will want to change your startbutton class name to S tart B utton since Java class names should begin with an upper case letter, and you should use camel-case names for all identifiers except constants. 哦,你需要将你的startbutton类名更改为S tart B utton,因为Java类名应该以大写字母开头,并且你应该使用camel-case名称来表示除常量之外的所有标识符。

If you're still stuck after trying out these recommendations, then consider creating and posting your minimal code example program for us to review, test, and possibly fix. 如果您在尝试这些建议后仍然困难,那么请考虑创建并发布您的最小代码示例程序 ,以供我们审核,测试和修复。

Overriding paint rather than paintComponent results in unpredictable behavior in the way that components are painted for any Swing component. 覆盖paint而不是paintComponent导致在为任何Swing组件绘制组件的方式中出现不可预测的行为。

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    ...
}

In fact overriding paintComponent isnt necessary as setBackground(BG_COLOR) will give the equivalent result 事实上,覆盖paintComponent是不必要的,因为setBackground(BG_COLOR)将给出相同的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM