带有 Jbutton 的卡片布局不切换
Cardlayout With Jbutton Not Switching
你好,我是 java 编程的新手,我目前正在尝试使用 GUI 开发一个小游戏,现在我遇到了一个问题,我只能使用带有卡布局,不幸的是我想使用我自己的按钮图标,为此我需要使用 JButton。但出于某种原因,当我使用 JButton 时,它不会与面板切换。我只是想知道我做错了什么。我正在使用一个名为 Ready To Program 的 IDE 作为小程序进行编程。
{
CardLayout PanelLayout = new CardLayout ();
JPanel AppPanel = new JPanel (); //Main App Panel using cardlayout
JPanel StartPanel = new JPanel (); //Start Menu Panel
JPanel GamePanel = new JPanel (); //Running Game Panel
JButton StartBtn = new JButton ();
public void init ()
{
StartBtn.setIcon(new ImageIcon ("StartIcon.png"));
StartBtn.setBorder(BorderFactory.createEmptyBorder());
StartBtn.setContentAreaFilled(true);
StartPanel.add (StartBtn);
AppPanel.setLayout (PanelLayout);
AppPanel.add (StartPanel, "1");
AppPanel.add (GamePanel, "2");
PanelLayout.show (AppPanel, "1");
setLayout (new BorderLayout ());
add ("Center", AppPanel);
}
public boolean action (Event e, Object o)
{
if (e.target == StartBtn)
{
PanelLayout.show (AppPanel, "2");
}
return true;
}
}
所有变量名中的第一个不应以大写字符开头。我从未在任何使用大写字符的论坛中看到过教程或答案,因此请不要自行制定约定。通过实例学习。
I can only use a normal button with a card layout, unfortunately I want to use my own icon with the button and to do this I need to use a JButton.
JButton 是一个普通按钮。你指的是什么其他类型的按钮?不管按钮是有文字还是有图标,或者两者都有,它仍然是一个按钮。
您代码中的按钮不起作用,因为您没有向该按钮添加 ActionListener。
阅读 How to Use Button 上的 Swing 教程部分以获取更多信息和示例。本教程还有一个关于 How to Write ActionListeners
.
的部分
您的代码有一个 action(...)
方法。我不知道这是否是由 IDE 生成的,但基本上该方法中的代码就是您的 ActionListener 中的代码。
add ("Center", AppPanel);
这不是向面板添加组件的方法的正确形式。首先不要使用硬编码的文字字符串,使用API:
提供的变量
add (appPanel, BorderLayout.CENTER);
你好,我是 java 编程的新手,我目前正在尝试使用 GUI 开发一个小游戏,现在我遇到了一个问题,我只能使用带有卡布局,不幸的是我想使用我自己的按钮图标,为此我需要使用 JButton。但出于某种原因,当我使用 JButton 时,它不会与面板切换。我只是想知道我做错了什么。我正在使用一个名为 Ready To Program 的 IDE 作为小程序进行编程。
{
CardLayout PanelLayout = new CardLayout ();
JPanel AppPanel = new JPanel (); //Main App Panel using cardlayout
JPanel StartPanel = new JPanel (); //Start Menu Panel
JPanel GamePanel = new JPanel (); //Running Game Panel
JButton StartBtn = new JButton ();
public void init ()
{
StartBtn.setIcon(new ImageIcon ("StartIcon.png"));
StartBtn.setBorder(BorderFactory.createEmptyBorder());
StartBtn.setContentAreaFilled(true);
StartPanel.add (StartBtn);
AppPanel.setLayout (PanelLayout);
AppPanel.add (StartPanel, "1");
AppPanel.add (GamePanel, "2");
PanelLayout.show (AppPanel, "1");
setLayout (new BorderLayout ());
add ("Center", AppPanel);
}
public boolean action (Event e, Object o)
{
if (e.target == StartBtn)
{
PanelLayout.show (AppPanel, "2");
}
return true;
}
}
所有变量名中的第一个不应以大写字符开头。我从未在任何使用大写字符的论坛中看到过教程或答案,因此请不要自行制定约定。通过实例学习。
I can only use a normal button with a card layout, unfortunately I want to use my own icon with the button and to do this I need to use a JButton.
JButton 是一个普通按钮。你指的是什么其他类型的按钮?不管按钮是有文字还是有图标,或者两者都有,它仍然是一个按钮。
您代码中的按钮不起作用,因为您没有向该按钮添加 ActionListener。
阅读 How to Use Button 上的 Swing 教程部分以获取更多信息和示例。本教程还有一个关于 How to Write ActionListeners
.
您的代码有一个 action(...)
方法。我不知道这是否是由 IDE 生成的,但基本上该方法中的代码就是您的 ActionListener 中的代码。
add ("Center", AppPanel);
这不是向面板添加组件的方法的正确形式。首先不要使用硬编码的文字字符串,使用API:
提供的变量add (appPanel, BorderLayout.CENTER);