我一直在关注本教程:
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和/或我能理解的。一些建议将不胜感激。谢谢。
答案 0 :(得分:1)
您在创建和添加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();
}
它现在会显示类似......
由于您的内容和JToolBar
都需要居住在contentPane
,因此您需要对所做的任何更改都很有用。此解决方案创建了一个辅助窗格,您可以在其上使用#34;内容可以按照您想要的方式放置,但随后会添加到仍在使用contentPane
的{{1}}