gpt4 book ai didi

java - 如何改变JButton的大小?

转载 作者:行者123 更新时间:2023-12-01 07:05:15 25 4
gpt4 key购买 nike

这是我的代码。该按钮的大小与 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);
}
}

最佳答案

我认为你忘记了setLayout,当我这样做时它工作得很好。不要使用 setBounds 并将其放入 javalearning 构造函数中,我还建议您使用 setDefaultCloseOperation 就像

public javalearning() {
FlowLayout f = new FlowLayout();
this.setLayout(f);
this.setSize(600, 600);
this.setTitle("this is a tittle");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton();
button.setText("Button");
this.add(button);
// button.setBounds(10, 10, 10, 10);

this.setVisible(true);
}

public static void main(String[] args) {
javalearning m = new javalearning();
}

最后,按照惯例,Java 类名以大写字母开头,并且采用驼峰式大小写。像 JavaLearning 这样的东西会遵循这个约定。

关于java - 如何改变JButton的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26951193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com