如何将JTextArea添加到Closable TabbedPane

时间:2014-11-20 06:31:10

标签: java swing layout-manager jtextarea jtabbedpane

我想将带有Textarea的Scrollpane添加到Tabbedpane.I为此编写了代码。但是,它显示了一个小的textarea而不是整个框架。我在createAction()方法中编写了这个代码。我希望tabbedpane带有关闭按钮并添加Scrollpane to 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;               
}

1 个答案:

答案 0 :(得分:1)

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

tx = new JTextArea(3,40);