更改JButton的尺寸

更改JButton的尺寸

本文介绍了更改JButton的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此有疑问.该按钮占用了整个JFrame.我试过更改JFrame和JButton的尺寸,但没有更改.它完全隐藏了下面的JTable.有人可以告诉我我在做什么错.

I have problem with this. The button is taking up the entire JFrame. I've tried changing the dimensions of the JFrame and the JButton but with no changed. It's completely hiding a JTable underneath. Could someone please tell me what I'm doing wrong.

JFrame FRAME = new JFrame();
  JButton BUTTON = new JButton("OK");
  FRAME.add(new JScrollPane(TableName));
  BUTTON.setPreferredSize(new Dimension(20,30));
  FRAME.add(BUTTON);
  FRAME.setSize(700, 600);
  FRAME.setVisible(true);
  FRAME.setLocationRelativeTo(null);

推荐答案

JFrame的默认布局管理器是BorderLayout.您试图添加两个不允许的CENTER组件.仅显示最后添加的一个.

The default layout manager for a a JFrame is a BorderLayout. You are attempting to add two component the CENTER which is not allowed. Only the last one added will be displayed.

在将组件添加到BorderLayout时需要指定约束.您的代码应类似于:

You need to specify constraints when adding components to a BorderLayout. Your code should be something like:

  frame.add(new JScrollPane(TableName), BorderLayout.CENTER);
  button.setPreferredSize(new Dimension(20,30));
  frame.add(button, BorderLayout.PAGE_START);

此外,变量名称不应大写.遵循Java约定.

Also, variables names should NOT be upper cased. Follow Java convention.

如何使用BorderLayout上的Swing教程中阅读本节. a>有关更多信息和工作示例.该教程代码还将向您展示如何更好地构建程序结构,从而遵循Swing编码约定.

Read the section from the Swing tutorial on How to Use BorderLayout for more information and working examples. The tutorial code will also show you how to better structure your program so you follow Swing coding conventions.

这篇关于更改JButton的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!