java - 自动调整 Swing 组件的大小

标签 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); 
}

有人告诉我应该在面板上使用允许我执行此操作的布局,但我不知道要使用哪种布局以及如何将其正确应用到该程序。任何有关如何执行此操作的解决方案或建议将不胜感激。

最佳答案

从这些方面开始。它使用 GridLayout 来拉伸(stretch)组件的大小。使用 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();
    }
}

关于java - 自动调整 Swing 组件的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28929883/

相关文章:

java - 如何通过将 BoxLayout 更改为不同的 LayoutManager 来改进 GUI?

java - Servlet相对路径 "Path not found"

java - 在 MVC 中映射操作时要实现的最佳设计模式

java - Swing 中的同步

java - 从 vlcj 播放器数组中播放视频

java - 在 Swing GUI 中提供空白

java - 我的 JPanel 中的 GridBagLayout 不起作用

java - 如何使用 SQL 查询删除列与任何集合匹配的行?

Java 线程问题

java - 在 Swing 工作线程之外更改模型是否可以?