0

我只是有这个问题:

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

其中 Sales 只是添加到一个简单的 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);
    }

一切正常,除了组件在由于单击或其他原因更新窗口之前不会绘制。所以它是一个空白屏幕,直到你点击东西。(对不起,我在让它全屏的事情上剪了一些角落......)

4

1 回答 1

2

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

尝试类似...

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

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

边注

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

于 2013-02-20T23:51:16.883 回答