gpt4 book ai didi

Java (swing) 工具栏未显示

转载 作者:行者123 更新时间:2023-12-01 11:03:56 25 4
gpt4 key购买 nike

我一直在关注本教程:
Java toolbar tutorial
但是,当我尝试将工具栏插入 GUI 时,它没有显示。有人可以给我建议该怎么做吗?到目前为止的代码是这样的:

import javax.swing.Box;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.AbstractAction;
import static javax.swing.Action.MNEMONIC_KEY;
import static javax.swing.Action.SMALL_ICON;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.KeyStroke;
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BorderFactory;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JLabel;
import javax.swing.JToolBar;

public class gui extends JFrame {

private JLabel statusbar;

public gui() {

initUI();
}

private void initUI() {

createMenuBar();
createToolBar();

statusbar = new JLabel("Ready");
statusbar.setBorder

(BorderFactory.createEtchedBorder());
add(statusbar, BorderLayout.SOUTH);

JButton quitButton = new JButton("Quit");
quitButton.setToolTipText("A button component");

quitButton.addActionListener(new ActionListener()

{
@Override
public void actionPerformed(ActionEvent event)

{
System.exit(0);
}
});

quitButton.setMnemonic(KeyEvent.VK_B);

createLayout(quitButton);

setTitle("Typing Tutor");
setSize(360, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private void createMenuBar() {

JMenuBar menubar = new JMenuBar();

ImageIcon iconNew = new ImageIcon("new.png");
ImageIcon iconOpen = new ImageIcon("open.png");
ImageIcon iconSave = new ImageIcon("save.png");
ImageIcon iconExit = new ImageIcon("exit.png");

JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);

// Old code

JMenu impMenu = new JMenu("Import");
fileMenu.setMnemonic(KeyEvent.VK_I);

JMenuItem newsfMi = new JMenuItem("Import newsfeed list...");
JMenuItem bookmMi = new JMenuItem("Import bookmarks...");
JMenuItem mailMi = new JMenuItem("Import mail...");

impMenu.add(newsfMi);
impMenu.add(bookmMi);
impMenu.add(mailMi);

JMenuItem newMi = new JMenuItem("New", iconNew);
JMenuItem openMi = new JMenuItem("Open", iconOpen);
JMenuItem saveMi = new JMenuItem("Save", iconSave);

JMenuItem exitMi = new JMenuItem("Exit", iconExit);
exitMi.setMnemonic(KeyEvent.VK_E);
exitMi.setToolTipText("Exit application");
exitMi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event)

{
System.exit(0);
}
});

// New code

JMenu viewMenu = new JMenu("View");
viewMenu.setMnemonic(KeyEvent.VK_V);

JCheckBoxMenuItem sbarMi = new JCheckBoxMenuItem

("Show status bar");
sbarMi.setMnemonic(KeyEvent.VK_S);
sbarMi.setDisplayedMnemonicIndex(5);
sbarMi.setSelected(true);

sbarMi.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent e) {

if (e.getStateChange() ==

ItemEvent.SELECTED) {
statusbar.setVisible(true);
} else {
statusbar.setVisible(false);
}

}

});

// Newer code
JMenu helpMenu = new JMenu("Help");


viewMenu.add(sbarMi);
fileMenu.add(newMi);
fileMenu.add(openMi);
fileMenu.add(saveMi);
fileMenu.addSeparator();
fileMenu.add(impMenu);
fileMenu.addSeparator();
fileMenu.add(exitMi);


menubar.add(fileMenu);
menubar.add(viewMenu);
menubar.add(Box.createHorizontalGlue());
menubar.add(helpMenu);

setJMenuBar(menubar);
}

private void createToolBar() {

JToolBar toolbar = new JToolBar();

ImageIcon icon = new ImageIcon("exit.png");

JButton exitButton = new JButton(icon);
toolbar.add(exitButton);

exitButton.addActionListener(new ActionListener()

{
@Override
public void actionPerformed(ActionEvent event)

{
System.exit(0);
}
});

add(toolbar, BorderLayout.NORTH);
}

private void createLayout(JComponent... arg) {

Container pane = getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);

gl.setAutoCreateContainerGaps(true);

gl.setHorizontalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
);

gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
);

pack();
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
gui ex = new gui();
ex.setVisible(true);
}
});
}
}

我还检查了 stackoverflow 和谷歌搜索,但我看到的要么是针对 android 的,要么是我能理解的。一些建议将不胜感激。谢谢。

最佳答案

您在创建并添加 JToolBar 后更改了布局(到 GroupLayout - 不要让我开始做这个选择)

所以,如果我们更改createLayout,它会创建一个辅助组件,例如......

private void createLayout(JComponent... arg) {

JPanel pane = new JPanel();
//Container pane = getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);

gl.setAutoCreateContainerGaps(true);

gl.setHorizontalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
);

gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
);

add(pane);

pack();
}

现在它会显示类似...的内容

Frame

由于您的内容和 JToolBar 都需要位于 contentPane 中,因此您需要注意对其所做的任何更改。此解决方案创建了一个辅助 Pane ,您可以按照您想要的方式将“主要”内容放置在其中,但随后将其添加到仍然使用 BorderLayout< 的 contentPane

关于Java (swing) 工具栏未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33139706/

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