gpt4 book ai didi

java - 如何将 JTextArea 添加到 Closable TabbedPane

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

我想将带有文本区域的滚动 Pane 添加到选项卡。我为此编写了代码。但是,它显示了一个小文本区域而不是整个框架。我在 createAction() 方法中编写了此代码。我想要带有关闭按钮的选项卡并添加滚动 Pane 到 tabbedpane。请检查一次。谢谢。

我的代码:

public class ClosableTabbedpane extends javax.swing.JFrame {
JTextArea tx;
int i=0;
public ClosableTabbedpane() {

initComponents();

}

private static JPanel getTitlePanel(final JTabbedPane tabbedPane, final JPanel panel, String title) {
JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
titlePanel.setOpaque(false);
JLabel titleLbl = new JLabel(title);
titleLbl.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
titlePanel.add(titleLbl);
JButton closeButton = new JButton("x");

closeButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
tabbedPane.remove(panel);
}
});
titlePanel.add(closeButton);
return titlePanel;
}

@SuppressWarnings("unchecked")
private void initComponents() {

tabbedPane = new javax.swing.JTabbedPane();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
create = new javax.swing.JMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jMenu1.setText("File");

create.setText("Create");
create.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
createActionPerformed(evt);
}
});
jMenu1.add(create);

jMenuBar1.add(jMenu1);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
);

pack();
}

private void createActionPerformed(java.awt.event.ActionEvent evt) {
i++;
tx = new JTextArea();
JPanel panel = new JPanel();
panel.setBounds(400,400,400,400);
tx.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
JScrollPane scrollpane=new JScrollPane(tx);
panel.add(scrollpane);
panel.setOpaque(false);
tabbedPane.add(panel);
tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel), getTitlePanel(tabbedPane, panel, "Doc"+i));
}

public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ClosableTabbedpane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ClosableTabbedpane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ClosableTabbedpane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ClosableTabbedpane.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ClosableTabbedpane().setVisible(true);
}
});
}
private javax.swing.JMenuItem create;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem open;
private javax.swing.JTabbedPane tabbedPane;
}

最佳答案

要建议文本区域的大小,请在创建时指定所需的行数和列数。例如

tx = new JTextArea(3,40);

关于java - 如何将 JTextArea 添加到 Closable TabbedPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27033205/

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