嗨,我对 Java 还不够陌生,我正在尝试学习在窗口构建器中使用卡片功能,但我在使用 show 方法时遇到了问题。当我单击按钮时,它不会更改为所需的卡。我一直在松散地遵循本教程
这是我的代码:
public class HomeScreen {
private final String WEIGHTS = "Weights panel";
private final String EXCERCISE = "Excercise panel";
private final String CARDIO = "Cardio panel";
private JPanel cards;
//private CardLayout cardLayout = new CardLayout();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run() {
createAndShowGUI();
}
});
}
});
}
public void addComponentToPane(Container pane) {
final JPanel background = new JPanel();
background.setBackground(Color.DARK_GRAY);
background.setLayout(new CardLayout(0, 0));
JButton btnHome = new JButton();
btnHome.setForeground(Color.LIGHT_GRAY);
btnHome.setFont(new Font("Tekton Pro Cond", Font.PLAIN, 22));
btnHome.setBackground(Color.DARK_GRAY);
btnHome.setText("Home");
background.add(btnHome, "name_107366949842088");
JPanel cardio = new JPanel();
JPanel exercise = new JPanel();
JPanel weights = new JPanel();
JButton btnWeights_1 = new JButton("Weights");
btnWeights_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("Button Pressed");
CardLayout cL = (CardLayout) (cards.getLayout()) ;
//cL.next(cards);
cL.show(cards, WEIGHTS);
}
});
JButton btnCardio = new JButton("Cardio");
/*
* Add new cards
*/
cards = new JPanel(new CardLayout());
cards.add(exercise, EXCERCISE);
exercise.setLayout(new BoxLayout(exercise, BoxLayout.X_AXIS));
exercise.add(btnWeights_1);
exercise.add(btnCardio);
cards.add(cardio,CARDIO);
cards.add(weights,"Weights panel");
JButton btnLegs = new JButton("Running");
cardio.add(btnLegs);
JButton btnSwimming = new JButton("Swimming");
cardio.add(btnSwimming);
JButton btnCycling = new JButton("Cycling");
cardio.add(btnCycling);
JButton btnBoxing = new JButton("Boxing");
cardio.add(btnBoxing);
cards.add(weights);
JButton btnBiceps = new JButton("Biceps");
weights.add(btnBiceps);
JButton btnLegs_1 = new JButton("Legs");
weights.add(btnLegs_1);
JButton btnBack = new JButton("Back");
weights.add(btnBack);
JButton btnChest = new JButton("Chest");
weights.add(btnChest);
pane.add(background, BorderLayout.PAGE_START);
pane.add(cards,BorderLayout.PAGE_END);
}
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("CardLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
HomeScreen demo = new HomeScreen();
demo.addComponentToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
}