我想在GUI中有一个菜单栏。菜单不可见。
public class GUI extends JPanel implements ItemListener{
final static String RUN_TEST = "Test 4G";
final static String SETTINGS = "Settings";
JPanel p;
JPanel cards = new JPanel(new CardLayout());
public GUI(){
JFrame window = new JFrame();
TestRun runTest = new TestRun();
cards.add(runTest , RUN_TEST);
cards.add(runTest , SETTINGS);
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, RUN_TEST);
window.setContentPane(cards);
window.pack();
window.setVisible(true);
}
@Override
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}
}
如何向用户显示菜单“Test 4G”和“settings”以便他们可以更改JPanel? 谢谢你的帮助
答案 0 :(得分:1)
这是在JMenuBar
中使用JFrame
和JPopupMenu
JPanel
中使用public class MainFrame extends JFrame {
final static String RUN_TEST = "Test 4G";
final static String SETTINGS = "Settings";
private JPanel viewPanel = new JPanel();
public MainFrame() throws HeadlessException {
super("MainFrame");
cretaeGUI();
}
private void cretaeGUI() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new BorderLayout());
setJMenuBar(cretaeMenuBar());
setMinimumSize(new Dimension(800, 600));
viewPanel.setLayout(new CardLayout());
viewPanel.add(new Test4GView(this), RUN_TEST);
viewPanel.add(new SettingsView(this), SETTINGS);
add(viewPanel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
}
private JMenuBar cretaeMenuBar() {
JMenuItem testMenuItem = new JMenuItem("Test 4G");
testMenuItem.addActionListener(this::showTest4GView);
JMenuItem settingsMenuItem = new JMenuItem("Settings");
settingsMenuItem.addActionListener(this::showSettingsView);
JMenu viewMenu = new JMenu("View");
viewMenu.add(testMenuItem);
viewMenu.add(settingsMenuItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(viewMenu);
return menuBar;
}
private void showView(String name) {
((CardLayout)viewPanel.getLayout()).show(viewPanel, name);
}
public void showTest4GView(ActionEvent event) {
showView(RUN_TEST);
}
public void showSettingsView(ActionEvent event) {
showView(SETTINGS);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));
}
}
的示例。
public class Test4GView extends JPanel {
private MainFrame mainFrame;
public Test4GView(MainFrame mainFrame) {
this.mainFrame = mainFrame;
add(new JLabel("Test 4G"));
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
showPopupMenu(e);
}
@Override
public void mouseReleased(MouseEvent e) {
showPopupMenu(e);
}
private void showPopupMenu(MouseEvent e) {
if(!e.isPopupTrigger()) {
return;
}
JMenuItem showSettingsView = new JMenuItem("Settings");
showSettingsView.addActionListener(mainFrame::showSettingsView);
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(showSettingsView);
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
public class SettingsView extends JPanel {
private MainFrame mainFrame;
public SettingsView(MainFrame mainFrame) {
this.mainFrame = mainFrame;
add(new JLabel("Settings"));
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
showPopupMenu(e);
}
@Override
public void mouseReleased(MouseEvent e) {
showPopupMenu(e);
}
private void showPopupMenu(MouseEvent e) {
if(!e.isPopupTrigger()) {
return;
}
JMenuItem showSettingsView = new JMenuItem("Test 4G");
showSettingsView.addActionListener(mainFrame::showTest4GView);
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(showSettingsView);
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
这些都是观点
"../node_modules/bootstrap/dist/css/bootstrap.min.css"