2

我正在写一个摇摆程序。但问题是我想要的 JButton 很小。它的高度和宽度应该由我来决定,但下面的代码会创建一个长的水平按钮。

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

public class SwingExample 
{
         //Create the GUI and show it. For thread safety, this method should be
        //invoked from the event-dispatching thread

        private static void createAndShowGUI()
        {
            //create and setup the window
            JFrame frame=new JFrame("Swing Demo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //Set the size of the window
            frame.setPreferredSize(new Dimension(300,300));            


            //Centre the window on the screen
            WinUtilities wu=new WinUtilities();
            wu.centerWindow(frame);

            //Create a panel
            JPanel panel=new JPanel(new BorderLayout());

            //Create three buttons

                //Button1
                    JButton But1=new JButton("Add");
                    But1.setText("Add Data");
                    But1.setSize(new Dimension(10,20)); //Using it has no effect
                    But1.setMnemonic('A');
                    But1.setMargin(new Insets(12,7,20,10)); //Using it has no effect
                    But1.setBorder(null); //Using it has no effect
                    panel.add(But1,BorderLayout.WEST);

                //Button2
                    JButton But2=new JButton("Edit");
                    But2.setText("Edit Data");
                    But2.setMnemonic(KeyEvent.VK_E);
                    But2.setPreferredSize(new Dimension(5,5));

                    panel.add(But2,BorderLayout.CENTER);

               //Button3
                    JButton But3=new JButton("Display");
                    But3.setText("Display Data");
                    But3.setMnemonic(KeyEvent.VK_D);
                    But3.setPreferredSize(new Dimension(5,5));
                    panel.add(But3,BorderLayout.EAST);


            //Set window characteristics
            frame.setContentPane(panel);
            //frame.add(panel);
            frame.pack();



            //Display the window
            frame.setVisible(true);
        }

        public static void main(String args[])
        {
            //Schedule a job for the event dispatching thread

            //creating and showing this application's GUI
            javax.swing.SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowGUI();
                }
            });

        }
}

class WinUtilities
{
    public void centerWindow(JFrame frm)
    {
        frm.setLocationRelativeTo(null);
    }
}

请帮忙

4

2 回答 2

3

例如

在此处输入图像描述

建议

  • JPanel已在 API 中实现FlowLayout(相当接受PreferredSize),

  • 那么 BoderLayout可能是错LayoutManager的,

  • 或使用GridLayout(然后所有JButtons将具有相同的大小,基于最大的项目)

代码

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

public class SwingExample {

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Swing Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        JButton but1 = new JButton("Add");
        but1.setText("Add Data");
        but1.setMnemonic('A');
        but1.setBorderPainted(false);
        but1.setBackground(Color.red);
        but1.setBorder(null);
        but1.setFocusable(false);
        but1.setMargin(new Insets(0, 0, 0, 0));
        //but1.setContentAreaFilled(false);
        panel.add(but1);
        JButton but2 = new JButton("Edit");
        but2.setText("Edit Data");
        but2.setMnemonic(KeyEvent.VK_E);
        panel.add(but2);
        JButton but3 = new JButton("Display");
        but3.setText("Display Data");
        but3.setMnemonic(KeyEvent.VK_D);
        panel.add(but3);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
            }
        });
    }
}
于 2012-10-01T10:15:27.260 回答
1

按钮很大,因为:

  • 您已将框架的首选尺寸设置为 300x300
  • 您已将内容的布局设置为 BorderLayout
  • 按钮被放大以适应这么大的框架

只需删除 frame.setPreferredSize() 按钮就会变小(框架大小将通过调用 frame.pack() 方法由内部组件的首选大小决定)。

顺便一提:

  • But1.setSize(...); 被忽略 - 你应该总是调用: setMinimumSize/setMaximumSize/setPreferredSize 相反,布局根据约束确定大小
  • 不太可能在首选大小为 5x5 的按钮上正确显示文本:“编辑数据”、“添加数据”,考虑使用图标或使按钮更大
于 2012-10-01T10:25:42.597 回答