如何为 JButton 提供首选大小? [英] How to give a preffered size to the JButton?

查看:31
本文介绍了如何为 JButton 提供首选大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   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); 然后当我拉伸屏幕时按钮的高度增加但它的宽度仍然保持不变.
所以请告诉我出了什么问题?

So iam not getting the defined size for the buttons but when i set frame.setResizable(false); then when i stretch the screen the button's height increases but its width still remains the same.
So please tell me what is going wrong?

推荐答案

只是看了你的简短描述,我不知道你的问题是什么.但仅基于问题标题

Just reading your short descrption, I have no idea what your problem is. But based solely on the question title

如何为 JButton 提供首选大小?"

"How to give a preffered size to the JButton?"

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

Don't. Let the the layout manager handle this for you. If you want a bigger button, you can use JButton.setMargins(Insets) and/or JButton.setFont(Font) where you specify a bigger font.

如果您希望按钮拉伸或拉伸,您需要选择一个合适的布局管理器,它会或不会尊重按钮的首选大小.例如,BorderLayout 和 GridLayout 不会遵守首选大小,而是将按钮拉伸以使其适合,而 FlowLayout、BoxLayout 和 GridBagLayout 将遵守首选大小.正如您在此处

If you want you button stretched or not to stretch, You need to select an appropriate layout manager, that will or won't respect the buttons preferred size. For instance, BorderLayout and GridLayout won't respect preferred sizes and will stretch the button the fit, and FlowLayout, BoxLayout, and GridBagLayout will respect the preferred size. As you can see here

查看 GridBagLayout 示例

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

这里有嵌套面板,它会给出相同的结果

And here's with nesting panels which will give the same result

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

这篇关于如何为 JButton 提供首选大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆