如何设置摆动组件的大小 [英] How to set size of swing components
问题描述
我想自定义JTextField对象的高度和宽度。我尝试过使用setSize方法,将width和height作为维度传递,也作为int传递。但它们似乎都不起作用。我是否遗漏了某些内容,例如面板上的某些强制性方法调用或某些内容,以便大小定制有效?请帮忙。提前致谢。
I want to customize the height and width of the JTextField objects. I have tried with the setSize method, passing width and height as dimensions and as int as well. But none of them seems to work. Am I missing something, like some mandatory method call on the panel or something so that the size customization would be effective? Please help. Thanks in advance.
编辑:这里有一些代码:
Here is a bit of the code:
public class WestPanel extends JPanel{
private JLabel dateL;
private JTextField date;
public WestPanel(){
setBackground(Color.white);
setLayout(new GridLayout(1,2,0,0));
dateL=new JLabel("Date: ");
date=new JTextField("dd/mm/yyyy");
date.setSize(60,10);
add(dateL);
add(date);
//....remaining code....//
推荐答案
让布局管理器处理Swing组件的尺寸,但如果绝对必须,请将 setPreferredSize
与尊重的布局管理器结合使用该财产。
Let the layout manager take care of the dimensions of your Swing components, but if you absolutely must, use setPreferredSize
in combination with a layout manager that respects that property.
这篇关于如何设置摆动组件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!