摆动组件未按预期显示

时间:2016-06-20 23:24:14

标签: java swing jframe

下面课程的目的是在每个JFrame标签中创建一个Applet JTabbedPane。方法loadPanel()返回Box组件(假定的"加载屏幕"用于显示Applet加载时),而loadGame()方法加载并返回Applet

但是,当load()被调用时,JFrame会显示,但JTabbedPaneApplet加载之后才会显示。我一直绞尽脑汁,不知道这里有什么问题,任何建议都值得赞赏。

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;

@SuppressWarnings("serial")
public class Loaders extends JFrame {

    int dimension;
    int osClients;
    int rs3Clients;

    public Loaders(int osClients, int rs3Clients) {
        this.osClients = osClients;
        this.rs3Clients = rs3Clients;
        this.dimension = (int) Math.sqrt(osClients + rs3Clients);
    }

    public void load() {

        Loader[] os = new Loader[osClients];
        Loader[] rs3 = new Loader[rs3Clients];

        setTitle("DENCH CLIENTS");
        setSize(768, 528);
        setResizable(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBackground(new Color(0x2B2B2B));
        JTabbedPane tabs = new JTabbedPane();

        for(int i = 0; i < osClients; i++) {
            os[i] = new Loader(Game.OSRS, i+1);
            tabs.add("OS " + i, os[i].loadPanel());
        }

        for(int i = 0; i < rs3Clients; i++) {
            rs3[i] = new Loader(Game.RS3, i+1);
            tabs.add("RS3 " + i, rs3[i].loadPanel());
        }
        add(tabs);
        tabs.setVisible(true);
        setVisible(true);

        for(Loader l : os) {
            System.out.println(l.getWorld());
            int index = tabs.indexOfTabComponent(l.getBox());
            tabs.setComponentAt(index, l.loadGame());
        }

        for(Loader l : rs3) {
            int index = tabs.indexOfTabComponent(l.getBox());
            tabs.setComponentAt(index, l.loadGame());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  • setVisible移至方法的末尾
  • 无需tab.setVisible(true) 由于您正在更新选项卡面板,因此可以在swing调度线程中调用它,如下所示:

    SwingUtilities.invokeLater(new Runnable(){
        @Override
        public void run() {
            for(Loader l : os) {
                System.out.println(l.getWorld());
                int index = tabs.indexOfTabComponent(l.getBox());
                tabs.setComponentAt(index, l.loadGame());
            }
    
            for(Loader l : rs3) {
                int index = tabs.indexOfTabComponent(l.getBox());
                tabs.setComponentAt(index, l.loadGame());
            }
        }                           
    });
    tabs.setVisible(true);
    setVisible(true);
    }