JPanel positions and sizes changes according to screensize

女生的网名这么多〃 提交于 2019-11-26 04:27:58

问题


The program runs with its size according to screen resolution or size of the computer. When I run it in a computer with specific size of its monitor it will change accordingly. My problem is the positioning and size of a JPanel, or any object inside the frame, to adopt on the change of screen size.

So whenever I will present my program on any monitor with different sizes the components will still be organized and placed as what I\'ve originally designed it. But here I\'m testing it with one JPanel named displayMenu. In which it displays a panel colored in green.

package saves.project;

import com.sun.awt.AWTUtilities;
import java.awt.*;
import java.awt.image.BufferedImage;

import java.io.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.BorderFactory;

import javax.imageio.ImageIO;

public class Homepage extends JFrame{

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Border grayline = BorderFactory.createLineBorder(Color.GRAY);;

int width = screenSize.width, height = screenSize.height;

public Homepage() throws IOException{

    super(\"Homepage\");
    displayMenu();
    displayBackground();

}

public static BufferedImage resize(BufferedImage image, int width, int height) {

    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
    Graphics2D g2d = (Graphics2D) bi.createGraphics();
    g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
    g2d.drawImage(image, 0, 0, width, height, null);
    g2d.dispose();
    return bi;

}

public void displayBackground() throws IOException{

    JPanel pBackground = new JPanel();
    pBackground.setSize(screenSize);
    pBackground.setLayout(new FlowLayout());
    add(pBackground);

    BufferedImage header = ImageIO.read(new File(\"res\\\\bg.jpg\"));
    BufferedImage resizedImage = resize(header,width,height);
    ImageIcon image = new ImageIcon(resizedImage);
    JLabel lblheader = new JLabel(image, JLabel.CENTER);
    pBackground.add(lblheader);

}

public void displayMenu() {

    JPanel pTitle = new JPanel();
    pTitle.setLayout(null);
    pTitle.setBounds(width/3, (height/2)+20, width/2, height/2);
    pTitle.setBackground(Color.GREEN);
    add(pTitle);

}

public void CreateAndShowGUI() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setUndecorated(true);
    setVisible(true);  

}

}

Also, it looks like my background was not totally spread throughout the frame. There is a white line on the top but the rest is the background. What should I gonna do? Thanks for help!


回答1:


Following up with @MadProgrammer comment:

You really need to learn how to use Layout Mangers. Setting sizes is not the way to go, as they will perform different on different machines.

One important thing to know about Layout Managers are which layouts respect the preferred sizes of its internal components. The ones that do not respect the sizes, will stretch the components. Some layouts may not stretch their components, but will position them in a default location within the open space, when the main container is stretched.

To get desired results, it is also sometimes necessary to nest containers with different layouts, this taking advantage of two or more layouts.

I know this isn't really much of an answer to your question, but I thought you may still gain some insight to your problem, and how you can achieve what you are trying to, with the use of Layout Managers.

Below I just made up a quick example of the different natures of some of the main Layout Managers. You can play around with it. Note the main JFrame is using the default BorderLayout. I only explicitly set the layout as BorderLayout so you can see which layout causes the effect.

Also have a look at Laying out Components Withing a Container to learn more about how to use different Layout Managers. Avoid using the null layouts and trying to position everything yourself. Let the layouts do it for you, as Swing was built to used with Layout Managers.


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.*;

public class TestingLayoutManagers {

    private JPanel northFlowLayoutPanel;
    private JPanel southBorderLayoutPanel;
    private JPanel centerGridBagLayoutPanel;
    private JPanel westBoxLayoutPanel;
    private JPanel eastGridLayoutPanel;

    private final JButton northButton = new JButton("North Button");
    private final JButton southButton = new JButton("South Button");
    private final JButton centerButton = new JButton("Center Button");
    private final JButton eastButton = new JButton("East Button");

    private final JButton menuButton1 = new JButton("Menu Item 1");
    private final JButton menuButton2 = new JButton("Menu Item 2");
    private final JButton menuButton3 = new JButton("Menu Item 3");
    private final JButton menuButton4 = new JButton("Menu Item 4");
    private final JButton menuButton5 = new JButton("Menu Item 5");

    public TestingLayoutManagers() {
        northFlowLayoutPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        southBorderLayoutPanel = new JPanel(new BorderLayout());
        centerGridBagLayoutPanel = new JPanel(new GridBagLayout());
        eastGridLayoutPanel = new JPanel(new GridLayout(1, 1));
        Box box = Box.createVerticalBox();
        westBoxLayoutPanel = new JPanel();

        northFlowLayoutPanel.add(northButton);
        northFlowLayoutPanel.setBorder(BorderFactory.createTitledBorder("Flow Layout"));

        southBorderLayoutPanel.add(southButton);
        southBorderLayoutPanel.setBorder(BorderFactory.createTitledBorder("Border Layout"));

        centerGridBagLayoutPanel.add(centerButton);
        centerGridBagLayoutPanel.setBorder(BorderFactory.createTitledBorder("GridBag Layout"));

        eastGridLayoutPanel.add(eastButton);
        eastGridLayoutPanel.setBorder(BorderFactory.createTitledBorder("Grid Layout"));

        box.add(menuButton1);
        box.add(menuButton2);
        box.add(menuButton3);
        box.add(menuButton4);
        box.add(menuButton5);
        westBoxLayoutPanel.add(box);
        westBoxLayoutPanel.setBorder(BorderFactory.createTitledBorder("Box Layout"));

        JFrame frame = new JFrame("Test Layout Managers");
        frame.setLayout(new BorderLayout());      // This is the deafault layout
        frame.add(northFlowLayoutPanel, BorderLayout.PAGE_START);
        frame.add(southBorderLayoutPanel, BorderLayout.PAGE_END);
        frame.add(centerGridBagLayoutPanel, BorderLayout.CENTER);
        frame.add(eastGridLayoutPanel, BorderLayout.LINE_END);
        frame.add(westBoxLayoutPanel, BorderLayout.LINE_START);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(
                        UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException
                    | InstantiationException
                    | IllegalAccessException
                    | UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }

            new TestingLayoutManagers();
        });
    }
}


来源:https://stackoverflow.com/questions/21375255/jpanel-positions-and-sizes-changes-according-to-screensize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!