![](/img/trans.png)
[英]How to add a button in the tab bar of JTabbedPane, that is not a tab?
[英]How to add a new tab to JTabbedPane with a JTextArea
我想在单击新选项时在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);
现在,文本区域将填充选项卡式窗格中的可用空间。
如果您想要的只是选项卡上的文本区域,则您不需要JPanel。 只需将新文本区域添加到选项卡式窗格即可。
JPanel jp1 = new JPanel();
JTextArea ta1 = new JTextArea();
ta1.setBounds(10, 10,100 , 100);
jp1.add(ta1);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jp1);
jp1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(ta1, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE));
jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(ta1, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE));
jTabbedPane1.addTab("untitled",jp1);
//这是我的问题的正确答案,这是在选项卡式窗格中传递带有面板的文本区域的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.