gpt4 book ai didi

java - JPanel 上的组件不显示

转载 作者:行者123 更新时间:2023-11-30 04:25:33 24 4
gpt4 key购买 nike

我创建了一个 JPanel 并对其进行了一些修改。我将其背景更改为渐变颜色,这是该类。

public class JGradientPanel extends JPanel {
private static final int N = 32;
private Color color1, color2;
public JGradientPanel(Color color1, Color color2) {
this.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));

this.color1 = color1;
this.color2 = color2;
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//Color color1 = getBackground();
//Color color2 = color1.darker();
int w = getWidth();
int h = getHeight();
GradientPaint gp = new GradientPaint(
0, 0, color1, 0, h, color2);
g2d.setPaint(gp);
g2d.fillRect(0, 0, w, h);
}
}

现在我向面板添加了一些组件,但它没有显示任何内容,这是代码

public JPanel getMenu() {
JGradientPanel menu = new JGradientPanel(Color.WHITE, Color.white.darker());
int menuHeight = (int) ((int) getHeight() * 0.07);
menu.setPreferredSize(new Dimension(screenWidth,menuHeight));
menu.setLayout(new GridLayout(1,10));
menu.setBackground(Color.LIGHT_GRAY);

//test
JGradientButton test = new JGradientButton("test",Color.GREEN, Color.BLUE);
menu.add(test);

JLabel space = new JLabel(); // first blank space on the menu
space.setBounds(0, 0, menu.getPreferredSize().width - 50, menu.getPreferredSize().height);
space.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.GREEN));
menu.add(space);
JLabel moreSpaces[] = new JLabel[6];
buttons = new JButton[buttonLabels.length];
for(int counter = 0; counter < moreSpaces.length + buttonLabels.length; counter ++ ) {
if(counter < 3) {
buttons[counter] = new JButton(buttonLabels[counter]); //menu buttons

} else {
moreSpaces[counter - 3] = new JLabel(); // the rest of the blank in the menu
}
}
// adding components to menu panel
for(int counter = 0; counter < moreSpaces.length + buttonLabels.length; counter ++){
if(counter < 3) {
buttons[counter].setFocusPainted(false);
menu.add(buttons[counter]);
} else {
menu.add(moreSpaces[counter - 3]);
}

}
return menu;
}

我是错过了什么还是做错了什么?我的代码有什么问题吗?

最佳答案

编辑

关于java - JPanel 上的组件不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15952439/

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