Java 中的 CardLayout 通过其中一张“卡片"中的动作而改变 [英] CardLayout in Java change by action in one of the 'cards'
问题描述
我正在使用 JFrame
制作一个简单的游戏.我制作了一个简单的开始"屏幕,它基本上由一个 String
和一个 JButton
组成.我正在使用 actionPerformed(ActionEvent e)
方法获取按钮点击.我不知道如何通过单击按钮来更改卡片.这似乎是一个需要解决的简单问题,但随之而来的问题是:我的主 JFrame、我的 StartScreen 和我的 JPanel 都在不同的文件中.我的主文件 Virus.java 是我创建 JFrame
的地方.我的文件 VirusGamePanel.java 是游戏发生的地方.我的文件 StartScreen.java 是带有按钮的屏幕.当玩家单击按钮时,我想将卡片"更改为游戏屏幕.我怎样才能做到这一点?我的 StartScreen.java 文件:
I am making a simple game using a JFrame
. I have made a simple "Start" screen which basically consists of a String
and a JButton
. I am picking up the button click with the actionPerformed(ActionEvent e)
method. I don't know how to change the cards using a button click. This may seem like a simple problem to solve, but the twist comes with this: My main JFrame, my StartScreen and my JPanel where the game takes place are all in separate files. My main file, Virus.java, is where I create the JFrame
. My file VirusGamePanel.java is where the game takes place. My file StartScreen.java is the screen with the button. I want to change 'cards' to the game screen when the player clicks the button. How can I do this?
My StartScreen.java file:
package virus;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.CardLayout;
public class StartScreen extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
JButton start = new JButton("Start");
public StartScreen(){
start.addActionListener(this);
start.setBounds(new Rectangle(400,300,100,30));
this.add(start);
}
public void paint(Graphics g){
super.paint(g);
g.setFont(new Font("Impact",Font.BOLD,72));
g.setColor(Color.MAGENTA);
g.drawString("Virus",275,300);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==start)
{
//what to do here?
}
}
}
我的 Virus.java 文件:
My Virus.java file:
package virus;
import javax.swing.*;
import java.awt.CardLayout;
import virus.StartScreen;
public class Virus extends JFrame{
private static final long serialVersionUID =1L;
JFrame jf = new JFrame("Virus");
static JPanel thegame = new JPanel(new CardLayout());
JPanel game = new VirusGamePanel();
JPanel start = new StartScreen();
public Virus(){
jf.setResizable(false);
jf.setSize(600,600);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
jf.setVisible(true);
jf.add(thegame);
thegame.add(start);
thegame.add(game);
}
public static void main(String[] args) {
new Virus();
}
}
推荐答案
您只需在 actionPerformed(...)
方法中纠正此问题:
You simply have to right this in your actionPerformed(...)
method :
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==start)
{
//what to do here?
CardLayout cardLayout = (CardLayout) Virus.thegame.getLayout();
cardLayout.next(Virus.thegame);
}
}
正如@kleopatra (THE EMPRESS) 本人所指出的,不要覆盖 paint()
而是在 paintComponent(Graphics g)
方法中做你的绘画任何 JPanel/JComponent
.此外,首先将组件添加到您的JFrame
,一旦它的大小实现,然后只将其设置为可见,而不是在此之前.无需为 JFrame
设置大小,只需覆盖 JPanel
的方法 getPreferredSize()
,使其返回一些有效的 Dimension代码>对象.
As very much pointed out by @kleopatra (THE EMPRESS) herself, don't override paint()
instead do your painting stuff inside paintComponent(Graphics g)
method of any JPanel/JComponent
. Moreover, first add the components to your JFrame
, once it's size is realized, then only set it to Visible, not before that. Instead of setting sizes for the JFrame
simply override the JPanel
's method getPreferredSize()
, make it return some valid Dimension
Object.
请在下次编写代码时观看此序列:
Do watch this sequence, as you write your code the next time :
public Virus(){
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
thegame.add(start);
thegame.add(game);
jf.add(thegame);
jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
这是你的完整代码:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.CardLayout;
public class Virus extends JFrame{
private static final long serialVersionUID =1L;
JFrame jf = new JFrame("Virus");
static JPanel thegame = new JPanel(new CardLayout());
JPanel game = new VirusGamePanel();
JPanel start = new StartScreen();
public Virus(){
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
thegame.add(start);
thegame.add(game);
jf.add(thegame);
jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
public static void main(String[] args) {
new Virus();
}
}
class StartScreen extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
JButton start = new JButton("Start");
public StartScreen(){
start.addActionListener(this);
start.setBounds(new Rectangle(400,300,100,30));
this.add(start);
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setFont(new Font("Impact",Font.BOLD,72));
g.setColor(Color.MAGENTA);
g.drawString("Virus",275,300);
}
@Override
public Dimension getPreferredSize()
{
return (new Dimension(600, 600));
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==start)
{
//what to do here?
CardLayout cardLayout = (CardLayout) Virus.thegame.getLayout();
cardLayout.next(Virus.thegame);
}
}
}
class VirusGamePanel extends JPanel
{
public VirusGamePanel()
{
JLabel label = new JLabel("I am ON", JLabel.CENTER);
add(label);
}
@Override
public Dimension getPreferredSize()
{
return (new Dimension(600, 600));
}
}
这篇关于Java 中的 CardLayout 通过其中一张“卡片"中的动作而改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!