Java 如何更改JButton的大小?

Java 如何更改JButton的大小?,java,swing,jbutton,layout-manager,flowlayout,Java,Swing,Jbutton,Layout Manager,Flowlayout,这是我的密码。按钮的大小与FlowLayout相同。我怎样才能使按钮变小 public class javalearning extends JFrame{{ FlowLayout f = new FlowLayout(); this.setSize(600,600); JFrame j = new JFrame(); this.setTitle("this is a tittle"); JButton button = new JButto

这是我的密码。按钮的大小与FlowLayout相同。我怎样才能使按钮变小

public class javalearning extends JFrame{{

    FlowLayout f = new FlowLayout();
    this.setSize(600,600);

    JFrame j = new JFrame();
    this.setTitle("this is a tittle");

        JButton button = new JButton();
        button.setText("Button");
        this.add(button);
        button.setBounds(10, 10, 10, 10);

        this.setVisible(true);  
    }
}
如果需要,FlowLayout将从左到右或从右到左包装组件。如果您希望显式设置每个JButton的大小,您应该使用setPreferredSize而不是setSize或setBounds,因为布局管理器在执行布局时通常使用最小、首选和最大大小。

我想您忘了设置布局,这样做很好。不要使用setBounds并将其放入javalearning构造函数中,我还建议您设置DefaultCloseOperation,如


最后,按照惯例,Java类名以大写字母开头,大小写相同。类似JavaLearning的东西将遵循这一约定。

您从未将布局管理器FlowLayout设置为框架,因此JFrame仍然使用其默认的BorderLayout布局管理器

尝试使用更像

FlowLayout f = new FlowLayout();
setLayout(f);
this.setTitle("this is a tittle");

JButton button = new JButton();
button.setText("Button");
this.add(button);

this.pack();
this.setVisible(true);  
相反

请仔细查看以了解更多详细信息

button.setBounds(x, y, height, width);
你可以给高度和宽度小于10!
您还可以使用GridLayout在一个按钮中为较小的按钮设置for循环cough@MadProgrammer哎呀。错过了。谢谢它只会混淆代码,但除此之外,它的方向是正确的;谢谢事实上,我刚刚检查了我的代码,似乎我忘记添加setlayout,所以现在它可以正常工作了。再次感谢:
button.setBounds(x, y, height, width);