JPanel上的组件未显示 [英] Components on the JPanel doesn't show

查看:150
本文介绍了JPanel上的组件未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a JPanel and i modify it a bit. I change it's background to gradient color here is the class.

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);
        }
}

现在我在面板上添加了一些组件,但是它什么也没显示,这是代码

Now i added few components to the panel but it didn't display anything, here is the code

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;
    }

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

Did i miss something or i did it wrong? What's wrong with my code?

推荐答案

  • 覆盖public class JGradientPanel extends JPanel {

    Swing绘画作者:public void paintComponent(Graphics g) {作者:默认从不返回 JPanel的PreferredSize

    Painting in Swing by public void paintComponent(Graphics g) { by default never to returns PreferredSize to JPanel,

    JPanel返回Dimension[0, 0];

    编辑

    这篇关于JPanel上的组件未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆