自动调整 Swing 组件的大小
Automatic resizing of Swing components
当我编译和 运行 我的程序时,我希望能够重新调整它的大小并使组件保持相同的比例因子。这意味着当框架展开时,组件也会展开,并保持原来的大小和间距比例。
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
将组件拉伸到合适的大小。使用 setMargin(Insets)
将按钮做得更大一些。为截图缩小了字号,所有数字调整到需要。
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();
}
}
当我编译和 运行 我的程序时,我希望能够重新调整它的大小并使组件保持相同的比例因子。这意味着当框架展开时,组件也会展开,并保持原来的大小和间距比例。
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
将组件拉伸到合适的大小。使用 setMargin(Insets)
将按钮做得更大一些。为截图缩小了字号,所有数字调整到需要。
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();
}
}