没有鼠标单击,JPanel组件不会更新

时间:2013-02-20 23:47:35

标签: java swing jtabbedpane

我刚遇到这个问题:

public class Sales extends JPanel{
    ArrayList<JPanel> panes;
    ArrayList<String> tabs;
    JTabbedPane tp;
    public Sales(Dimension d){
        setSize(d);
        setLayout(null);
        tp = new JTabbedPane();
        Font f = new Font("Arial",Font.PLAIN,32);
        tp.setFont(f);
        for(Menu menu : FileManager.menus){
            JPanel tmp = new JPanel();
            /*int s = (int) Math.ceil(Math.sqrt(menu.products.size()));
            tmp.setLayout(new GridLayout(s,s));
            System.out.println("size" + s);
            for(Product p : menu.products){
                p.setFont(f);
                tmp.add(p);
            }*/
            tp.addTab(menu.name,null,tmp,"What is this?");
        }
        tp.setBounds(0,0,getWidth(),getHeight());
        add(tp);
    }
}

销售额刚刚添加到简单的JFrame中:

public Main(){
        super("HoboGames Pos System");
        setUndecorated(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
        sale = new Sales(getSize());
        add(sale);
    }

一切正常,除非组件在窗口因点击或其他内容而更新之前不会绘制。所以这是一个空白的屏幕,直到你点击东西。(对不起,我切了一些角落,比如全屏......)

1 个答案:

答案 0 :(得分:2)

它们不会更新,因为您在完成向其添加组件之前已使窗口可见。

尝试类似......

GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
sale = new Sales(getSize());
add(sale);
setVisible(true);

或者,您可以在添加完组件后在框架上调用revalidaterepaint,但说实话,第一种方式更简单。

旁注

强烈建议不要在组件上使用setSize,您应该依靠适当的布局管理,例如BorderLayout来决定组件的大小。

相关问题