java .setBounds 不适用于 JLabel 和 JButton

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17264761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:29:56  来源:igfitidea点击:

.setBounds not working for JLabel and JButton

javaswingjbuttonjlabellayout-manager

提问by James Loughlin

I am trying to change the positioning of a JLabel and a JButton on my GUI. Even though I try to use .setBounds to change their locations; they both just appear in the top centre of the screen.

我正在尝试更改 GUI 上 JLabel 和 JButton 的位置。即使我尝试使用 .setBounds 来更改它们的位置;它们都只出现在屏幕的顶部中央。

import java.awt.color.*;
import java.awt.font.*;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.UIManager.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class yo implements MouseListener {

Image image;
JButton button = new JButton("Wassup");
JFrame frame = new JFrame();
JLabel heloo = new JLabel("yo");
JPanel panel = new JPanel()
{
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        ImageIcon i = new ImageIcon("hi.jpg");
        image = i.getImage();
        g.drawImage(image,150,150,null);
        g.drawString("Hello",100,100);
        g.drawString("Hi",50,50);
    }
};


public yo()
{
    frame.add(panel);
    frame.setTitle("Hello");
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    panel.add(heloo);
    panel.add(button);
    button.setBounds(200,100,200,100);
    heloo.setBounds(100,100,100,100);
    button.addMouseListener(this);
}

public void mouseClicked (MouseEvent event)
{
    heloo.setText(String.format("Clicked at %d,%d", event.getX(), event.getY()));
}
public void mouseEntered (MouseEvent Event){}
public void mouseExited (MouseEvent Event){}
public void mousePressed (MouseEvent Event){}
public void mouseReleased (MouseEvent Event){}

public static void main(String[] args)
{
    new yo();
}
}

I apologise about all of the imports, I don't really know which ones I need and which ones are just pointless.

我对所有的进口表示歉意,我真的不知道哪些是我需要的,哪些是毫无意义的。

Basically I would like some help on how to change the positioning of my components.

基本上我想要一些关于如何更改组件定位的帮助。

回答by camickr

Don't use setBounds() to set the size and location of a component.

不要使用 setBounds() 来设置组件的大小和位置。

Let the layout manager do its job. That is if fact what is happening. A JPanel uses a FlowLayout, so the components are being positioned based on the rules of the FlowLayout. You can change the FlowLayout to align components to the left if you want. Or you can use a different layout manager.

让布局管理器完成它的工作。那就是如果事实正在发生。JPanel 使用FlowLayout,因此组件是根据 FlowLayout 的规则定位的。如果需要,您可以更改 FlowLayout 以将组件向左对齐。或者您可以使用不同的布局管理器。

Read the Swing tutorial on Layout Managersto find other layout managers you can use.

阅读有关布局管理器的 Swing 教程以查找您可以使用的其他布局管理器。

回答by Ericson Willians

public yo() {
    frame.add(panel);
    frame.setTitle("Hello");
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    panel.setLayout(null);
    panel.add(heloo);
    panel.add(button);
    button.setBounds(200,100,200,100);
    heloo.setBounds(100,100,100,100);
    button.addMouseListener(this);
}

By setting the layout of the JPanel to null, it will be in the "Absolute Layout", and then you'll be able to set the position of the JLabel and JButton with setBounds().

通过将JPanel的布局设置为null,它将在“绝对布局”中,然后您就可以使用setBounds()设置JLabel和JButton的位置。