[英]How can i add two components to my screen without them overlapping and only showing one?
Right now i can only have one of my two components show up on the screen the second one always just overlaps the first so the second is only showing. 现在,我只能在屏幕上显示我的两个组件中的一个,第二个组件总是与第一个组件重叠,因此第二个组件只能显示。
How can I get both of them to show up at the same time. 我怎样才能让他们同时出现。
package Game;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Amazing {
private final int width = 300;
private final int height = width / 16 * 9;
private int scale = 3;
private static Graphics g;
private static JFrame frame = new JFrame();
public Amazing(){
Dimension size = new Dimension(width*scale, height*scale);
frame.setPreferredSize(size);
frame.setResizable(false);
frame.setTitle("Amazing!");
Character character = new Character(50,50,scale,width,height);
Map map = new Map(scale, width, height);
frame.add(map);
frame.add(character);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
character.requestFocus();
}
public static void main(String[] args){
Amazing amazing = new Amazing();
}
}
I figured you don't need to see the other two classes(if you want i can also post them) but I think all you need to know about them are that the map class creates and paints with paintComponent(Graphics g)
2 randomly generated rectangles while the Character class creates and uses the paintComponent(Graphics g)
method to show and Image on the screen. 我认为您不需要查看其他两个类(如果您愿意,我也可以发布它们),但是我认为您需要了解的所有信息是地图类创建并使用
paintComponent(Graphics g)
2随机生成Character类创建并使用paintComponent(Graphics g)
方法在屏幕上显示和显示图像时显示矩形。
Since you didn't specify a layout, you were using BorderLayout which only displays 1 component in each section. 由于未指定布局,因此使用的是BorderLayout,该布局在每个节中仅显示1个组件。 Unless you specify a different location as an additional arg when using the default BorderLayout, the add(component) method will "overwrite" whatever was there previously.
除非使用默认的BorderLayout时将其他位置指定为附加arg,否则add(component)方法将“覆盖”以前的内容。 Therefore, when you add your second component to your frame it overwrites the first.
因此,当您将第二个组件添加到框架时,它将覆盖第一个组件。
I noticed that you imported GridLayout, but i don't see it used in your code. 我注意到您导入了GridLayout,但是我没有在您的代码中看到它。 If you add a statement in the constructor like:
如果在构造函数中添加如下语句:
this.setLayout(new GridLayout(4, 2)); this.setLayout(new GridLayout(4,2));
it will change the layout from the default BorderLayout to the GridLayout, and you will have 4 rows of 2 compartments each in which to place components. 它将布局从默认的BorderLayout更改为GridLayout,并且您将有4行,每行2个隔离专区,可在其中放置组件。 Calling the add(component) method will add a component in the next available section, starting with row 1 spot 1.
调用add(component)方法将在下一个可用部分中添加一个组件,从第1行第1点开始。
If you only want to add those two items, try putting in the statement 如果您只想添加这两项,请尝试添加以下语句
this.setLayout(new GridLayout(1, 2)); this.setLayout(new GridLayout(1,2));
before you add your components. 在添加组件之前。 This will place them side by side.
这将使它们并排放置。
When adding components to a panel in Swing you should choose a LayoutManager
for the panel ( FlowLayout
is simple, GridBoxLayout
flexible, but there are many more). 在Swing中向面板添加组件时,您应该为面板选择一个
LayoutManager
( FlowLayout
很简单, GridBoxLayout
灵活,但是还有更多)。 Depending on the layout chosen you also associate data with the component to tell the LayoutManager
where to place the component. 根据选择的布局,您还可以将数据与组件相关联,以告诉
LayoutManager
将组件放置在何处。
The "main" problem you're having is the fact that by default JFrame
uses a BorderLayout
. 您遇到的“主要”问题是默认情况下
JFrame
使用BorderLayout
。 BorderLayout
will only only a single component to occupy each of its five available positions. BorderLayout
将仅占据一个组件的五个可用位置中的每一个。 By default, BorderLayout
will place components at the CENTER
position. 默认情况下,
BorderLayout
将组件放置在CENTER
位置。
You should explore a different layout manager which meets your requirements. 您应该探索另一个满足您要求的布局管理器。
Take a look at Laying Out Components Within a Container 看看在容器中布置组件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.