这个问题可能已经解决了,但我试图让我的菜单栏与我的CardLayout一起工作,不像其他问题的按钮;我已经坚持了很长时间。
我目前正在努力让三个单独的课程一起工作,
我的CardLayout课程:
public class CardLayoutExample {
private CardLayout cardLayout = new CardLayout(20, 20);
private JPanel contentPane = new JPanel(cardLayout);
private MyPanel panel1;
private MyPanel panel2;
private MyPanel panel3;
private void displayGUI()
{
MenuBar menuBar = new MenuBar();
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane.add(createPanel(Color.BLACK), "Panel 1");
contentPane.add(createPanel(Color.RED), "Panel 2");
frame.setContentPane(contentPane);
frame.setJMenuBar(menuBar.getMenuBar());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public JPanel createPanel(Color color) {
JPanel panel = new JPanel();
panel.setBackground(color);
return panel;
}
public void redCard() {
System.out.println("Selected Red Item");
cardLayout.show(contentPane, "Panel 2");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CardLayoutExample().displayGUI();
}
});
}
}
MenuBar类:
public class MenuBar {
private JMenuBar menuBar;
private MenuActionListener mal;
public MenuBar() {
mal = new MenuActionListener();
System.out.println("menuBar");
//Creates a menubar for a JFrame
menuBar = new JMenuBar();
//Define and add drop down menu to the menubar
JMenu mainMenu = new JMenu("Main Menu");
menuBar.add(mainMenu);
//Define addMenu items
JMenuItem addRedItem = new JMenuItem("Red");
addRedItem.addActionListener(mal);
//Add main menu items/menu
mainMenu.add(addRedItem);
}
public JMenuBar getMenuBar()
{
return menuBar;
}
}
我的MenuActionListener类:
public class MenuActionListener implements ActionListener {
public void redActionPerformed() {
new CardLayoutExample().redCard();
}
@Override
public void actionPerformed(final ActionEvent e) {
String command = e.getActionCommand();
System.out.println(command);
switch (command) {
case "Red":
redActionPerformed();
break;
default:
}
}
}
当我从菜单栏中选择红色项目时,会触发以下代码行:System.out.println("Selected Red Item")
,然后显示我的红色面板的代码,但是,卡片根本没有变化?
我一直在尝试让我的菜单栏更换我的卡片;如何修复我的代码,以便我能正确显示我想要的卡?
提前谢谢。
答案 0 :(得分:1)
问题出在您的MenuActionListener.redActionPerformed
方法中。您正在创建一个全新的CardLayoutExample
对象并使用它而不是代表实际UI的现有对象。解决此问题的最简单方法是使您的Menu类嵌套,以便它们获得对外部CardLayoutExample
类的隐式引用。然后在redActionPerformed
中,您可以直接致电redCard()
。否则,您需要将对CardLayoutExample对象的引用传递给MenuActionListener类。请参阅下面的完整示例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CardLayoutExample {
private CardLayout cardLayout = new CardLayout(20, 20);
private JPanel contentPane = new JPanel(cardLayout);
private final static String p1 = "Panel 1";
private final static String p2 = "Panel 2";
private void displayGUI()
{
MenuBar menuBar = new MenuBar();
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane.add(createPanel(Color.BLACK), p1);
contentPane.add(createPanel(Color.RED), p2);
frame.setContentPane(contentPane);
frame.setJMenuBar(menuBar.getMenuBar());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public JPanel createPanel(Color color) {
JPanel panel = new JPanel();
panel.setBackground(color);
return panel;
}
public void redCard() {
System.out.println("Selected Red Item ");
((CardLayout)contentPane.getLayout()).show(contentPane, p2);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CardLayoutExample().displayGUI();
}
});
}
// Inner Menu Bar class
class MenuBar {
private JMenuBar menuBar;
private MenuActionListener mal;
public MenuBar() {
mal = new MenuActionListener();
System.out.println("menuBar");
//Creates a menubar for a JFrame
menuBar = new JMenuBar();
//Define and add drop down menu to the menubar
JMenu mainMenu = new JMenu("Main Menu");
menuBar.add(mainMenu);
//Define addMenu items
JMenuItem addRedItem = new JMenuItem("Red");
addRedItem.addActionListener(mal);
//Add main menu items/menu
mainMenu.add(addRedItem);
}
public JMenuBar getMenuBar()
{
return menuBar;
}
}
//Inner MenuActionListener class
class MenuActionListener implements ActionListener {
public void redActionPerformed() {
// Call the redCard() method in outer object.
redCard();
}
@Override
public void actionPerformed(final ActionEvent e) {
String command = e.getActionCommand();
System.out.println(command);
switch (command) {
case "Red":
redActionPerformed();
break;
default:
}
}
}
}