自动调整Swing组件的大小

时间:2015-03-08 17:57:28

标签: java swing layout-manager

当我编译并运行我的程序时,我希望能够重新调整它的大小并保持组件具有相同的比例因子。这意味着当框架展开时,组件也会扩展,保持尺寸和间距比例与原始尺寸一致。

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.lang.*;

public class ResizeTst
{
JFrame myMainWindow = new JFrame("This is my title");

JPanel  firstPanel = new JPanel();

//Components
JButton CompAth = new JButton();
JButton ViewAth = new JButton();
JButton UpdateRD = new JButton();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String []fontFamilies = ge.getAvailableFontFamilyNames();
Font FontT5 = new Font("SansSerif", Font.BOLD, 50);
///////////

public void runGUI()
{
    myMainWindow.setBounds(10, 10, 1296, 756);
    myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    myMainWindow.setLayout(new GridLayout(1,1));

    createFirstPanel();

    myMainWindow.getContentPane().add(firstPanel);

    myMainWindow.setVisible(true); 
}

public void createFirstPanel()
{
    firstPanel.setLayout(null);

    CompAth.setLocation(500,250);
    CompAth.setSize(320,300);
    CompAth.setText("<html><CENTER>Compare<br>Athletes</CENTER></html>");
    CompAth.setFont(FontT5);
    firstPanel.add(CompAth);

    ViewAth.setLocation(100,250);
    ViewAth.setSize(320,300);
    ViewAth.setText("<html><CENTER>View<br>Athletes</CENTER></html>");
    ViewAth.setFont(FontT5);
    firstPanel.add(ViewAth);

    UpdateRD.setLocation(900,250);
    UpdateRD.setSize(320,300);
    UpdateRD.setText("<html><CENTER>Update<br>Running<br>Details</CENTER></html>");
    UpdateRD.setFont(FontT5);
    firstPanel.add(UpdateRD);
}

public static void main(String[] args)
{
    ResizeTst rt = new ResizeTst();
    rt.runGUI();
}
}

因此,如果我实现了这个代码,它将允许我自动在任何系统上以全尺寸运行它,而不会使组件的大小在比例因子上变化到原来的大小。允许我的程序在全屏幕上在较大或较小的屏幕上看起来不好看。

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.lang.*;

public class ResizeTst
{
JFrame myMainWindow = new JFrame("This is my title");

JPanel  firstPanel = new JPanel();

//Components
JButton CompAth = new JButton();
JButton ViewAth = new JButton();
JButton UpdateRD = new JButton();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String []fontFamilies = ge.getAvailableFontFamilyNames();
Font FontT5 = new Font("SansSerif", Font.BOLD, 50);
///////////
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

public void runGUI()
{
    myMainWindow.setExtendedState(JFrame.MAXIMIZED_BOTH);
    myMainWindow.setSize(screenSize);
    myMainWindow.setVisible(true);    
    myMainWindow.setResizable(true);
    myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    myMainWindow.setLayout(new GridLayout(1,1));

    createFirstPanel();

    myMainWindow.getContentPane().add(firstPanel);

    myMainWindow.setVisible(true); 
}

我被告知我应该使用面板上的布局来允许我这样做,但我不知道要使用哪种布局以及如何正确地将它应用于此程序。任何有关如何做到这一点的解决方案或建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

从这些方面开始。它使用GridLayout来扩展组件的大小。使用setMargin(Insets)使按钮更大一些。屏幕截图的字体大小减小,调整所需的所有数字。

enter image description here

import javax.swing.*;
import java.awt.*;
import javax.swing.border.EmptyBorder;

public class ResizeTst {

    JFrame myMainWindow = new JFrame("This is my title");
    JPanel firstPanel = new JPanel();

//Components
    JButton compAth = new JButton();
    JButton viewAth = new JButton();
    JButton updateRD = new JButton();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String[] fontFamilies = ge.getAvailableFontFamilyNames();
    Font fontT5 = new Font(Font.SANS_SERIF, Font.BOLD, 25);
///////////

    public void runGUI() {
        myMainWindow.setBounds(10, 10, 1296, 756); // don't guess the size (1)
        myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        myMainWindow.setLayout(new GridLayout(1, 1));
        createFirstPanel();
        myMainWindow.getContentPane().add(firstPanel);
        myMainWindow.pack(); // 1) Make it mininum size needed
        myMainWindow.setMinimumSize(myMainWindow.getSize());
        myMainWindow.setVisible(true);
    }

    public void createFirstPanel() {
        //firstPanel.setLayout(null);
        firstPanel.setLayout(new GridLayout(1,0,50,50));
        firstPanel.setBorder(new EmptyBorder(50,50,50,50));
        Insets buttonMargin = new Insets(20, 20, 20, 20);

        compAth.setText("<html><CENTER>Compare<br>Athletes</CENTER></html>");
        compAth.setMargin(buttonMargin);
        compAth.setFont(fontT5);
        firstPanel.add(compAth);

        viewAth.setMargin(buttonMargin);
        viewAth.setText("<html><CENTER>View<br>Athletes</CENTER></html>");
        viewAth.setFont(fontT5);
        firstPanel.add(viewAth);

        updateRD.setMargin(buttonMargin);
        updateRD.setText("<html><CENTER>Update<br>Running<br>Details</CENTER></html>");
        updateRD.setFont(fontT5);
        firstPanel.add(updateRD);
    }

    public static void main(String[] args) {
        // should be on the EDT!
        ResizeTst rt = new ResizeTst();
        rt.runGUI();
    }
}