根据屏幕大小设置组件大小

时间:2016-03-26 03:27:15

标签: java swing layout-manager gridbaglayout

我在java中有一个GUI,我正在为Learn Java应用程序构建。我的JFrame中有一个JPanel,其GridBagLayout。 GUI看起来像:( [Brackets]是按钮)

--------------------------------------------
|          |                    |          |
|          |                    |          |
| Instr-   |    Coding Area     |  Console |
| uctions  |                    |          |
|          |                    |          |
|          |                    |          |
|----|-----|--------------------|          |
|[..]|[...]| [Run]              |          |
|----|-----|-----------|--------|          |
|[Lsn List]|[Previous] | [Next] |          |
|----------|-----------|--------|----------|

我可以通过对代码的preferredSize进行硬编码来正确运行代码,但这并不会根据用户的屏幕尺寸更改尺寸。我可以将JFrame的大小正确设置为用户的屏幕大小;但是当我使用JFrame大小的百分比来设置组件的大小时,我不能使用任何Insets而不会使组件溢出框架。

我该怎么做? (我觉得使用百分比不是最好的做法,而且我可能遗漏了GridBagLayout)。

这是我的Main.java。 (我试图删除任何无关紧要的东西,但我只能在没有破坏示例的情况下将其降低到~100行。)

import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
    public static Main gui;
    public static int height, width;
    public static String version;
    public static JPanel panel;
    private GridBagConstraints c;
    private static final long serialVersionUID = 1L;
    public Main() {
        initVars();
        initUI();
    }
    private void initVars() {
        Toolkit toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        Insets scnMax = toolkit.getScreenInsets(getGraphicsConfiguration()); // screen insets
        int heightOver = scnMax.bottom + scnMax.top; // <-- these variables make sure not to include taskbar in the window size
        int widthOver = scnMax.left + scnMax.right; // <---
        width = (int)size.getWidth() - widthOver;
        height = (int)size.getHeight() - heightOver;
        panel = new JPanel();
    }
    private void initUI() {
        getContentPane().add(panel);
        pack();
        setSize(width,height);
        panel.setLayout(new GridBagLayout());
    /* code area */
        JEditorPane codeArea = new JEditorPane();
        JScrollPane codeScroll = new JScrollPane(codeArea);
        codeScroll.setPreferredSize(pDim(0.4f,0.7f));
        c = getLocation(2,0,3,1);
        panel.add(codeScroll,c);
    /* learn/instructions area */
        JTextArea instructions = new JTextArea("Instructions");
        instructions.setEditable(false);
        instructions.setPreferredSize(pDim(0.3f,0.7f));
        c = getLocation(0,0,2,1);
        panel.add(instructions, c);
    /* console */
        JTextArea console = new JTextArea("Console");
        console.setEditable(false);
        console.setPreferredSize(pDim(0.3f,0.95f));
        c = getLocation(5,0,1,3);
        panel.add(console, c);
    /* buttons */
        Dimension navigationSize = new Dimension(110,50);
        int both = GridBagConstraints.BOTH;

        JButton btnLearn = new JButton("Learn");
        c = getLocation(0,1,1,1);
        c.fill = both;
        panel.add(btnLearn, c);

        JButton btnInstructions = new JButton("Instructions");
        c = getLocation(1,1,1,1);
        c.fill = both;
        panel.add(btnInstructions, c);

        JButton btnRun = new JButton("Run");
        btnRun.setPreferredSize(navigationSize);
        c = getLocation(2,1,1,1);
        c.fill = both;
        panel.add(btnRun, c);

        JButton btnPrevious = new JButton("Previous");
        btnPrevious.setPreferredSize(navigationSize);
        c = getLocation(2,2,2,1);
        c.fill = both;
        panel.add(btnPrevious, c);

        JButton btnNext = new JButton("Next");
        btnNext.setPreferredSize(navigationSize);
        c = getLocation(4,2,1,1);
        c.fill = both;
        panel.add(btnNext, c);

        JButton btnLessonList = new JButton("Lesson List");
        c = getLocation(0,2,2,1);
        c.fill = both;
        panel.add(btnLessonList,c);
    }
    private GridBagConstraints getLocation(int x, int y, int width, int height) {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = x; c.gridy = y; c.gridwidth = width; c.gridheight = height;
        return c;
    }
    private int pX(float percent) {return (int)(percent*width);}
    private int pY(float percent) {return (int)(percent*height);}
    private Dimension pDim(float x, float y) {
        return new Dimension(pX(x),pY(y));
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Main.gui = new Main();
                gui.setVisible(true);
            }
        });
    }
}

0 个答案:

没有答案