0

作为我的软件开发文凭的一部分,我们必须创建一个 Java GUI 来管理足球联赛。我的小组已经制作了许多 JPanel,它们在我们放入 JTabbedPane 的点击原型中。到目前为止,这一直很好,我将它们移动到单独的文件和 MVC 布局中。

我正在使用一个窗口类来保存顶级 JFrame 和菜单栏,并在其中添加选项卡式窗格。这在构造函数中工作正常,但是当我从构造函数中删除它们并尝试通过Window.attachTabbedPanel(String name, JPanel panel)它从引导程序中添加它们时,它不会显示,但查询会tabbedPane.getTabCount()显示递增数量的选项卡。

这是一组精简的代码:Bootstrap 文件:

public class Bootstrap {

    private Window mainWindow;

    public Bootstrap() {

        //Window class is our containing JFrame and JMenuBar
        mainWindow = new Window();

        //Load up our view classes
        TestTab tab1 = new TestTab();
        TestTab tab2 = new TestTab();

        //Attach them
        mainWindow.attachTabbedPanel("Tab1", tab1.getScreen());
        mainWindow.attachTabbedPanel("Tab2", tab2.getScreen());

    } // Bootstrap()

    public gui.Window getWindow(){
        return mainWindow;
    }

} // Bootstrap

这由主文件调用:

public class Main {

    public static void main(String[] args) {

        Bootstrap RunMVC = new Bootstrap();
        gui.Window mainWindow = RunMVC.getWindow();
        mainWindow.run();
    } // main()

} // Main

问题从 Window 类开始,我在选项卡中添加了一个In Constructor选项卡以检查我没有填满 tabbedPane,但此时它工作正常。

public class Window {

    private JFrame frame;
    private JMenuBar menuBarMain;
    private JMenu mnFile;
    private JTabbedPane tabbedPane;
    private int count;
    /**
     * Create the application.
     */
    public Window() {

        //Build the frame
        frame = new JFrame();
        frame.setBounds(100, 100, 1280, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new CardLayout(0, 0));
        //End Build frame

        TestTab conTab = new TestTab();

        //Add the tabbed pane to hold the top level screens
        tabbedPane = new JTabbedPane(JTabbedPane.TOP);    
        frame.getContentPane().add(tabbedPane, "name_1");
        tabbedPane.addTab("In Consructor", conTab.getScreen());
        count = 1;
    }

    public void attachTabbedPanel(String name, JPanel panel){
        System.out.println("Window: adding Jpanel name: "+name);
        System.out.println("panel is a: "+panel);
        tabbedPane.addTab(name, panel);
        tabbedPane.updateUI();
        System.out.println("Number of tabs: "+tabbedPane.getTabCount());
        System.out.println("Last Tab .isEnabledAt() "+tabbedPane.isEnabledAt(count++));
        tabbedPane.updateUI();
    }
    /**
     * Launch the window.
     */
    public void run() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window window = new Window();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

最后是小组:

public class TestTab {

    private JPanel screen;
    private JLabel lblSeason;
    private JButton btnEdit;
    private JLabel lblRounds;

    public JPanel getScreen() {
        return screen;
    }
    /**
     * Initialize the contents of the frame.
     */
    public TestTab() {

        screen = new JPanel();
        screen.setLayout(new MigLayout("", "[8%,right][10%,left][8%,right][10%,left][grow][50%]", "[][][grow]"));

        lblSeason = new JLabel("Test");
        screen.add(lblSeason, "flowx,cell 0 0");

        btnEdit = new JButton("Edit Test");
        screen.add(btnEdit, "cell 5 0,alignx right");

        lblRounds = new JLabel("More Testing");
        screen.add(lblRounds, "cell 0 1,alignx left");

    }

}
4

2 回答 2

0

updateUI()不做你认为它做的事。永远不应该直接调用它——它是从超类构造函数中调用的,以允许 Swing 外观委托初始化自身。

消除呼叫updateUI()可能会解决您的问题;或者如果选项卡式窗格已经在屏幕上,您可能需要强制重新绘制/重新验证 - 咒语是invalidate(); revalidate(); repaint();.

于 2013-10-22T07:13:56.733 回答
0

你的错误在这里:

public void run() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window window = new Window();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

您正在使用新的窗口实例而不是使用之前创建的,请尝试使用此代码

 public void run() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
于 2013-10-22T07:18:51.793 回答