Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使一个面板在另一个面板内工作?_Java_Image_Swing_Timer_Paintcomponent - Fatal编程技术网

Java 如何使一个面板在另一个面板内工作?

Java 如何使一个面板在另一个面板内工作?,java,image,swing,timer,paintcomponent,Java,Image,Swing,Timer,Paintcomponent,对于我正在制作的游戏,我希望玩家从菜单屏幕(Panel05)开始,然后单击按钮开始实际的游戏(Panel00)。而且,在玩游戏时,如果他们赢了或输了,当他们点击另一个按钮时,他们要么返回菜单,要么进入另一个关卡。现在,面板都是独立的程序,有自己的驱动程序,如果可能的话,我不确定如何让一个面板在另一个面板中工作。我将感谢所有的建议、回答或批评。下面是菜单面板 import javax.swing.*; import java.awt.*; import java.awt.event.*; impo

对于我正在制作的游戏,我希望玩家从菜单屏幕(Panel05)开始,然后单击按钮开始实际的游戏(Panel00)。而且,在玩游戏时,如果他们赢了或输了,当他们点击另一个按钮时,他们要么返回菜单,要么进入另一个关卡。现在,面板都是独立的程序,有自己的驱动程序,如果可能的话,我不确定如何让一个面板在另一个面板中工作。我将感谢所有的建议、回答或批评。下面是菜单面板

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;


public class Panel05 extends JPanel
{
 private BufferedImage myImage;
 private Graphics myBuffer;
 public JButton button1;

public Panel05()
{

  myImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
  myBuffer = myImage.getGraphics();




  setLayout(null);


  button1 = new JButton();
  button1.setSize(100, 100);
  button1.setLocation(500,500);
  button1.setForeground(Color.WHITE);
  button1.setFont(new Font("Serif", Font.BOLD, 30));
  button1.setText("Start");
  button1.addActionListener(new B1Listener());
  button1.setBorder(null);
  button1.setOpaque(false);
  button1.setContentAreaFilled(false);
  button1.setBorderPainted(false);
  add(button1);
  setFocusable(true);  
}


public void paintComponent(Graphics g)
{
  ImageIcon Nintendo = new ImageIcon("trumpL.png");

  g.drawImage(Nintendo.getImage(), 0, 0, 1000, 1000, null);

}

private class B1Listener implements ActionListener
{
  public void actionPerformed(ActionEvent e)
  {


  }
 }
}
这是游戏第一关的面板

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.image.BufferedImage;
 import javax.swing.Timer;


 public class Panel00 extends JPanel
{
 private BufferedImage myImage;
 private Graphics myBuffer;
 public Timer timer;
 public JButton button1;
 public JButton button2;
 public JLabel label1 = new JLabel("Good Choice!");
 public JLabel label2 = new JLabel("You're Fired!!");
 public int x = 5;      //CountDown from 5  
 public int delay = 1000;   //milliseconds
 boolean drawWin = false;
 boolean drawLose = false;



 public Panel00()
{

  myImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
  myBuffer = myImage.getGraphics();




  setLayout(null);


  button1 = new JButton();
  button1.setSize(300, 200);
  button1.setLocation(100,150);
  button1.setFont(new Font("Serif", Font.BOLD, 18));
  button1.setText("<html><center>"+"Until we are able to determine and understand this problem"+"<br>"+" and the dangerous threat it poses, our country cannot be the victims of horrendous attacks"+"<br>"+"by people that believe only in Jihad, and have no sense of reason or respect for human life"+"</center></html>");
  button1.addActionListener(new B1Listener());
  button1.setBorder(null);
  button1.setOpaque(false);
  button1.setContentAreaFilled(false);
  button1.setBorderPainted(false);
  add(button1);

  button2 = new JButton();
  button2.setSize(300, 200);
  button2.setLocation(600,150);
  button2.setFont(new Font("Serif", Font.BOLD, 18));
  button2.setText("<html><center>"+"If ISIS wants to fight, fine with us. "+"<br>"+"We have wanted that fight for a long time. There is no room in the world for ISIS any more."+"<br>"+"The Muslims or us,  one of us will have to go."+"</center></html>");
  button2.addActionListener(new B2Listener());
  button2.setBorder(null);
  button2.setOpaque(false);
  button2.setContentAreaFilled(false);
  button2.setBorderPainted(false);
  add(button2);

  ActionListener counter = 
     new ActionListener() 
     {
        public void actionPerformed(ActionEvent evt) 
        { 
           repaint();
           x--;
           if (x == 0)
           {
              timer.stop();  
           }
        }
     };
  timer = new Timer(delay, counter);
  timer.start();


  setFocusable(true);  
}


public void paintComponent(Graphics g)
{
  ImageIcon Nintendo = new ImageIcon("trump speech.jpg");

  g.drawImage(Nintendo.getImage(), 0, 0, 1000, 1000, null);

  ImageIcon N = new ImageIcon("happy.JPG");

  g.setColor(Color.WHITE);
  g.fillOval(90,100,320,320);

  g.setColor(Color.WHITE);
  g.fillOval(590,100,320,320);

  g.setColor(Color.WHITE);
  g.setFont(new Font("Serif",Font.BOLD, 50));
  g.drawString(""+x,500,50);

  if (drawWin)
  {
     g.drawImage(N.getImage(), 0, 0, 1000, 1000, null); 
  }
  ImageIcon L = new ImageIcon("loser.JPG");
  if (drawLose)
  {
     g.drawImage(L.getImage(), 0, 0, 1000, 1000, null); 
  }

}

private class B1Listener implements ActionListener
{
  public void actionPerformed(ActionEvent e)
  {
     repaint();
     drawWin = true;

     label1.setLocation(250,650);
     label1.setSize(1000, 400);
     label1.setForeground(new Color(212, 175, 55));
     label1.setFont(new Font("Serif", Font.BOLD, 100));
     add(label1);

     button1.setEnabled(false);
     button2.setEnabled(false);

     button1.setText("");
     button2.setText("");

     timer.stop();

  }
 }

   private class B2Listener implements ActionListener
  {
  public void actionPerformed(ActionEvent e)
  {
     repaint();
     drawLose = true;

     label2.setLocation(500,700);
     label2.setSize(400, 400);
     label2.setForeground(Color.RED);
     label2.setFont(new Font("Serif", Font.BOLD, 40));
     add(label2);

     button1.setEnabled(false);
     button2.setEnabled(false);

     button1.setText("");
     button2.setText("");


     timer.stop();
  }
 }
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.awt.Graphics;
导入java.awt.Image;
导入java.awt.image.buffereImage;
导入javax.swing.Timer;
公共类Panel00扩展了JPanel
{
私有缓冲区映像myImage;
专用图形缓冲区;
公共定时器;
公共按钮1;
公共按钮2;
publicjlabel1=新JLabel(“好选择!”);
public JLabel label2=新JLabel(“你被解雇了!!”);
public int x=5;//从5开始倒计时
public int delay=1000;//毫秒
布尔值drawWin=false;
布尔值drawLose=false;
公共小组讨论会00()
{
myImage=新的BuffereImage(1000,1000,BuffereImage.TYPE_INT_RGB);
myBuffer=myImage.getGraphics();
setLayout(空);
button1=新的JButton();
按钮1.设置大小(300200);
按钮1.设置位置(100150);
button1.setFont(新字体(“衬线”,Font.BOLD,18));
按钮1.setText(“+”除非我们能够确定和理解这个问题“+”
“+”及其所构成的危险威胁,否则我国不能成为恐怖袭击的受害者“+”
“+”这些袭击是由那些只相信圣战、对人的生命毫无理性或尊重的人实施的“+”); 按钮1.addActionListener(新的B1Listener()); 按钮1.设置订单(空); 按钮1.设置不透明(假); 按钮1.setContentAreaFilled(假); 按钮1.已涂漆(假); 添加(按钮1); button2=新的JButton(); 按钮2.设置大小(300200); 按钮2.设置位置(600150); 按钮2.setFont(新字体(“衬线”,Font.BOLD,18)); 按钮2.setText(“+”如果伊斯兰国想打仗,我们可以。”+“
”+“我们很久以来就想打仗了。世界上再也没有伊斯兰国的空间了。”+“
”+”穆斯林或我们中的一个将不得不离开。”+”; addActionListener(新的B2Listener()); 按钮2.设置订单(空); 按钮2.设置不透明(假); 按钮2.setContentAreaFilled(假); 按钮2.已涂漆(假); 添加(按钮2); ActionListener计数器= 新建ActionListener() { 已执行的公共无效操作(操作事件evt) { 重新油漆(); x--; 如果(x==0) { timer.stop(); } } }; 定时器=新定时器(延迟、计数器); timer.start(); 设置聚焦(真); } 公共组件(图形g) { ImageIcon任天堂=新ImageIcon(“trump speech.jpg”); g、 drawImage(任天堂.getImage(),0,0,1000,1000,null); ImageIcon N=新的ImageIcon(“happy.JPG”); g、 setColor(Color.WHITE); g、 圆角(90100320320); g、 setColor(Color.WHITE); g、 Fillova(590100320320); g、 setColor(Color.WHITE); g、 setFont(新字体(“衬线”,Font.BOLD,50)); g、 抽绳(“+x500,50”); 如果(drawWin) { g、 drawImage(N.getImage(),0,0,1000,1000,null); } ImageIcon L=新的ImageIcon(“loser.JPG”); 如果(抽离) { g、 drawImage(L.getImage(),0,0,1000,1000,null); } } 私有类B1Listener实现ActionListener { 已执行的公共无效操作(操作事件e) { 重新油漆(); drawWin=真; 标签1.设置位置(250650); 标签1.设置尺寸(1000400); 标签1.设置前景(新颜色(212、175、55)); label1.setFont(新字体(“衬线”,Font.BOLD,100)); 添加(标签1); 按钮1.设置启用(假); 按钮2.setEnabled(错误); 按钮1.setText(“”); 按钮2.setText(“”); timer.stop(); } } 私有类B2Listener实现ActionListener { 已执行的公共无效操作(操作事件e) { 重新油漆(); drawLose=真; 标签2.设置位置(500700); 标签2.设置尺寸(400400); 标签2.设置前景(颜色:红色); label2.setFont(新字体(“衬线”,Font.BOLD,40)); 添加(标签2); 按钮1.设置启用(假); 按钮2.setEnabled(错误); 按钮1.setText(“”); 按钮2.setText(“”); timer.stop(); } } }
建议:

  • 首先,使用CardLayout交换JPanel。您将需要另一个JPanel,一个使用CardLayout的JPanel,您将使用适当的字符串常量将上面的两个JPanel添加到第一个JPanel中,然后您可以随意交换JPanel。这本书会告诉你怎么做。我已经发布了一些代码,可以在一些
  • 代码中的其他严重问题—首先也是最重要的是,从不读取paintComponent方法中的图像或任何其他文件。这种方法主要决定了GUI的感知响应能力,您永远不想让它慢下来。但是,为什么要这样做呢?为什么不在图像中读取一次,将其存储在一个变量中,然后处理它呢
  • 还可以在覆盖中调用
    super.paintComponent(…)
    方法,否则可能无法去除GUI中的脏像素
  • 还要避免像瘟疫一样使用空布局。虽然空布局和
    setBounds()
    可能会像创建复杂GUI的最简单和最好的方式一样吸引新手,但您创建的GUI越多,在使用它们时遇到的困难就越严重。当GUI调整大小时,它们不会调整您的组件的大小,它们是一个需要增强或维护的皇家女巫,当它们放置在滚动窗格中时会完全失败,当在所有平台或屏幕分辨率与原始分辨率不同的情况下查看时,它们看起来非常糟糕

你能解释一下常数在新的C中的位置吗