我正在使用制作一个简单的游戏JFrame。我制作了一个简单的“开始”屏幕,该屏幕主要由a String和a组成JButton。我正在使用该actionPerformed(ActionEvent e)方法单击按钮。我不知道如何通过单击按钮来更改卡。这看起来似乎是一个很简单的问题,但随之而来的是:游戏的主JFrame,StartScreen和JPanel都位于单独的文件中。我的主文件Virus.java是创建该文件的位置JFrame。我的文件VirusGamePanel.java是进行游戏的地方。我的文件StartScreen.java是带有按钮的屏幕。当玩家单击按钮时,我想将“纸牌”更改为游戏屏幕。我怎样才能做到这一点?我的StartScreen.java文件:
JFrame
String
JButton
actionPerformed(ActionEvent e)
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(); } }
您只需在您的actionPerformed(...)方法中对此进行纠正:
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一旦实现大小,首先将组件添加到您的中,然后仅将其设置为Visible,而不是在此之前。而不是为JFrame简单地覆盖JPanel的方法设置大小,而是getPreferredSize()使其返回一些有效的DimensionObject。
paint()
paintComponent(Graphics g)
JPanel/JComponent
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)); } }