我想在点击按钮时动态更改文本字段的宽度。 [英] I want to change the width of textfield dynamically on button click..
本文介绍了我想在点击按钮时动态更改文本字段的宽度。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
某种代码是:
构造函数中的代码是:
{
JTextField txt = new JTextField( 10 );
JPanel jp = new JPanel();
jp.add(txt);
JButton btn1 =新的JButton( change);
jp.add(btn1);
}
刚刚停留在我的actionPerformed方法中。我可以在这里编码什么...
解决方案
在匿名块中你要设置文本字段的大小。
JButton btnChangeWidth = new JButton( 改变宽度);
btnChangeWidth.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent arg0){
textField.setSize(x,y);
}
});
别忘了在容器上添加按钮..
如需更多参考,请访问此处 http://cshotopics.blogspot.in/2014/05/how-to-change-width-of-textfield.html
改变宽度使用SetSize
方法。
例如将宽度更改为800:
int h = txt.getSize()。getHeight();
txt.setSize( new 尺寸( 800 ,h));
some sort of code is:
The code inside constructor is:
{
JTextField txt=new JTextField(10);
JPanel jp=new JPanel();
jp.add(txt);
JButton btn1=new JButton("change");
jp.add(btn1);
}
just stuck in my actionPerformed method. what can i code here...
解决方案
In anonymous block you've to set the size of text field.
JButton btnChangeWidth = new JButton("Change width"); btnChangeWidth.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textField.setSize(x,y); } });
don't forget to add the button on container..
for more reference you can visit here http://cshotopics.blogspot.in/2014/05/how-to-change-width-of-textfield.html
Change the width by usingSetSize
Method.
For example to change the width to 800:
int h = txt.getSize().getHeight(); txt.setSize(new Dimension(800, h));
这篇关于我想在点击按钮时动态更改文本字段的宽度。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文