简体   繁体   中英

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. I am using the Window Builder Swing Designer Application window. 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. Specifically which JPanel are you trying to move?

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

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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