简体   繁体   中英

Java Swing Moving Away From Null Layout

在此处输入图片说明

I built a great GUI using the frowned upon null layout (I defined a lot of constants and used a window resize listener to make it easy). Everything worked perfectly until I started using a new computer. Now, the component's are not positioned properly (from the picture you can see that the components are offset down and right). After researching the problem I learned that layout managers make sure that the components are positioned properly throughout different machines. Because of this, I would like to start rebuilding the GUI in an actual layout manager. The problem is that I often feel limited in the way I position components when attempting to use an actual layout manager.

For anyone who is curious, I was originally using a dell inspiron laptop with windows 10, and have moved to an Asus Laptop (I don't know the actual model, but the touch screen can detach from the keyboard), also with windows 10.

My question:

Which layout manager would be the fastest and easiest to build the GUI shown in the picture above (out of the stock Swing Layouts and others). I would like this layout to respect the components' actual sizes for only a few but not all of the components. Using this layout, how would I go about positioning the inventory button (the hammer at the bottom left) so that the bottom left corner of the inventory button is 5 pixels up and right from the bottom left corner of the container, even after resizing the container?

Thanks in advance. All help is appreciated.

EDIT: The "go find a key" and "Attempt to force the door open" options should have their sizes respected.

The simplest solution that comes to my mind is a BorderLayout for the main panel. Add the textarea to NORTH / PAGE_START . Make another BorderLayout containing the inventory button ( WEST / LINE_START ) and the location label ( EAST / LINE_END ). Add that to SOUTH / PAGE_END of the main BorderLayout . Then just add a BoxLayout with vertical alignment to the main BorderLayout 's CENTER containing the two buttons. Here's a tutorial for the standard layout managers.


在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Insets;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Example {

    public Example() {
        JTextArea textArea = new JTextArea("There is a locked door");
        textArea.setRows(5);
        textArea.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        textArea.setEditable(false);

        WhiteButton button1 = new WhiteButton("Go find a key") {
            @Override
            public Dimension getMinimumSize() {
                return new Dimension(200, 25);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 25);
            }

            @Override
            public Dimension getMaximumSize() {
                return new Dimension(200, 25);
            }
        };
        WhiteButton button2 = new WhiteButton("Attempt to force the door open");
        button2.setMargin(new Insets(0, 60, 0, 60));

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
        buttonPanel.add(button1);
        buttonPanel.add(Box.createVerticalStrut(5));
        buttonPanel.add(button2);

        WhiteButton inventoryButton = new WhiteButton(
                new ImageIcon(new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB)));

        JLabel locationLabel = new JLabel("Location: 0");
        locationLabel.setVerticalAlignment(JLabel.BOTTOM);

        JPanel southPanel = new JPanel(new BorderLayout());
        southPanel.add(inventoryButton, BorderLayout.WEST);
        southPanel.add(locationLabel, BorderLayout.EAST);

        JPanel mainPanel = new JPanel(new BorderLayout(0, 5));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        mainPanel.add(textArea, BorderLayout.NORTH);
        mainPanel.add(buttonPanel);
        mainPanel.add(southPanel, BorderLayout.SOUTH);

        JFrame frame = new JFrame("Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Example();
            }
        });
    }

    private class WhiteButton extends JButton {

        public WhiteButton() {
            setBackground(Color.WHITE);
        }

        public WhiteButton(String text) {
            this();
            setText(text);
        }

        public WhiteButton(ImageIcon icon) {
            this();
            setIcon(icon);
            setBorder(BorderFactory.createLineBorder(Color.GRAY));
        }

    }

}

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