setSize不影响按钮的大小

时间:2010-06-19 16:40:08

标签: java swing size awt

我有一个示例代码:

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);
    }
}

结果这给了我一个窗口:

alt text http://img710.imageshack.us/img710/1573/21672037.gif

,虽然它应该会产生更像这样的东西:

alt text http://img686.imageshack.us/img686/8948/44650673.gif

为什么不以这种方式渲染?

2 个答案:

答案 0 :(得分:6)

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);
        }
    }
}

答案 1 :(得分:2)

  1. GridLayout可以直接显示添加到它们中的组件。我会使用另一种布局,或者如果你真的想要GridLayout检查下面的代码。

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

  3. 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);
    }
    }