简体   繁体   English

JPanel未显示在我的JFrame中,但显示在新窗口中

[英]JPanel not being Displayed in my JFrame, but in new Window

I believe I am doing something simple wrong; 我相信我在做一些简单的错误; and my research has got me no where. 我的研究使我无处可去。

I created a stand alone Java application, that I am trying to convert to run as an applet. 我创建了一个独立的Java应用程序,试图将其转换为作为applet运行。 (Being an applet is a requirement). (必须是applet)。

I created a JPanel class MazePanel,and I am trying to display it as an applet. 我创建了一个JPanel类MazePanel,并且试图将其显示为applet。 When I run my Applet in Netbeans, 2 windows load. 在Netbeans中运行Applet时,将加载2个窗口。 One is a blank Applet window, while the other is my Maze. 一个是空白的Applet窗口,另一个是我的迷宫。 I want the Maze (which is implemented in MazePanel), to display in my applet. 我希望迷宫(在MazePanel中实现)显示在我的applet中。 Can anyone take a look at this, and tell me what I am doing wrong. 谁能看看这个,告诉我我做错了什么。

public class MazeApplet extends JApplet {


protected JFrame frame;
protected JPanel jPanel;
protected MazeBuilder builder;
protected MazeFactory factory;

public MazeApplet() {
    //Set up frame
    frame = new JFrame("Maze -- Builder");
    frame.setLayout(new BorderLayout());

    frame.setVisible(true);
    frame.setResizable((true));

}

public void init() {

    factory = new ShoutMazeFactory();
    builder = new FactoryMazeBuilder(factory);
    Maze maze = MazeGameBuilder.createMaze(builder);
    maze.setCurrentRoom(1);
    jPanel = (new Maze.MazePanel(maze));
//Add jPanel to Frame
    frame.getContentPane().add(jPanel);
    frame.pack();
    }
}

Below is the JPanel Class I am trying to display 以下是我要显示的JPanel类

public class MazePanel extends JPanel{

protected Maze maze = new Maze();
  public void paint(Graphics g) {
Dimension dim = getSize(); 
g.setColor(Color.white);
g.fillRect(0, 0, dim.width, dim.height);    
g.setColor(Color.black);
  maze.draw(g);
  }    
}

While working in an applet you can't create a new frame which is the reason for the creation of the frame. 在applet中工作时,您无法创建新框架,这就是创建框架的原因。

Start you initial panel in the init() block and then make panels visible or invisible depending on the need. 在init()块中启动初始面板,然后根据需要使面板可见或不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM