恢复最小化窗口后,Java swing组件布局位置更改

问题描述 投票:-1回答:2

我正在开发一个使用JLabel显示信息的航班信息显示(FID)应用程序。我有不同的JPanels包含不同的标签。

我的问题是,当我启动窗口时,它会正确显示每个组件保持其所需的布局位置,但是当您尝试在最小化后恢复窗口时标签位置会发生变化(中心处的簇)。从而将所有标签聚集在一起。我真的需要解决这个问题。

以下是我的代码片段:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Departure extends javax.swing.JFrame {

    final private JPanel container = new JPanel();
    final private JPanel secPanel;
    final private JPanel imgPanel;
    final private JLabel logo;
    final private JLabel lagosLbl;
    final private JLabel londonLbl;
    final private JLabel newYorkLbl;
    final private JLabel lagosTime;
    final private JLabel londonTime;
    final private JLabel newYorkTime;
    final private JLabel departureLbl;
    final private JLabel airlineHd;
    final private JLabel flightHd;
    final private JLabel destHd;
    final private JLabel schedHd;
    final private JLabel gateHd;
    final private JLabel statusHd;
    final private JLabel footer;
    private JLabel footerLeft, footerMid, footerRight;


    private String todayDate, timeLocale, timeLondon, timeNewYork;

    Dimension sizeMain, sizeHeading, sizeBody, sizeLogo, sizeSec, screenSize;
    Insets insetsMain, insetsHeading, insetsBody, insetsLogo, insetsSec;

    public Departure(){

        super("DEPARTURES");

        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(0,0,screenSize.width, screenSize.height);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);      
        this.setVisible(true);



        container.setPreferredSize(new Dimension(screenSize.width, 
        screenSize.height));
        container.setBackground(new Color(53,86,163));
        this.getContentPane().add(container);

        Insets insets = this.getInsets();
        Dimension size = container.getPreferredSize();
        container.setBounds(0 + insets.left, 0 + insets.top,
        size.width + 5, size.height + 5);
        container.setLayout(null);


        ImageIcon icon = new ImageIcon(getClass().getResource("headerDe.jpg"));
        logo = new JLabel();
        logo.setIcon(new 
        ImageIcon(icon.getImage().getScaledInstance(screenSize.width, 200,
        java.awt.Image.SCALE_SMOOTH)));
        logo.setBackground(new Color(0,0,0));
        container.add(logo);

        insetsMain = container.getInsets();
        sizeMain = logo.getPreferredSize();
        logo.setBounds(0 + insetsMain.left, 0 + insetsMain.top,
        sizeMain.width + 5, sizeMain.height + 5);


        lagosLbl = new JLabel("LAGOS:");
        lagosLbl.setFont(new Font("Century Gothic",Font.BOLD, 40));
        lagosLbl.setForeground(new Color(0,0,140));
        logo.add(lagosLbl);

        insetsLogo = logo.getInsets();
        sizeLogo = lagosLbl.getPreferredSize();
        lagosLbl.setBounds(1550 + insetsLogo.left, 5 + insetsLogo.top,
        sizeLogo.width + 5, sizeLogo.height + 5);

        timeLocale = myTimeLocale();
        lagosTime = new JLabel(timeLocale);
        lagosTime.setFont(new Font("Century Gothic",Font.BOLD, 45));
        lagosTime.setForeground(new Color(0,0,140));
        logo.add(lagosTime);

        sizeLogo = lagosTime.getPreferredSize();
        lagosTime.setBounds(1750 + insetsLogo.left, 2 + insetsLogo.top,
        sizeLogo.width + 5, sizeLogo.height + 5);


        londonLbl = new JLabel("LONDON:");
        londonLbl.setFont(new Font("Century Gothic",Font.BOLD, 40));
        londonLbl.setForeground(new Color(0,0,140));
        logo.add(londonLbl);

        sizeLogo = londonLbl.getPreferredSize();
        londonLbl.setBounds(1515 + insetsLogo.left, 65 + insetsLogo.top,
        sizeLogo.width + 5, sizeLogo.height + 5);

        timeLondon = myTimeLondon();
        londonTime = new JLabel(timeLondon);
        londonTime.setFont(new Font("Century Gothic",Font.BOLD, 45));
        londonTime.setForeground(new Color(0,0,140));
        logo.add(londonTime);

        sizeLogo = londonTime.getPreferredSize();
        londonTime.setBounds(1750 + insetsLogo.left, 62 + insetsLogo.top,
        sizeLogo.width + 5, sizeLogo.height + 5);

        newYorkLbl = new JLabel("NEW YORK:");
        newYorkLbl.setFont(new Font("Century Gothic",Font.BOLD, 40));
        newYorkLbl.setForeground(new Color(0,0,140));
        logo.add(newYorkLbl);

        sizeLogo = newYorkLbl.getPreferredSize();
        newYorkLbl.setBounds(1485 + insetsLogo.left, 125 + insetsLogo.top,
        sizeLogo.width + 5, sizeLogo.height + 5);

        timeNewYork = myTimeNewYork();

        newYorkTime = new JLabel(timeNewYork);
        newYorkTime.setFont(new Font("Century Gothic",Font.BOLD, 45));
        newYorkTime.setForeground(new Color(0,0,140));
        myTimeNewYork();
        logo.add(newYorkTime);

        sizeLogo = newYorkTime.getPreferredSize();
        newYorkTime.setBounds(1750 + insetsLogo.left, 122 + insetsLogo.top,
        sizeLogo.width + 5, sizeLogo.height + 5);

        departureLbl = new JLabel("DEPARTURES");
        departureLbl.setFont(new Font("Century Gothic",Font.BOLD, 100));
        departureLbl.setForeground(new Color(53,86,163));
        logo.add(departureLbl);

        sizeLogo = departureLbl.getPreferredSize();
        departureLbl.setBounds(680 + insetsLogo.left, 50 + insetsLogo.top,
        sizeLogo.width + 5, sizeLogo.height + 5);


        secPanel = new JPanel();
        secPanel.setPreferredSize( new Dimension( screenSize.width, 
        (screenSize.width - 300) ) );
        secPanel.setBackground(new Color(255,255,255));
        container.add(secPanel);

        sizeMain = secPanel.getPreferredSize();
        secPanel.setBounds(0 + insetsMain.left, 200 + insetsMain.top,
        sizeMain.width + 5, sizeMain.height + 5);


        imgPanel = new JPanel();
        imgPanel.setPreferredSize( new Dimension( screenSize.width,45 ) );
        imgPanel.setBackground(new Color(0,0,140));
        secPanel.add(imgPanel);

        insetsHeading = secPanel.getInsets();
        sizeHeading = imgPanel.getPreferredSize();
        imgPanel.setBounds(0 + insetsHeading.left, 5 + insetsHeading.top,
        sizeHeading.width + 5, sizeHeading.height + 5);


        schedHd = new JLabel("STD");
        schedHd.setFont(new Font("Century Gothic",Font.BOLD, 28));
        schedHd.setForeground(new Color(255, 255, 255));
        imgPanel.add(schedHd);

        insetsHeading = imgPanel.getInsets();
        sizeHeading = schedHd.getPreferredSize();
        schedHd.setBounds(100 + insetsHeading.left, 5 + insetsHeading.top,
        sizeHeading.width + 5, sizeHeading.height + 5);

        airlineHd = new JLabel("AIRLINE");
        airlineHd.setFont(new Font("Century Gothic",Font.BOLD, 28));
        airlineHd.setForeground(new Color(255, 255, 255));
        imgPanel.add(airlineHd);

        sizeHeading = airlineHd.getPreferredSize();
        airlineHd.setBounds(350 + insetsHeading.left, 5 + insetsHeading.top,
        sizeHeading.width + 5, sizeHeading.height + 5);

        destHd = new JLabel("DESTINATION");
        destHd.setFont(new Font("Century Gothic",Font.BOLD, 28));
        destHd.setForeground(new Color(255, 255, 255));
        imgPanel.add(destHd);

        sizeHeading = destHd.getPreferredSize();
        destHd.setBounds(650 + insetsHeading.left, 5 + insetsHeading.top,
        sizeHeading.width + 5, sizeHeading.height + 5);

        flightHd = new JLabel("FLIGHT");
        flightHd.setFont(new Font("Century Gothic",Font.BOLD, 28));
        flightHd.setForeground(new Color(255, 255, 255));
        imgPanel.add(flightHd);   

        sizeHeading = flightHd.getPreferredSize();
        flightHd.setBounds(1020 + insetsHeading.left, 5 + insetsHeading.top,
        sizeHeading.width + 5, sizeHeading.height + 5);


        gateHd = new JLabel("GATE");
        gateHd.setFont(new Font("Century Gothic",Font.BOLD, 28));
        gateHd.setForeground(new Color(255, 255, 255));
        imgPanel.add(gateHd);

        sizeHeading = gateHd.getPreferredSize();
        gateHd.setBounds(1280 + insetsHeading.left, 5 + insetsHeading.top,
        sizeHeading.width + 5, sizeHeading.height + 5);

        statusHd = new JLabel("STATUS");
        statusHd.setFont(new Font("Century Gothic",Font.BOLD, 28));
        statusHd.setForeground(new Color(255, 255, 255));
        imgPanel.add(statusHd);

        sizeHeading = statusHd.getPreferredSize();
        statusHd.setBounds(1520 + insetsHeading.left, 5 + insetsHeading.top,
        sizeHeading.width + 5, sizeHeading.height + 5);

        ImageIcon foot = new ImageIcon(getClass().getResource("footer.jpg"));
        footer = new JLabel();
        footer.setIcon(new 
        ImageIcon(foot.getImage().getScaledInstance(screenSize.width, 70,
        java.awt.Image.SCALE_SMOOTH)));
        secPanel.add(footer);

        size = footer.getPreferredSize();
        footer.setBounds(0 + insets.left, 760 + insets.top,
        size.width + 5, size.height + 5);

        todayDate = dayOfWeek() + "    "+ myDate();
        footerLeft = new JLabel(todayDate);
        footerLeft.setFont(new Font("Century Gothic",Font.BOLD, 30));
        footerLeft.setForeground(new Color(255,255,255));
        footer.add(footerLeft);

        size = footerLeft.getPreferredSize();
        footerLeft.setBounds(15 + insets.left, 15 + insets.top,
        size.width + 5, size.height + 5);

        footerMid = new JLabel();
        //footerMid.setPreferredSize(new Dimension(120, 70));
        footerMid.setFont(new Font("Century Gothic",Font.BOLD, 30));
        footerMid.setForeground(new Color(255,255,255));
        footer.add(footerMid);

        size = footerMid.getPreferredSize();
        footerMid.setBounds(450 + insets.left, 15 + insets.top,
        size.width + 5, size.height + 5);

        footerRight = new JLabel();
        footerRight.setFont(new Font("Century Gothic",Font.BOLD, 30));
        footerRight.setForeground(new Color(255,255,255));
        footer.add(footerRight);

        size = footerRight.getPreferredSize();
        footerRight.setBounds(1540 + insets.left, 15 + insets.top,
        size.width + 5, size.height + 5);


    }

    public String dayOfWeek(){
        Date now = new Date();

        SimpleDateFormat simpleDateformat = new SimpleDateFormat("EEEE"); // the 
        day of the week in full
        //SimpleDateFormat simpleDateformat = new SimpleDateFormat("E"); // the 
        day of the week abbreviated
        return simpleDateformat.format(now);
    }

    protected static String myDate(){
        Calendar myD = Calendar.getInstance();

        SimpleDateFormat d = new SimpleDateFormat("dd/MM/yyyy");
        String date = (d.format(myD.getTime()));

        return date;
    }

    public static void main(String args []){
        Departure departure = new Departure();
    }
}

java swing jframe layout-manager null-layout-manager
2个回答
0
投票

GridLayout上使用imgPanel并使用循环添加标签。确保使用SwingConstants.CENTER将标签文本居中。

标签将全部等距离,您不需要设置任何插入或边距。

List<String> labels = Arrays.asList("STD", "AIRLINE", "DESTINATION", "FLIGHT", "GATE", "STATUS");
imgPanel.setLayout(new GridLayout(1, labels.size())); // 1 row, 6 columns
labels.stream().forEach(s -> {
    JLabel label = new JLabel(s, SwingConstants.CENTER); // centered label text
    label.setFont(new Font("Century Gothic", Font.BOLD, 28));
    label.setForeground(new Color(255, 255, 255));
    imgPanel.add(label);
});

您也可以在开始添加标签之前设置布局,但这更麻烦而且不动态。

imgPanel.setLayout(new GridLayout(1, 6)); // Six labels tiled horizontally

并且不要忘记将标签文本居中。

schedHd = new JLabel("STD", SwingConstants.CENTER);
airlineHd = new JLabel("AIRLINE", SwingConstants.CENTER);
// etc...

0
投票

感谢所有回答我问题的人。我后来发现我没有明确定义我在向它们添加一些内容(标签)之前声明的每个容器(面板)的布局,因此当我恢复窗口时,维度往往会分散。虽然这可能很麻烦,但我确实喜欢绝对控制窗口中每个项目的位置。再次感谢

以下是我如何解决它的一个例子;

//After initializing the imgPanel

imgPanel.setLayout(null); //explicitly declare the imgPanel layout to null

JLabel lbl = new JLabel("STD");
imgPanel.add(lbl)

Insets imgPanelInsets = imgPanel.getInsets(); //define the components insets
Dimension sizeImg = lbl.getPreferredSize(); //define the components dimension
lbl.setBounds(100 + imgPanelInsets.left, 5 + imgPanelInsets.top,
             sizeImg.width + 5, sizeImg.height + 5);  

JLabel lbl2 = new JLabel("AIRLINE");
imgPanel.add(lbl2)

sizeImg = lbl2.getPreferredSize();
lbl2.setBounds(100 + imgPanelInsets.left, 5 + imgPanelInsets.top,
             sizeImg.width + 5, sizeImg.height + 5); 
© www.soinside.com 2019 - 2024. All rights reserved.