简体   繁体   English

如何在屏幕上添加两个组件而又不重叠却只显示一个组件?

[英]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中向面板添加组件时,您应该为面板选择一个LayoutManagerFlowLayout很简单, 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.

相关问题 如何只用一个事件听我的所有组件? - How can I listen my all components with only one event? 如何绘制两个重叠的JPanel,以便在屏幕上显示两个显示的图形? - How Do I Draw Two Overlapping JPanels such that the Graphics Drawn On Both Of Them Display On The Screen? 如何将两个JButton与BorderLayout放置在PAGE_END而不重叠? - How do I position two JButtons at PAGE_END with BorderLayout without them overlapping? 如何添加两个包含十六进制数字的字符串而不将它们转换为 int? - How can I add two Strings containing hexadecimal-numbers without converting them to int? 如何添加标签而不使标签彼此重叠 - how to add labels without them overlapping each other GridBagLayout,放置组件时如何阻止屏幕拉伸? - GridBagLayout, how can I stop my screen from stretching when I'm placing my components? 使用NetBeans UI设计器,如何从一个项目复制UI组件并将其粘贴到另一个项目中? - Using NetBeans UI designer, how can I copy UI components from one project and paste them into another? 如何在不删除屏幕组件的情况下添加背景图像? 爪哇 - How to add a background image without removing components from screen? Java 如何将JButton添加到全屏Java程序 - How can I add a JButton to my full screen java program 当我仅在布局中声明一个按钮时,为什么在我的android应用程序中显示两个按钮 - Why are two buttons showing up in my android application when I only declared one in the layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM