3

我正在使用JFrame. 我制作了一个简单的“开始”屏幕,它基本上由 aString和 a组成JButton。我正在使用该actionPerformed(ActionEvent e)方法单击按钮。我不知道如何使用按钮单击来更改卡片。这似乎是一个很容易解决的问题,但随之而来的是扭曲:我的主 JFrame、我的 StartScreen 和我的 JPanel 都在不同的文件中。我的主文件 Virus.java 是我创建JFrame. 我的文件 VirusGamePanel.java 是游戏发生的地方。我的文件 StartScreen.java 是带有按钮的屏幕。当玩家单击按钮时,我想将“卡片”更改为游戏屏幕。我怎样才能做到这一点?我的 StartScreen.java 文件:

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 文件:

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();

    }

}
4

2 回答 2

7

你只需要在你的actionPerformed(...)方法中纠正这个:

public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==start)
    {
        //what to do here?
        CardLayout cardLayout = (CardLayout) Virus.thegame.getLayout();
        cardLayout.next(Virus.thegame);
    }
}

正如@kleopatra(皇后)本人非常指出的那样,不要覆盖,paint()而是在paintComponent(Graphics g)any 方法中进行绘画JPanel/JComponent。此外,首先将组件添加到您的JFrame,一旦实现它的大小,然后只将其设置为可见,而不是在那之前。与其为JFrame简单地覆盖JPanel的方法设置大小getPreferredSize(),不如让它返回一些有效的Dimension对象。

下次编写代码时,请务必观看此序列:

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));
    }
}
于 2012-08-21T16:14:51.073 回答
2

您的StartScreen类必须有权访问该类CardLayoutJFrame实例和VirusGamePanel该类的实例。setLayout您可以在构造函数或类的方法和setVirusGamePanel方法中传递这些实例StartScreen

就像是:

layout.next(virusGamePanel);

应该管用。

于 2012-08-21T15:48:52.553 回答