我是java初学者
我想在button的响应中,登录成功后跳转到birdgame的页面,但是panel中的action方法只能在main方法中执行才有效,在button响应的方法里new birdframe时action不能正常执行,点击界面没反应,请问应该如何修改这个代码
出问题的代码如下
login页面
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Login extends JFrame implements ActionListener{
String username="a";
String password="a";
BirdPanel panel;
JFrame loginframe;
JLabel label1,label2;
JTextField user;
JPasswordField pass;
JPanel jp1,jp2,jp3;
JButton jb;
public Login(){
super("登录");
loginframe=new JFrame();
label1 = new JLabel("姓名");
label2 = new JLabel("密码");
user = new JTextField("a", 10);
pass = new JPasswordField("a", 10);
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jb = new JButton("登录");
panel = new BirdPanel();
}
public void init(){
//setLayout设置为null即为清空布局管理器,允许组件自由布局
loginframe.setLayout(null);
loginframe.setSize(432,674);
loginframe.setLocationRelativeTo(null);
loginframe.setVisible(true);
loginframe.setResizable(false);
loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginframe.add(jb);
jp1.add(label1);
jp1.add(user);
jp1.setBounds(0,150,432,50);
jp2.add(label2);
jp2.add(pass);
jp2.setBounds(0, 225, 432, 50);
jp3.add(jb);
jp3.setBounds(0,300,432,50);
loginframe.add(jp1);
loginframe.add(jp2);
loginframe.add(jp3);
jb.addActionListener(this);
}
public static void main(String[] args) {
new Login().init();
// BirdFrame.main(args);
// BirdFrame frame = new BirdFrame();
// frame.init();
// frame.panel.action();
}
@Override
public void actionPerformed(ActionEvent e) {
if(username.equals(user.getText()) && password.equals(pass.getText())){
JOptionPane.showMessageDialog(null,"登录成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
//关闭登录窗口
loginframe.dispose();
// BirdFrame birdframe = new BirdFrame();
// birdframe.init();
// birdframe.panel.action();
}else{
//输入错误时报错并清空输入框
JOptionPane.showMessageDialog(null, "错误", "提示", JOptionPane.ERROR_MESSAGE);
user.setText("");
pass.setText("");
}
}
}
游戏窗口页面
import javax.swing.JFrame;
public class BirdFrame extends JFrame{
//定义游戏面板
BirdPanel panel ;
//构造方法,初始化窗体属性
public BirdFrame(){
//创建面板对象
panel = new BirdPanel();
}
public void init(){
this.setTitle("笨鸟");
this.setSize(432,674);
//设置位置(据中)
this.setLocationRelativeTo(null);
//设置不允许改变窗体大小
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.add(panel);
}
public static void main(String[] args) {
BirdFrame frame = new BirdFrame();
frame.init();
}
}
游戏面板页面
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class BirdPanel extends JPanel implements MouseListener{
Colum c1,c2;//定义两个柱子
BufferedImage background;//背景图片
BufferedImage startImage;//开始图片
BufferedImage overImage;//结束图片
Ground ground;//定义地板对象
Bird bird;//定义鸟对象
int score;//分数
boolean gameover;//判断游戏是否结束的boolean数
boolean start;// 判断游戏是否开始的boolean数
public BirdPanel(){
c1 = new Colum(1);//第一根柱子
c2 = new Colum(2);
ground = new Ground();
bird = new Bird();
try {
background = ImageIO.read(this.getClass().getResource("/img/bg.png"));
startImage = ImageIO.read(this.getClass().getResource("/img/start.png"));
overImage = ImageIO.read(this.getClass().getResource("/img/gameover.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//将监听器添加到面板中
this.addMouseListener(this);
}
//加@Override重写paint同名函数
@Override
public void paint(Graphics g){
super.paint(g);
//drawImage(image img,x,y,ImageObserver observer);
//drawImage(图像对象,x位置,y位置,异步信息通知的对象
g.drawImage(background, 0, 0, null);//画背景图片
if(!start){
//游戏没开始的话就画开始图片
g.drawImage(startImage,0,0,null);
}
//先画哪个哪个就在下面
//画柱子
g.drawImage(c1.img,c1.x-c1.width/2,c1.y-c1.height/2,null);
g.drawImage(c2.img,c2.x-c2.width/2,c2.y-c2.height/2,null);
//画鸟
g.drawImage(bird.img,bird.x-bird.width/2,bird.y-bird.height/2, null);
//画地板
g.drawImage(ground.img, ground.x, ground.y, null);
//设置界面中文字格式
Font font=new Font("楷体",Font.BOLD,30);
g.setFont(font);
g.setColor(Color.white);
//画界面分数
g.drawString("分数:" + score, 10, 30);
//判断游戏是否结束,如果gameover传入true则结束
if(gameover){
g.drawImage(overImage, 0, 0, null);
}
}
@Override
public void mousePressed(MouseEvent e) {
if(gameover){
//如果游戏结束
//重新启动游戏
reStart();
}else{
//游戏没结束
//游戏开始
start = true;
bird.flappy();
}
}
public void action(){
//一直刷新界面
while(true){
if(!gameover){
if(start){
//如果游戏没有结束,并且游戏开始了
//让地板移动
ground.setup();
//让鸟飞
bird.setup();
bird.fly();
//让主子移动
c1.setup();
c2.setup();
//增加难度,让第二根柱子上下移动
//c2.setupY();
}
//穿过一个柱子得一分
if(bird.x==c1.x || bird.x==c2.x){
score ++;
}
}
//如果鸟撞击柱子或者地面,游戏结束
if(bird.hit(c1)||bird.hit(c2)||bird.hit(ground)){
gameover = true;//游戏结束
}
this.repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
public void reStart(){
c1 = new Colum(1);
c2 = new Colum(2);
ground = new Ground();
bird = new Bird();
start = false;
gameover =false;
score = 0;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}