setSize 不影响按钮的大小

发布于 2024-09-06 22:04:13 字数 1894 浏览 4 评论 0原文

我有一个示例代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不气馁 2024-09-13 22:04:14

GridLayout 忽略按钮的首选大小。替换

JPanel players = new JPanel(new GridLayout(1, 3));

JPanel players = new JPanel(); // default FlowLayout

附录:这是一个让布局和默认首选尺寸完成工作的相关示例。通过指定尽可能少的约束,外观可以根据每个平台的外观进行调整。调用 pack() 时的感觉。

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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel players = new JPanel();
        players.add(new GameButton("Button 1"));
        players.add(new GameButton("Button 2"));
        players.add(new GameButton("Button 3"));
        players.setBackground(GameButton.color);

        JPanel game = new JPanel();
        game.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        game.setBackground(Color.green);
        game.setPreferredSize(new Dimension(600, 450));

        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        frame.setContentPane(content);
        content.add(players, BorderLayout.NORTH);
        content.add(game, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    private static class GameButton extends JButton {

        private static final Color color = Color.green.darker();
        private static final Font font = new Font("SanSerif", Font.BOLD, 20);
        private String name;

        public GameButton(String name) {
            super(name);
            this.setBackground(color);
            this.setFont(font);
        }
    }
}

GridLayout ignores the buttons' preferred size. Replace

JPanel players = new JPanel(new GridLayout(1, 3));

with

JPanel players = new JPanel(); // default FlowLayout

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.

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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel players = new JPanel();
        players.add(new GameButton("Button 1"));
        players.add(new GameButton("Button 2"));
        players.add(new GameButton("Button 3"));
        players.setBackground(GameButton.color);

        JPanel game = new JPanel();
        game.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        game.setBackground(Color.green);
        game.setPreferredSize(new Dimension(600, 450));

        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        frame.setContentPane(content);
        content.add(players, BorderLayout.NORTH);
        content.add(game, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    private static class GameButton extends JButton {

        private static final Color color = Color.green.darker();
        private static final Font font = new Font("SanSerif", Font.BOLD, 20);
        private String name;

        public GameButton(String name) {
            super(name);
            this.setBackground(color);
            this.setFont(font);
        }
    }
}
人│生佛魔见 2024-09-13 22:04:14
  1. GridLayout直接拉伸添加到其中的组件。我会使用其他布局,或者如果您确实想要 GridLayout,请查看下面的代码。

  2. 您还必须在按钮上使用 setPreferredSize! (见下文)

导入java.awt.*;

导入 javax.swing.*;
导入 javax.swing.border.BevelBorder;

公共类 AWT 扩展 JFrame {

公共静态无效主(字符串[] args){
最终 JFrame 框架 = new JFrame();
框架.setPreferredSize(新尺寸(600, 450));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架.setBackground(Color.green.darker());

按钮 btn_1 = new Button("按钮 1");
btn_1.setBackground(Color.green.darker());
btn_1.setPreferredSize(新维度(40, 100)); 

按钮 btn_2 = new Button("按钮 2");
btn_2.setBackground(Color.green.darker());
btn_2.setPreferredSize(新维度(40, 100)); 

按钮 btn_3 = new Button("按钮 3");
btn_3.setBackground(Color.green.darker());
btn_3.setPreferredSize(新维度(40, 100));    

GridLayout 布局 = new GridLayout(1, 3);
JPanel 玩家 = new JPanel(layout);
JPanel cell1 = new JPanel();
cell1.add(btn_1);
cell1.setBackground(Color.green.darker());
玩家.add(cell1);
JPanel cell2 = new JPanel();
cell2.add(btn_2);
cell2.setBackground(Color.green.darker());
玩家.add(cell2);
JPanel cell3 = new JPanel();
cell3.add(btn_3);
cell3.setBackground(Color.green.darker());
玩家.add(cell3);
players.setBackground(Color.green.darker());
players.setPreferredSize(new Dimension(450, 80));

JPanel 游戏 = new JPanel();
game.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
游戏.setBackground(Color.green);
游戏.setPreferredSize(新维度(600, 370));

JPanel 内容 = new JPanel();
content.setLayout(new BorderLayout());
框架.setContentPane(内容);
content.add(玩家,BorderLayout.NORTH);
content.add(游戏,BorderLayout.SOUTH);
框架.pack();
框架.setVisible(true);
}
}
  1. GridLayout stretches out the compoments added to them directly. I'd use another layout, or if you really want GridLayout checkout the code below.

  2. You have to use setPreferredSize on the buttons as well! (see below)

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.setPreferredSize(new Dimension(40, 100)); 

Button btn_2 = new Button("Button 2");
btn_2.setBackground(Color.green.darker());
btn_2.setPreferredSize(new Dimension(40, 100)); 

Button btn_3 = new Button("Button 3");
btn_3.setBackground(Color.green.darker());
btn_3.setPreferredSize(new Dimension(40, 100));    

GridLayout layout = new GridLayout(1, 3);
JPanel players = new JPanel(layout);
JPanel cell1 = new JPanel();
cell1.add(btn_1);
cell1.setBackground(Color.green.darker());
players.add(cell1);
JPanel cell2 = new JPanel();
cell2.add(btn_2);
cell2.setBackground(Color.green.darker());
players.add(cell2);
JPanel cell3 = new JPanel();
cell3.add(btn_3);
cell3.setBackground(Color.green.darker());
players.add(cell3);
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);
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文