gpt4 book ai didi

java - 避免 JToolBar 和 JTabbedPane 之间存在空格

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

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

这里是代码:

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
}

最佳答案

删除行 .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();
}

关于java - 避免 JToolBar 和 JTabbedPane 之间存在空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24383848/

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