gpt4 book ai didi

java - 单击按钮时如何动态添加带有文本区域的新选项卡?

转载 作者:行者123 更新时间:2023-12-01 12:55:41 26 4
gpt4 key购买 nike

作为我自己的一个项目,我决定创建一个文本编辑器,并且根据有关代码结构和用户设计的一些建议,我放弃了当用户单击“新建”菜单项时创建编辑器的新实例。相反,我决定使用 JTabbedPane 来使用“选项卡”。我可以设置第一个选项卡,但我似乎无法弄清楚如何在已有选项卡旁边动态创建一个新选项卡。有没有人有 JTabbedPane 的经验或者对我如何实现它有任何想法。

代码如下:

public class Editor extends JFrame implements ActionListener {

//===============================================
// FIELDS
//===============================================

// Menus
private JMenu fileMenu, editMenu;

// Menu Items
private JMenuItem newFile, openFile, saveFile, saveAsFile, pageSetup, printFile, exit;
private JMenuItem undo, redo, cut, copy, paste, selectAll;

// Menu Bar
private JMenuBar menuBar;

// New tab
private JTabbedPane tab;
private NewTab newTab;

// Text Area
private JScrollPane scroll;
private JTextArea textArea;

//===============================================
// CONSTRUCTOR
//===============================================

public Editor() {
super("SchongeEdit");

// Set initial size of the window
setSize(800, 600);
setLocationRelativeTo(null);

// Set the default close operation (exit when it gets closed)
setDefaultCloseOperation(EXIT_ON_CLOSE);

getContentPane().setLayout(new BorderLayout(0, 5));
newTab = new NewTab();
tab = newTab.createTab(createTextArea());
getContentPane().add(tab);

add(createMenuBar(), BorderLayout.NORTH);
}

//===============================================
// METHODS
//===============================================

// Creates the menu bar for holding the menus
private JMenuBar createMenuBar() {
menuBar = new JMenuBar();
menuBar.add(fileMenu());
menuBar.add(editMenu());

return menuBar;
}

// Creates the text area in a scroll pane
private JScrollPane createTextArea() {
// Set text area and default font
textArea = new JTextArea("", 0, 0);
textArea.setEditable(true);
textArea.setBorder(BorderFactory.createEmptyBorder(5, 10, 0, 0));
textArea.setFont(new Font("Verdana", Font.PLAIN, 14));
scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

return scroll;
}
}

我省略了 fileMenu()editMenu() 方法,因为它们不是必需的,只会堵塞空间

这是我的 NewTab() 类:

public class NewTab {

// Tabs
private JTabbedPane tab;
private String newTabName = "New Tab";

// Creates a new tabbed pane with a text area
protected JTabbedPane createTab(JScrollPane scroll) {
tab = new JTabbedPane(JTabbedPane.TOP);
tab.addTab(newTabName, scroll);

return tab;
}

}

最佳答案

您的基本问题是,您正在向现有的 JTabbedPane 添加新的 JTabbedPane

您不应让 NewTab 返回新的 JTabbedPane,而应让它返回充当选项卡 View 和选项卡名称的组件,例如...

public class NewTab {

private String tabName = "New Tab";

// Creates a new tabbed pane with a text area
protected JComponent createTab() {
JTextArea ta = new JTextArea(10, 20);
JScrollPane sp = new JScrollPane(ta);

return ta;
}

public String getTabName() {
// Could be used to hold the file name, for example
return tabName;
}

}

然后您可以使用它将 View 添加到现有的 JTabbedPane

关于java - 单击按钮时如何动态添加带有文本区域的新选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23922276/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com