使用JScrollPane和JPanel创建可滚动的JFrame

时间:2013-12-25 18:52:58

标签: java swing user-interface jframe

在寻找解决方案时,我在StackOverflow的所有答案中找到了几乎相同的想法。但是所有这些都没有用,我需要帮助我找到代码特殊部分的解决方案。

//Create a JTabbedPane for 2 tabs
mainTabs = new JTabbedPane(JTabbedPane.TOP);

//Create the first tab
reportingTabs = new JTabbedPane(JTabbedPane.TOP);

// editor is an object created from a class inherited form JPanel
editor = new GraphEditor();

//Create a JMenuBar
EditorMenuBar menuBar = new EditorMenuBar(editor);

//Create a JFrame for the editor
editorFrame = editor.createFrame();


//Create a JPanel object to contain bothe the JMenubar and the editor JFrame
JPanel editorPanel = new JPanel(new BorderLayout());

//Here the solution, creating a JScrollPane to contain only the editor JFrame to be scrolled
JScrollPane editorScroll = new JScrollPane();

//Adding the JMenuBar and the editor JFrame to the JPanel
editorPanel.add(menuBar, BorderLayout.NORTH);
editorPanel.add(editorFrame.getContentPane());

//Involve the JPanel into the JScrollPane
editorScroll.add(editorPanel);


//Adding the tabs to the main JFrame
maingui.getContentPane().add(mainTabs);

//Adding the JScrolledPane to a tab
mainTabs.addTab("Editor", editorScroll);

结果是,thers在maingui中不是JFrame。 (没有SCrollPane解决方案,它正确地适用)

1 个答案:

答案 0 :(得分:4)

完全删除editorScroll变量并替换

     mainTabs.addTab("Editor", editorScroll); 

     mainTabs.addTab("Editor", new JScrollPane(editorPanel));

它应该有用......