【问题标题】:How to give a preffered size to the JButton?如何为 JButton 提供首选尺寸?
【发布时间】:2014-08-27 00:12:12
【问题描述】:
   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); 时,当我拉伸屏幕时,按钮的高度会增加,但它的宽度仍然保持不变。
所以请告诉我出了什么问题?

【问题讨论】:

  • 不相关,硬编码系统路径,可能会在部署应用时出现麻烦,考虑如何add images to Java Project,为项目结构组织的方式。此外,在将所有组件添加到JFrame 后,尝试将frame.setVisible(true) 作为最后一行,前面是frame.pack()。在此之前不要调用它,这不是一个好习惯,也是你必须再次设置大小的原因,而是调用frame.pack()

标签: java swing


【解决方案1】:

您应该查看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();
        }
    });
}

【讨论】:

  • 哦!我是java新手,所以我不太了解它。
【解决方案2】:

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

“如何为 JButton 指定首选大小?”

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

如果您希望按钮拉伸或 拉伸,您需要选择合适的布局管理器,该管理器将尊重或不尊重按钮的首选大小。例如,BorderLayout 和 GridLayout 不会尊重首选尺寸,而是会适当拉伸按钮,而 FlowLayout、BoxLayout 和 GridBagLayout 会尊重首选尺寸。如您所见here


查看 GridBagLayout 示例

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

【讨论】:

  • 请再告诉我一件事,在您的第一种情况下,为什么我们需要backImage.setLayout(new BorderLayout());。我注意到没有它,图像显示但没有按钮,这是什么原因,请回复.谢谢。
  • 因为 JLabel 有默认的空布局。为了添加到空布局,您需要为添加到其中的组件指定边界。喜欢panel.setBounds(0, 0, 300, 300); label.add(panel);。使用布局管理器,无需设置边界,因为布局管理器会为您处理尺寸和定位
  • 这意味着如果不设置边界,您无法将组件添加到空布局容器中
  • 是的,没错。测试一下。不过,您应该始终使用布局管理器。主容器已经带有默认布局管理器。 JFrame=BorderLayout, JPanel=FlowLayout
  • 非常感谢您救了我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 2015-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多