CardLayout in Java change by action in one of the 'cards'

不想你离开。 提交于 2019-11-29 15:12:50

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

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

Here is your full code :

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

Your StartScreen class has to have access to the instance of the CardLayout of the JFrame and the instance of the VirusGamePanel class. You can pass these instances in the constructor or a setLayout method and setVirusGamePanel method of your StartScreen class.

Something like:

layout.next(virusGamePanel);

should work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!