我想在单击新选项时在 tabbedpane
中创建新选项卡。但新选项卡应包括带有文本区域的面板,其位置与我通过在 netbeans 中拖放创建的第一个选项卡相同。我创建一个选项卡,并希望该选项卡的新实例与其他选项卡一样,我该怎么做?
我正在创建一个记事本应用程序,我想在 tabbedpane
中以选项卡的形式添加新文件的功能。我通过在 netbeans 中拖放创建了一个选项卡。但我不知道单击新选项时如何在新选项卡中使用此实例。
//This is the code to create new panel but it is not working
int i= 1;
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt)
{
JPanel jp = new JPanel();
jp = jPanel1; // jPanel1 is the panel created by drag and drop.
// i cantains text area.
jTabbedPane1.addTab("untitled"+i,jp);
i++;
}
我希望应用程序创建新选项卡,就像在 netbeans 或 dev 等中创建的选项卡一样。
最佳答案
jp = jPanel1; // jPanel1 is the panel created by drag and drop.
您无法共享组件。 Swing 组件只能有一个父组件。
因此您需要创建一个新的 JPanel 实例和一个新的 JTextArea 实例。
JTextArea textArea = new JTextArea(5, 20);
JPanel panel = new JPanel( new BorderLaout() );
panel.add( textArea );
jTabbedPane1.addTab("untitled"+i, panel);
现在文本区域将填充选项卡式 Pane 中的可用空间。
如果您想要的只是选项卡上的文本区域,则根本不需要 JPanel。只需将新文本区域添加到选项卡式 Pane 即可。
关于java - 如何使用 JTextArea 将新选项卡添加到 JTabbedPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56136129/