gpt4 book ai didi

java - JToolBar 不显示

转载 作者:行者123 更新时间:2023-12-01 16:35:33 25 4
gpt4 key购买 nike

我有一个简单的绘图应用程序。我需要在左侧添加一个菜单和一个工具栏。现在,我不再使用简单的 JFrame,而是创建一个扩展 JFrame 的简单类。我能够按照一些在线示例添加菜单,但不知道如何添加 JToolBar。我尝试了几种不同的方法,但没有任何效果。我没有收到错误,一切都很好,但我没有看到任何 JToolBar。这是我的 JFrame 的代码,希望对您有所帮助。

class Menu extends JFrame {
private JMenuItem openItem;
private JMenuItem saveItem;
private JMenuItem saveAsItem;

public Menu(String title) {

openItem = new JMenuItem("Open...");
openItem.setMnemonic('O');
openItem.setAccelerator(KeyStroke.getKeyStroke("control O"));

saveItem = new JMenuItem("Save");
saveItem.setMnemonic('S');
saveItem.setAccelerator(KeyStroke.getKeyStroke("control S"));

saveAsItem = new JMenuItem("Save As...");
saveAsItem.setMnemonic('S');
saveAsItem.setAccelerator(KeyStroke.getKeyStroke("control S"));

// (2) Build menubar, menus, and add menuitems.
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
menubar.add(fileMenu);
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(saveItem);

// (3) Add listeners to menu items
openItem.addActionListener(new OpenAction()); // TODO change

setJMenuBar(menubar);

JToolBar toolbar = new JToolBar("Toolbar", JToolBar.VERTICAL);//);
// JPanel panel = new JPanel();
// panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton newb = new JButton("new");
toolbar.add(newb);
// toolbar.setOpaque(true);
toolbar.setLocation(100, 100);
toolbar.setVisible(true);
// toolbar.setMinimumSize(new Dimension(100, 100));
// toolbar.setAlignmentX(0);

// panel.add(toolbar);
add(toolbar, BorderLayout.NORTH);
getContentPane().add(toolbar, BorderLayout.NORTH);
// getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);

setTitle(title);

pack();
setLocationRelativeTo(null); // Center window.
}

// OpenAction
class OpenAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Menu.this, "Can't Open.");
}
}
}

最佳答案

它工作正常,并且您不需要设置可见工具栏,因为它默认显示,也不要在同一位置(北)两次添加工具栏

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MenuDemo {
public static void main(String... args) {
EventQueue.invokeLater(
new Runnable() {
@Override
public void run() {
JFrame menu = new Menu("Testing");
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu.setVisible(true);
}
}
);
}
}

class Menu extends JFrame {
private JMenuItem openItem;
private JMenuItem saveItem;
private JMenuItem saveAsItem;

public Menu(String title) {

openItem = new JMenuItem("Open...");
openItem.setMnemonic('O');
openItem.setAccelerator(KeyStroke.getKeyStroke("control O"));

saveItem = new JMenuItem("Save");
saveItem.setMnemonic('S');
saveItem.setAccelerator(KeyStroke.getKeyStroke("control S"));

saveAsItem = new JMenuItem("Save As...");
saveAsItem.setMnemonic('S');
saveAsItem.setAccelerator(KeyStroke.getKeyStroke("control S"));

// (2) Build menubar, menus, and add menuitems.
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
menubar.add(fileMenu);
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(saveItem);

// (3) Add listeners to menu items
openItem.addActionListener(new OpenAction()); // TODO change

setJMenuBar(menubar);

JToolBar toolbar = new JToolBar("Toolbar", JToolBar.VERTICAL);//);

JButton newb = new JButton("new");
toolbar.add(newb);

add(toolbar, BorderLayout.NORTH);
setTitle(title);
setLocationRelativeTo(null);
pack();
}

// OpenAction
private class OpenAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Menu.this, "Can't Open.");
}
}

}

关于java - JToolBar 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9550246/

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