JFrame大小影响JButton大小,但只在垂直方向上?

huangapple go评论71阅读模式
英文:

JFrame size affecting JButton size but only vertically?

问题

I am confused about how JFrame extends the JButton vertically to match JFrame min size but not horizontally. I would like for it to not extend in either direction and know why it extends or doesn't.

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

public class somestring {
    public static void main(String []args){
		JFrame frame = new JFrame();
		Container content = frame.getContentPane();
		content.setLayout(new BorderLayout());
    	
    	Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension max = toolkit.getScreenSize();
		frame.setMinimumSize(new Dimension(1000,1000));
		frame.setSize(max);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JButton s = new JButton("generate somestring");
		s.setPreferredSize(new Dimension(400,400));
        content.add(s, BorderLayout.WEST);
		frame.pack();
		frame.setResizable(false);
		frame.setVisible(true);
    }
}
英文:

I am confused about how JFrame extends the JButton vertically to match JFrame min size but not horizontally. I would like for it to not extend in either direction and know why it extends or doesn't.

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

public class somestring {
    public static void main(String []args){
		JFrame frame = new JFrame();
		Container content = frame.getContentPane();
		content.setLayout(new BorderLayout());
		
    	
    	Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension max = toolkit.getScreenSize();
		frame.setMinimumSize(new Dimension(1000,1000));
		frame.setSize(max);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JButton s = new JButton("generate somestring");
		s.setPreferredSize(new Dimension(400,400));
        content.add(s, BorderLayout.WEST);
		frame.pack();
		frame.setResizable(false);
		frame.setVisible(true);
    }
}

答案1

得分: 1

I am confused about how JFrame extends the JButton vertically to match JFrame min size but not horizontally.

对于 JFrame 如何垂直扩展 JButton 以匹配 JFrame 的最小大小但不水平扩展,我感到困惑。

The default layout manager of the content pane of the frame is the BorderLayout, so the above is unnecessary.

帧的内容窗格的默认布局管理器是 BorderLayout,所以上述操作是不必要的。

In any case, the reason it extends vertically is because that is the rule of the BorderLayout when you add a component to the WEST.

无论如何,它垂直扩展的原因是当您将组件添加到 WEST 时,BorderLayout 的规则要求如此。

Read the section from the Swing tutorial on Layout Managers. The section on the BorderLayout will explain the rules for adding components to the WEST.

阅读 Swing 教程中关于布局管理器的部分。BorderLayout 部分将解释如何将组件添加到 WEST 的规则。

I would like for it to not extend in either direction

我希望它不要在任何方向上扩展。

Then you need to use a different layout manager.

那么,您需要使用不同的布局管理器。

For example you could use a FlowLayout. The button will be centered at the top of the frame.

例如,您可以使用 FlowLayout。按钮将位于帧的顶部居中显示。

Or you could use a GridBagLayout (with the default GridBagConstraints). The button will be centered both horizontally and vertically.

或者,您可以使用 GridBagLayout(使用默认的 GridBagConstraints)。按钮将在水平和垂直方向上都居中显示。

frame.pack();
frame.setResizable(false);

您应该在调用 pack() 之前设置 resizable 属性。

frame.setMinimumSize(new Dimension(1000,1000));
frame.setSize(max);

不确定您为何设置最小大小并调用 setSize()。如果您希望按钮以其首选大小显示,只需调用 pack()。

英文:

> I am confused about how JFrame extends the JButton vertically to match JFrame min size but not horizontally.

content.setLayout(new BorderLayout());
...
content.add(s, BorderLayout.WEST);

The default layout manager of the content pane of the frame is the BorderLayout, so the above is unnecessary.

In any case, the reason it extend vertically is because that is the rule of the BorderLayout when you add a component to the WEST.

Read the section from the Swing tutorial on Layout Managers. The section on the BorderLayout will explain the rules for adding components to the WEST.

> I would like for it to not extend in either direction

Then you need to use a different layout manager.

For example you could use a FlowLayout. The button will be centered at the top of the frame.

Or you could use a GridBagLayout (with the default GridBagConstraints). The button will be centered both horizontally and vertically.

frame.pack();
frame.setResizable(false);

You should set the resizable property BEFORE invoking pack().

frame.setMinimumSize(new Dimension(1000,1000));
frame.setSize(max);

Not sure why you are setting the minimum size and invoking setSize(). If you want the button to be displayed at its preferred size, then just invoke pack().

huangapple
  • 本文由 发表于 2020年8月12日 00:45:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63362700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定