避免JToolBar和JTabbedPane之间的空间

时间:2014-06-24 10:07:23

标签: java swing awt layout-manager

我正在创建一个JFrame示例。在此过程中,首先我选择了框架,然后添加JMenuBar,然后添加JToolbar,然后添加JTextPane。在菜单栏中,我添加了File Menu,然后我将create MenuItem添加到文件菜单中。当我点击创建菜单项时,打开一个内部框架作为普通文档。但是,工具栏和选项卡式窗格之间会显示巨大的空间。如何避免这个空间?

这是代码:

public class CreateDoc extends javax.swing.JFrame {
JScrollPane scrollPane;
JTextPane textPane;
int i=0;
public CreateDoc() {
    initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    createToolBar = new javax.swing.JToolBar();
    Help = new javax.swing.JButton();
    tabbedPane = new javax.swing.JTabbedPane();
    createMenuBar = new javax.swing.JMenuBar();
    createMenu = new javax.swing.JMenu();
    create = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    createToolBar.setRollover(true);

    Help.setIcon(new javax.swing.ImageIcon(getClass().getResource("/about.png"))); // NOI18N
    Help.setToolTipText(create.getText());
    Help.setFocusable(false);
    Help.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    Help.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    createToolBar.add(Help);

    createMenu.setText("File");

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

    createMenuBar.add(createMenu);

    setJMenuBar(createMenuBar);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(createToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(createToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 254, Short.MAX_VALUE))
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 22, Short.MAX_VALUE)
                .addComponent(tabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );

    pack();
}// </editor-fold>                        

private void createActionPerformed(java.awt.event.ActionEvent evt) {                                       
    final JInternalFrame internalFrame = new JInternalFrame("");
    i++;
    internalFrame.setName("Document"+i);
    internalFrame.setClosable(true);
    internalFrame.setAutoscrolls(true);
    textPane=new JTextPane();
    textPane.setFont(new java.awt.Font("Miriam Fixed", 0, 14));
    scrollPane=new JScrollPane(textPane);
        internalFrame.add(scrollPane);
        tabbedPane.add(internalFrame);
        internalFrame.setSize(internalFrame.getMaximumSize());
        internalFrame.pack();
        internalFrame.setVisible(true);
}                                      

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    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(CreateDoc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(CreateDoc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(CreateDoc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(CreateDoc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new CreateDoc().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JButton Help;
private javax.swing.JMenuItem create;
private javax.swing.JMenu createMenu;
private javax.swing.JMenuBar createMenuBar;
private javax.swing.JToolBar createToolBar;
private javax.swing.JTabbedPane tabbedPane;
// End of variables declaration                   
}

4 个答案:

答案 0 :(得分:1)

使用.addGap(0, 22, Short.MAX_VALUE)删除行.addGap(0, 0, Short.MAX_VALUE)。这会解决您的问题吗?

编辑: 评论考虑在内。我已经改变了一点使用的布局,以便它可以正常工作。

private void initComponents() {

    createToolBar = new javax.swing.JToolBar();
    Help = new javax.swing.JButton();
    tabbedPane = new javax.swing.JTabbedPane();
    createMenuBar = new javax.swing.JMenuBar();
    createMenu = new javax.swing.JMenu();
    create = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    createToolBar.setRollover(true);
// Commented for testing only - pls uncomment
//      Help.setIcon(new javax.swing.ImageIcon(getClass().getResource(
//              "/about.png"))); // NOI18N
        Help.setToolTipText(create.getText());
        Help.setFocusable(false);
        Help.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        Help.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    createToolBar.add(Help);

    createMenu.setText("File");

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

    createMenuBar.add(createMenu);

    setJMenuBar(createMenuBar);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
            getContentPane());
    layout.setHorizontalGroup(
        layout.createParallelGroup(Alignment.LEADING)
            .addComponent(createToolBar, GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
            .addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(createToolBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGap(2)
                .addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
    );
    getContentPane().setLayout(layout);

    pack();
}

答案 1 :(得分:0)

我已经执行了您的代码并找到了您正在寻找的解决方案。 在您的代码中:

layout.setVerticalGroup(
...

在第二个addGroup选项中,只需更改代码addGap(0, 22, Short.MAX_VALUE)中的值22即可 减少值22将帮助您缩小选项卡式窗格和菜单栏之间的差距。

感谢。

答案 2 :(得分:0)

您不应该在setVerticalGroup(),

中向tabpane添加间隙

修改后的代码:

layout.setVerticalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addComponent(createToolBar,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 254, Short.MAX_VALUE))
                .addGroup(
                        layout.createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(
                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup()
                                                .addGap(0, 0, Short.MAX_VALUE) // Do not add gap here or set as minimum**
                                                .addComponent(
                                                        tabbedPane,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        257,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))));

        pack();

enter image description here

答案 3 :(得分:0)

修改组布局,如下所示:

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
                .addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
                .addComponent(createToolBar, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(createToolBar, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE))
        );