简体   繁体   English

如何将JPanel放在另一个JPanel上?

[英]How do I put JPanel on another JPanel?

I am new to the java (and programming in general) and I am trying to make my very first program. 我是java的新手(以及一般的编程),我正在努力创建我的第一个程序。 I'm stuck on the same problem for about 5 hours now, so I've decided to ask for help. 我现在约5个小时就遇到了同样的问题,所以我决定寻求帮助。

Basically I'm trying to make a program (2d game) that has about 20 positions on the board. 基本上我正在尝试制作一个在电路板上有大约20个位置的程序(2d游戏)。 Each position is either blue (owned by player1), red(owned by player2) or black(not owned by anyone). 每个位置都是蓝色(由player1拥有),红色(由player2拥有)或黑色(不是任何人拥有)。

The way I'm going about this is in main I've put a method that calls setup game, and then a method that plays game. 我正在进行的方式主要是我提出了一种调用设置游戏的方法,然后是一种玩游戏的方法。 I am working on the setup game, basically all it does is it makes an object of class Background (extends JPanel , and overrides paintComponent() ) and 20 objects of class Position (extends JPanel, and overrides paintComponent() ). 我正在设置安装游戏,基本上所有它做的是它创建类Background的对象(扩展JPanel ,并覆盖paintComponent() )和类Position 20个对象(扩展JPanel,并覆盖paintComponent() )。

So far I'm stuck on putting those Position objects on top of Background object. 到目前为止,我一直坚持将这些Position对象放在Background对象之上。

When I do: 当我做:

Background background= new Background();
frame.getContentPane().add(background);
Position position1= new Position;
frame.getContentPane().add(position1);
frame.setVisible(true);

it shows only a circle and no background as I was hoping, if I first add position and then background, I only have background and no circle. 它只显示一个圆圈而没有我希望的背景,如果我先添加位置然后再添加背景,我只有背景而没有圆圈。

Anyway I'm new to the java and I am still having trouble founding my way around, however I've tried to search for solutions, and I've found many different solutions to this problem (such as adding position to background first, and then adding background to frame, etc.) but I couldn't make any of them to work. 无论如何,我是java的新手,我仍然无法找到自己的方式,但是我试图寻找解决方案,并且我已经找到了许多不同的解决方案来解决这个问题(例如首先向后台添加位置,以及然后添加背景到框架等)但我不能让它们中的任何一个工作。

I am aware that the way I am adding them both to frame is (very likely) completely wrong, but I wrote it that way so you would (hopefully) be sure that what I've wrote actually does show you that my code for each of those classes draws something on the screen. 我知道我将它们添加到帧中的方式(很可能)是完全错误的,但我这样编写它所以你(希望)确保我写的实际上确实向你显示我的每个代码这些类在屏幕上绘制了一些内容。

PS: I didn't copy my code here as most of variable and method names aren't in English so it's fairly hard to read, but if you still think its needed, I will add it. PS:我没有在这里复制我的代码,因为大多数变量和方法名称都不是英文的,所以它很难阅读,但如果你仍然认为它需要,我会添加它。 Also I'm sorry for my probably stupid question, but I'm kinda hitting a wall here and I've no idea what else to try. 另外,我很抱歉我的愚蠢问题,但我在这里碰壁,我不知道还有什么可以尝试的。

Basically I'm trying to make a program (2d game) that has about 20 positions on the board. 基本上我正在尝试制作一个在电路板上有大约20个位置的程序(2d游戏)。 Each position is either blue (owned by player1), red(owned by player2) or black(not owned by anyone). 每个位置都是蓝色(由player1拥有),红色(由player2拥有)或黑色(不是任何人拥有)。

  1. Painting in Swing by default never returns PreferredSize , is required to override getPreferedSize() 默认情况下, 在Swing中绘制永远不会返回PreferredSize ,需要覆盖getPreferedSize()

  2. JPanel has implemented FlowLayout in API, this LayoutManager accepting only PreferredSize came from JComponents added to this container JPanel在API中实现了FlowLayout ,这个只接受PreferredSize的LayoutManager来自添加到此容器的JComponents

  3. after am changes to post an SSCCE , short, runnable, compilable 在上午更改后发布SSCCE ,简短,可运行,可编译

Background background= new Background();
frame.getContentPane().add(background);
Position position1= new Position;
frame.getContentPane().add(position1);

A JFrame uses a BorderLayout by default. 默认情况下,JFrame使用BorderLayout。 Also by default when you add a component to a Container that uses a BorderLayout the comopnent is added to the CENTER. 此外,默认情况下,当您将组件添加到使用BorderLayout的Container时,comopnent将添加到CENTER中。 Only one comonent can be added to the CENTER so your Position comonent replaces the Background component. 只有一个组件可以添加到CENTER中,因此您的Position组件将替换Background组件。

You want to add the Position to the Background and then add the Background to the frame. 您想要将位置添加到背景,然后将背景添加到框架。 Something like: 就像是:

Background background= new Background();
Position position1= new Position;
background.add(position1);
frame.add(background);

Note: there is no need to uses getContentPane() when adding a component to the frame. 注意:向框架添加组件时不需要使用getContentPane()。

The root panel should be a JFrame with a Container class underneath. 根面板应该是一个JFrame,下面有一个Container类。 When you call someRoot.window.container = yourJPanel, that loads the JPanel as the main component view of the JFrame. 当您调用someRoot.window.container = yourJPanel时,它会将JPanel加载为JFrame的主要组件视图。 Note, a JFrame can only hold one JPanel but other JPanels can hold other JPanels. 请注意,JFrame只能容纳一个JPanel,但其他JPanel可以容纳其他JPanel。 Just as you add the initial JPanel to the JFRam, a JPanel's own container can be another JPanel. 就像您将初始JPanel添加到JFRam一样,JPanel自己的容器也可以是另一个JPanel。 Hope this helps. 希望这可以帮助。

Like this: 像这样:

JPanel temp = new JPAnel();
frame.getContentPane().add(temp);
temp.getContentPane().add(new JPanel());

After these additions, there is a command that is illuding me but you call on JFrame to get it to refresh in real time. 在这些添加之后,有一个命令可以解释我,但是你调用JFrame来让它实时刷新。 I think it is something like: 我认为它是这样的:

frame.validate();  //thanks @SMT

or something, 或者其他的东西,

Try using something like 尝试使用类似的东西

jPanelExampleName.validate();
jPanelExampleName.repaint();

after adding your JPanels. 添加JPanel后。

It sounds like you want to use one JFrame and attach JPanels to it. 听起来你想要使用一个JFrame并将JPanels附加到它。 This is how I personally would do it. 这就是我个人会这样做的。

Declare your JFrame and JPanels 声明你的JFrame和JPanels

JFrame frame1 = new JFrame( "App Name");
JPanel panel1 = new JPanel(); 
JPanel panel2 = new JPanel(); 
JPanel panel3 = new JPanel(); 
JPanel panel4 = new JPanel(); 

Set the Background (I'm using colors but you get the idea) 设置背景(我使用颜色,但你明白了)

panel1.setBackground(Color.orange);
panel2.setBackground(Color.orange);
panel3.setBackground(Color.orange);
panel4.setBackground(Color.orange);

Set your layout for the JFrame (I'm using BoxLayout not sure which would be best for you) You can find the best one for you and some sample code here. 设置JFrame的布局(我使用BoxLayout不确定哪个最适合你)你可以在这里找到最好的一个和一些示例代码 Also just set the default close operation. 也只需设置默认的关闭操作。

frame1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame1.setLayout( new BoxLayout( frame1.getContentPane(), BoxLayout.Y_AXIS ) );

Then Just attach your JPanels 然后只需附上您的JPanel

frame1.add( panel1);
frame1.add( panel2);
frame1.add( panel3);
frame1.add( panel4);
frame1.pack();
frame1.setVisible( true );

This will allow you to use the JPanels you created and then change the colors via other methods. 这将允许您使用您创建的JPanels,然后通过其他方法更改颜色。

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

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