简体   繁体   English

无法将JPanel中的JLabel组件与其所在的JPanel的左侧对齐

[英]Unable to align JLabel component in JPanel to the left of the JPanel it is in

No matter how I set my Bounds for my JLabel, it is always in the same spot. 无论我如何为JLabel设置界限,它始终位于同一位置。 I am using the Window Builder Swing Designer Application window. 我正在使用Window Builder Swing Designer应用程序窗口。 Here is my code: 这是我的代码:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;

public class Restaurants {

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Restaurants window = new Restaurants();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
     public Restaurants() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(0, 0, 1368, 689);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JPanel panel = new JPanel();
    panel.setBorder(new LineBorder(new Color(0, 0, 0), 12));
    panel.setBounds(10, 143, 572, 258);
    frame.getContentPane().add(panel);

    JLabel label= new JLabel("Chicken Burger Meal");
    label.setFont(new Font("Tahoma", Font.PLAIN, 18));
    label.setBounds(21,70,173,29);
    panel.add(label);

    JLabel lblChickenBurger = new JLabel("Chicken Burger");
    lblChickenBurger.setFont(new Font("Tahoma", Font.PLAIN, 18));
    lblChickenBurger.setBounds(21,22,173,29);
    panel.add(lblChickenBurger);

    JPanel panel_1 = new JPanel();
    panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 12));
    panel_1.setBounds(592, 143, 401, 259);
    frame.getContentPane().add(panel_1);

    JPanel panel_2=new JPanel();
    panel_2.setBorder(new LineBorder(new Color(0,0,0), 12));
    panel_2.setBounds(10,412,572,228);
    frame.getContentPane().add(panel_2);

    JPanel panel_3=new JPanel();
    panel_3.setBorder(new LineBorder(new Color(0,0,0),12));
    panel_3.setBounds(592,411,401,228);
    frame.getContentPane().add(panel_3);

    JPanel panel_4 = new JPanel();
    panel_4.setBorder(new LineBorder(new Color(0, 0, 0), 12));
    panel_4.setBounds(1003, 174, 401, 465);
    frame.getContentPane().add(panel_4);

    JLabel lblNewLabel = new JLabel("Loan Management System");
    lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 60));
    lblNewLabel.setBounds(160, 49, 849, 96);
    frame.getContentPane().add(lblNewLabel);
}

} }

What do you think is wrong? 你觉得错什么? I have sorted through all possible strategies that I can think of , but none of the strategies seems to yield any type of viable solution. 我已经整理了所有可以想到的可能策略,但是这些策略似乎都无法提供任何可行的解决方案。

Changing the arguments passed into the setBounds() method seems to be working for me with your code. 更改传递给setBounds()方法的参数似乎对我的代码有用。 Specifically which JPanel are you trying to move? 具体来说,您要迁移哪个JPanel?

I would try looking at the GridLayout layout manager for what you're trying to do: https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html 我将尝试查看GridLayout布局管理器以了解您要执行的操作: https : //docs.oracle.com/javase/tutorial/uiswing/layout/grid.html

The easiest and most appropriate way is to NOT use absolute positioning - use the appropriate layout manager(s). 最简单,最合适的方法是不使用绝对定位-使用合适的布局管理器。 Attempting to use absolute positioning is fraught with problem, especially if you port to a different platform that has a different screen size, or pixel size, or default font, or any of dozens of other things. 尝试使用绝对定位会遇到很多问题,尤其是当您移植到具有不同屏幕大小,像素大小,默认字体或其他数十种内容的其他平台时。 And changing font or label strings, or adding/removing components, will be a nightmare. 更改字体或标签字符串,或添加/删除组件将是一场噩梦。

JPanel itsepf does not pay any attention to a component's size of (x,y) - it relies on a layout manager to do this. JPanel itsepf并不关注组件的大小(x,y)-它依赖于布局管理器来执行此操作。

If you insist on doing it with a null layout, You will have to create your own JPanel subclass with a null layout manager, and in the paintComponent() method place the components yourself (or alternatively, write your own LayoutManager to do this). 如果您坚持使用null布局执行此操作,则必须使用null布局管理器创建自己的JPanel子类,并在paintComponent()方法中自行放置组件(或者,编写自己的LayoutManager来执行此操作)。

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

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