setSize 不影响按钮的大小
我有一个示例代码:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class AWT extends JFrame {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(600, 450));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.green.darker());
Button btn_1 = new Button("Button 1");
btn_1.setBackground(Color.green.darker());
btn_1.setSize(40, 100);
Button btn_2 = new Button("Button 2");
btn_2.setBackground(Color.green.darker());
btn_2.setSize(40, 100);
Button btn_3 = new Button("Button 3");
btn_3.setBackground(Color.green.darker());
btn_3.setSize(40, 100);
JPanel players = new JPanel(new GridLayout(1, 3));
players.add(btn_1);
players.add(btn_2);
players.add(btn_3);
players.setBackground(Color.green.darker());
players.setPreferredSize(new Dimension(450, 80));
JPanel game = new JPanel();
game.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
game.setBackground(Color.green);
game.setPreferredSize(new Dimension(600, 370));
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
frame.setContentPane(content);
content.add(players, BorderLayout.NORTH);
content.add(game, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
结果给了我这样一个窗口:
替代文本http://img710.imageshack。 us/img710/1573/21672037.gif
,虽然它应该会产生类似这样的结果:
alt text http://img686.imageshack.us/img686/8948/44650673.gif
为什么它不以这种方式渲染?
I have a sample code :
import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class AWT extends JFrame {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(600, 450));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.green.darker());
Button btn_1 = new Button("Button 1");
btn_1.setBackground(Color.green.darker());
btn_1.setSize(40, 100);
Button btn_2 = new Button("Button 2");
btn_2.setBackground(Color.green.darker());
btn_2.setSize(40, 100);
Button btn_3 = new Button("Button 3");
btn_3.setBackground(Color.green.darker());
btn_3.setSize(40, 100);
JPanel players = new JPanel(new GridLayout(1, 3));
players.add(btn_1);
players.add(btn_2);
players.add(btn_3);
players.setBackground(Color.green.darker());
players.setPreferredSize(new Dimension(450, 80));
JPanel game = new JPanel();
game.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
game.setBackground(Color.green);
game.setPreferredSize(new Dimension(600, 370));
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
frame.setContentPane(content);
content.add(players, BorderLayout.NORTH);
content.add(game, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
This as a result gives me such a window :
alt text http://img710.imageshack.us/img710/1573/21672037.gif
, while it should result in something more like this :
alt text http://img686.imageshack.us/img686/8948/44650673.gif
Why it is not rendering in this way ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GridLayout
忽略按钮的首选大小。替换为
附录:这是一个让布局和默认首选尺寸完成工作的相关示例。通过指定尽可能少的约束,外观可以根据每个平台的外观进行调整。调用
pack()
时的感觉。GridLayout
ignores the buttons' preferred size. Replacewith
Addendum: Here's a related example of letting the layout and default preferred sizes do the work. By specifying as few constraints as possible, the appearance adjusts to each platform's look & feel when
pack()
is invoked.GridLayout直接拉伸添加到其中的组件。我会使用其他布局,或者如果您确实想要 GridLayout,请查看下面的代码。
您还必须在按钮上使用 setPreferredSize! (见下文)
GridLayout stretches out the compoments added to them directly. I'd use another layout, or if you really want GridLayout checkout the code below.
You have to use setPreferredSize on the buttons as well! (see below)