我不知道如何调整第三个按钮的大小,使其与其他两个按钮的大小相同并将其放置在底部。
class ControlFrame extends JFrame
implements Runnable
{
JButton jb_inc = new JButton();
JButton jb_zero = new JButton();
JButton jb_dec = new JButton();
ControlFrame() {
super("Control Frame");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
ControlFrame(int x,int y, int w, int h) {
this();
this.setBounds(x, y, w, h);
this.setVisible(true);
jb_inc.setBounds(10,10,90,20);
jb_zero.setBounds(10,40,90,20);
jb_dec.setBounds(10,60,90,20);
jb_inc.setVisible(true);
jb_zero.setVisible(true);
jb_dec.setVisible(true);
this.getContentPane().add(jb_inc);
this.getContentPane().add(jb_zero);
this.getContentPane().add(jb_dec);
}
public void run() {
}
}
public class Counting_Machine
{
public static void main(String[] args) {
ControlFrame cf = new ControlFrame(0,200,80,150);
}
}
最佳答案
我建议您首先阅读 Swing 教程 Using Layout Managers 中的部分。您的代码将无法工作,因为您试图将 3 个组件添加到 BorderLayout 的“中心”,而这将无法工作。选择更能满足您需求的 LayoutManager。
使用布局管理器时无需使用 setBounds(...) 方法。布局管理器的工作是根据布局管理器的规则定位组件。每个按钮的首选大小应该相同,因为您尚未向按钮添加任何文本。
此外,不需要在按钮上调用 setVisible(),默认情况下所有 Swing 组件都是可见的,除了顶级容器(JFrame、JDialog)。
关于java - 无法调整 JButton 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1958689/