如何为JButton提供首选大小?

时间:2014-07-06 10:30:18

标签: java swing

   import javax.swing.*;
   import java.awt.*;
   class MainGui{
         JFrame frame = new JFrame();
         JPanel mainPanel = new JPanel();
         JButton newBut = new JButton("New Game");
         JButton continueBut = new JButton("Continue");
         JButton exitBut = new JButton("Exit");
         JLabel backImage = new JLabel(new ImageIcon("C:\\Users\\BSK\\Desktop\\game5.jpg"));
         public MainGui(){
             frame.setSize(600,800);
             frame.setVisible(true);
             frame.setResizable(false);
             setButtonSize();
             frame.setLayout(new BorderLayout());
             frame.setContentPane(backImage);
             frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
             insertBlankArea(frame);
             frame.getContentPane().add(newBut);
             insertBlankArea(frame);
             frame.getContentPane().add(continueBut);
             insertBlankArea(frame);
             frame.getContentPane().add(exitBut);
             frame.setSize(799,800);

      }
      public void insertBlankArea(JFrame frame){
            frame.getContentPane().add(Box.createRigidArea(new Dimension(280,155)));
      }
      public void setButtonSize(){
            Dimension dim = new Dimension(100,100);//here is the problem,i am not getting the desired dimension and the size of buttons remains the default.
            newBut.setPreferredSize(dim);
            continueBut.setPreferredSize(dim);
            exitBut.setPreferredSize(dim);
      }

      public static void main(String[] args) {
            MainGui mainGui = new MainGui();
      }
  }

所以我没有得到按钮的定义尺寸,但是当我设置frame.setResizable(false);时,当我伸展屏幕时按钮的高度增加但宽度仍保持不变。
请告诉我出了什么问题? this is the output

2 个答案:

答案 0 :(得分:4)

您应该查看A Visual Guide to Layout Managers并根据您的情况选择最合适的一个。您还应该避免明确设置大小(例如:setSizesetMinimumSizesetMaximumSizesetPreferredSize),因为这些方法是布局管理器的责任。您可能还有兴趣阅读this question on whether or not the use of the different set size methods should be avoided or not

最后,您不应该在Event Dispatch Thread (EDT)之外调用MainGUI课程。大多数与Swing GUI相关的方法都不是线程安全的,因此需要在EDT中执行。以下是主要方法的更正版本:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            MainGui mainGui = new MainGui();
        }
    });
}

答案 1 :(得分:3)

只是阅读你的简短描述,我不知道你的问题是什么。但完全基于问题标题

  

“如何为JButton提供优先大小?”

别。让布局管理器为您处理。如果您想要更大的按钮,可以使用JButton.setMargins(Insets)和/或JButton.setFont(Font)指定更大的字体。

如果您希望拉伸按钮或拉伸,则需要选择适当的布局管理器,它将会或不会尊重按钮首选大小。例如,BorderLayout和GridLayout 将不会尊重首选大小,并将按钮拉伸,并且FlowLayout,BoxLayout和GridBagLayout 尊重首选大小。正如您所见here

enter image description here


请参阅GridBagLayout的示例

enter image description here

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

class MainGui {
    JFrame frame = new JFrame();
    JPanel mainPanel = new JPanel();
    JButton newBut = new JButton("New Game");
    JButton continueBut = new JButton("Continue");
    JButton exitBut = new JButton("Exit");
    JLabel backImage = new JLabel(new ImageIcon(
            getClass().getResource("images.jpg")));

    public MainGui() {
        backImage.setLayout(new BorderLayout());
        frame.setContentPane(backImage);
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setOpaque(false);
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        mainPanel.add(newBut, gbc);

        gbc.gridy = 1;
        mainPanel.add(continueBut, gbc);

        gbc.gridy = 2;
        mainPanel.add(exitBut, gbc);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(mainPanel);
        frame.setSize(250, 275);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                MainGui mainGui = new MainGui();
            }
        });
    }
}

这里有嵌套面板,可以得到相同的结果

import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

class MainGui {
    JFrame frame = new JFrame();
    JPanel mainPanel = new JPanel();
    JButton newBut = new JButton("New Game");
    JButton continueBut = new JButton("Continue");
    JButton exitBut = new JButton("Exit");
    JLabel backImage = new JLabel(new ImageIcon(
            getClass().getResource("images.jpg")));

    public MainGui() {
        backImage.setLayout(new GridLayout(3,1));
        frame.setContentPane(backImage);
        JPanel p1= new JPanel(new GridBagLayout());
        p1.setOpaque(false);
        p1.add(newBut);
        JPanel p2 = new JPanel(new GridBagLayout());
        p2.setOpaque(false);
        p2.add(continueBut);
        JPanel p3 = new JPanel(new GridBagLayout());
        p3.setOpaque(false);
        p3.add(exitBut);

        frame.add(p1);
        frame.add(p2);
        frame.add(p3);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(250, 275);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                MainGui mainGui = new MainGui();
            }
        });
    }
}