【问题标题】:How to get a Panel to work inside another Panel?如何让一个面板在另一个面板内工作?
【发布时间】:2016-07-01 21:06:14
【问题描述】:

对于我正在制作的游戏,我希望玩家从菜单屏幕 (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();
  }
 }
}

【问题讨论】:

    标签: java image swing timer paintcomponent


    【解决方案1】:

    建议:

    • 首先,使用 CardLayout 交换 JPanel。您将需要另一个 JPanel,一个使用 CardLayout 的 JPanel,您将使用适当的字符串 constant 将上面的两个 JPanel 添加到第一个 JPanel,然后您可以随意交换 JPanel。 CardLayout Tutorial 将向您展示如何操作。我已经发布了一些可以在these links 中找到的代码。 Here's a nice one
    • 您的代码中存在其他严重问题——首先,从不在 paintComponent 方法中读取图像或任何其他文件。这种方法主要决定了您的 GUI 的感知响应能力,您永远不想减慢它的速度。但是,为什么要这样做?为什么不读取一次图像,将其存储在一个变量中,然后处理它?
    • 还要在您的覆盖中调用 super.paintComponent(...) 方法,否则您可能无法清除 GUI 中的脏像素。
    • 还要避免使用像瘟疫这样的空布局。虽然空布局和setBounds() 对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但创建的 Swing GUI 越多,使用它们时遇到的困难就越严重。当 GUI 调整大小时,它们不会调整您的组件大小,它们是增强或维护的皇家女巫,放置在滚动窗格中时它们完全失败,在与原始不同的所有平台或屏幕分辨率上查看时它们看起来很糟糕.

    【讨论】:

    • 您能否解释一下 Constant 在新 Card Layout JPanel 中的位置以及它的具体作用?
    • @JH97:首先阅读the tutorial,然后尝试解决它,如果仍然卡住,请显示您的代码和您的具体问题。
    • 我花了几个小时试图通过示例来理解卡片布局,但我就是不明白。我已经完成了 6 行代码,从那以后就被卡住了。我理解目标是什么,以及它应该如何完成,但我不知道如何将它应用到我正在尝试做的事情中,即使有你的帮助。所有的例子似乎都与我的不同,并且使用了很多我不熟悉的术语。我相信我会及时了解这一切,但就目前而言,这只是让我觉得自己像个白痴,让我感到困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    相关资源
    最近更新 更多