使用 JScrollPane 和 JPanel 创建可滚动的 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);

结果,在maingui中没有JFrame。(没有SCrollPane解决方案,它正确应用)

完全删除编辑器滚动变量并替换

     mainTabs.addTab("Editor", editorScroll); 

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

它应该工作...

最新更新