JAVA 设置 JFrame 的最大大小
JAVA Set Maximum Size of JFrame
我正在做一个关于 JAVA Swing 的项目。我在 JAVA 方面经验不多,需要你们的帮助。
我正在使用 frame.pack()
,因此它会根据需要采用最佳大小。我的 screenTwo
可以根据 ScreenOne
的输入而改变。我用过 frame.setMaximumSize(new Dimension(1300, 500))
,但它似乎不起作用。
如果框架变大,它会与任务栏重叠,我不希望这样,所以它应该比整个屏幕尺寸小一点。如何做到这一点?
编辑:
代码:
public class Test {
private static JFrame frame ;
private JRadioButton[] radioButton;
public static void main(String[] args) {
Test t = new Test();
t.go();
frame.pack();
frame.setMaximumSize(new Dimension(1300, 600));
frame.setResizable(false);
frame.setVisible(true);
Dimension si = frame.getContentPane().getSize();
System.out.println(si); //Output: java.awt.Dimension[width=934,height=751]
}
public void go()
{
frame = new JFrame();
JPanel panel1 = new JPanel();
frame.getContentPane().add(panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.setLayout(new BorderLayout(0, 20));
Font font = new Font("Times New Roman", Font.PLAIN, 20);
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 1));
panel2.add(new JLabel(" Flight ID"));
panel2.add(new JLabel(" Departure"));
panel2.add(new JLabel(" Arrival "));
panel2.add(new JLabel(" Transit Time "));
panel2.add(new JLabel("Total Duration "));
panel1.add(BorderLayout.NORTH, panel2);
JPanel panel3 = new JPanel();
panel3.setLayout(new GridLayout(0, 1));
GridBagConstraints c = new GridBagConstraints();
int comboSize = 15, i;
JPanel panelCombo[] = new JPanel[comboSize];
ButtonGroup group = new ButtonGroup();
radioButton = new JRadioButton[comboSize];
/***
Adding a bunch of components in here
***/
for(i=0; i<comboSize; i++) {
panelCombo[i] = new JPanel();
panelCombo[i].setLayout(new GridBagLayout());
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(0, 50, 0, 50);
panelCombo[i].add(new JLabel("Flight 1 ID"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Dept Time"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Arr Time"), c);
c.gridx++;
c.gridheight = 4;
panelCombo[i].add(new JLabel("Total time"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Duration"), c);
c.gridx++;
radioButton[i] = new JRadioButton();
panelCombo[i].add(radioButton[i], c);
group.add(radioButton[i]);
c.gridheight = 1;
c.gridx = 0;
c.gridy++;
panelCombo[i].add(new JLabel("Depar to Arrival"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Date"), c);
c.gridx = 0;
c.gridy++;
panelCombo[i].add(new JLabel("Flight 2 ID"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Dept Time"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Arr Time"), c);
c.gridx = 0;
c.gridy++;
panelCombo[i].add(new JLabel("Dept to Arrival"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Date"), c);
panelCombo[i].setBackground(Color.GREEN);
panel3.add(panelCombo[i]);
panelCombo[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
JScrollPane scroll = new JScrollPane(panel3);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel1.add(BorderLayout.CENTER, scroll);
}
}
首先,您 post 编写的所有代码是什么?您的问题是关于框架的首选和最大尺寸。框架的布局与问题无关。
我们对您的申请不感兴趣。我们只对演示问题的代码感兴趣。
例如:
JPanel panel = new JPanel();
panel.setPreferredSize( new Dimension(1400, 600) );
JFrame frame = new JFrame();
frame.add( panel );
frame.pack();
frame.setMaximumSize( new Dimension(1300, 500) );
frame.setVisible( true );
现在您将上面的代码放在 main() 方法中,并且您有代码可以演示您的问题。这就是您在论坛中 post 的内容,而不是您应用程序的核心转储。
If the frame gets bigger, it overlaps with the Taskbar, and I don't want that,
关于您的问题,您可以使用以下代码在桌面上获取 space:
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle bounds = env.getMaximumWindowBounds();
System.out.println("Screen Bounds: " + bounds );
在 pack() 之后添加上面的代码,然后添加如下内容:
int width = Math.min(frame.getWidth(), bounds.width);
int height = Math.min(frame.getHeight(), bounds.height);
frame.setSize( new Dimension(width, height) );
frame.setVisible(true);
我正在做一个关于 JAVA Swing 的项目。我在 JAVA 方面经验不多,需要你们的帮助。
我正在使用 frame.pack()
,因此它会根据需要采用最佳大小。我的 screenTwo
可以根据 ScreenOne
的输入而改变。我用过 frame.setMaximumSize(new Dimension(1300, 500))
,但它似乎不起作用。
如果框架变大,它会与任务栏重叠,我不希望这样,所以它应该比整个屏幕尺寸小一点。如何做到这一点?
编辑:
代码:
public class Test {
private static JFrame frame ;
private JRadioButton[] radioButton;
public static void main(String[] args) {
Test t = new Test();
t.go();
frame.pack();
frame.setMaximumSize(new Dimension(1300, 600));
frame.setResizable(false);
frame.setVisible(true);
Dimension si = frame.getContentPane().getSize();
System.out.println(si); //Output: java.awt.Dimension[width=934,height=751]
}
public void go()
{
frame = new JFrame();
JPanel panel1 = new JPanel();
frame.getContentPane().add(panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.setLayout(new BorderLayout(0, 20));
Font font = new Font("Times New Roman", Font.PLAIN, 20);
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 1));
panel2.add(new JLabel(" Flight ID"));
panel2.add(new JLabel(" Departure"));
panel2.add(new JLabel(" Arrival "));
panel2.add(new JLabel(" Transit Time "));
panel2.add(new JLabel("Total Duration "));
panel1.add(BorderLayout.NORTH, panel2);
JPanel panel3 = new JPanel();
panel3.setLayout(new GridLayout(0, 1));
GridBagConstraints c = new GridBagConstraints();
int comboSize = 15, i;
JPanel panelCombo[] = new JPanel[comboSize];
ButtonGroup group = new ButtonGroup();
radioButton = new JRadioButton[comboSize];
/***
Adding a bunch of components in here
***/
for(i=0; i<comboSize; i++) {
panelCombo[i] = new JPanel();
panelCombo[i].setLayout(new GridBagLayout());
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(0, 50, 0, 50);
panelCombo[i].add(new JLabel("Flight 1 ID"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Dept Time"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Arr Time"), c);
c.gridx++;
c.gridheight = 4;
panelCombo[i].add(new JLabel("Total time"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Duration"), c);
c.gridx++;
radioButton[i] = new JRadioButton();
panelCombo[i].add(radioButton[i], c);
group.add(radioButton[i]);
c.gridheight = 1;
c.gridx = 0;
c.gridy++;
panelCombo[i].add(new JLabel("Depar to Arrival"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Date"), c);
c.gridx = 0;
c.gridy++;
panelCombo[i].add(new JLabel("Flight 2 ID"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Dept Time"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Arr Time"), c);
c.gridx = 0;
c.gridy++;
panelCombo[i].add(new JLabel("Dept to Arrival"), c);
c.gridx++;
panelCombo[i].add(new JLabel("Date"), c);
panelCombo[i].setBackground(Color.GREEN);
panel3.add(panelCombo[i]);
panelCombo[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
JScrollPane scroll = new JScrollPane(panel3);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel1.add(BorderLayout.CENTER, scroll);
}
}
首先,您 post 编写的所有代码是什么?您的问题是关于框架的首选和最大尺寸。框架的布局与问题无关。
我们对您的申请不感兴趣。我们只对演示问题的代码感兴趣。
例如:
JPanel panel = new JPanel();
panel.setPreferredSize( new Dimension(1400, 600) );
JFrame frame = new JFrame();
frame.add( panel );
frame.pack();
frame.setMaximumSize( new Dimension(1300, 500) );
frame.setVisible( true );
现在您将上面的代码放在 main() 方法中,并且您有代码可以演示您的问题。这就是您在论坛中 post 的内容,而不是您应用程序的核心转储。
If the frame gets bigger, it overlaps with the Taskbar, and I don't want that,
关于您的问题,您可以使用以下代码在桌面上获取 space:
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle bounds = env.getMaximumWindowBounds();
System.out.println("Screen Bounds: " + bounds );
在 pack() 之后添加上面的代码,然后添加如下内容:
int width = Math.min(frame.getWidth(), bounds.width);
int height = Math.min(frame.getHeight(), bounds.height);
frame.setSize( new Dimension(width, height) );
frame.setVisible(true);