Java Swing JTextField setMargin 意外行为
Java Swing JTextField setMargin unexpected behavior
当我调整 window 大小时,我的自定义 JTextField 改变了大小。它只执行一次,这是我第一次调整 window 的大小。与paintComponent
:
里面的这一行有关
setMargin(new Insets(2, 25, 2, 2));
运行 在我调整 window 之前,该命令不会调整文本字段的大小。在调整 window、运行 后,该命令导致 JTextField 变大。 window 的实际大小无关紧要。第一次更改 window 大小时,JTextField 变大,然后保持大直到时间结束。我希望在我启动程序后字段就很大,显然,我不希望它随机改变大小。
如何固定此 JTextField 的大小,使其不会随机更改?
这里是完整的 class:
/**
* From https://gmigdos.wordpress.com/2010/03/30/java-a-custom-jtextfield-for-searching/
* @author Georgios Migdos <cyberpython@gmail.com> */
public class JIconTextField extends JTextField {
private Icon icon;
private Insets dummyInsets;
public JIconTextField(int columns) throws IOException {
super(columns);
Border border = UIManager.getBorder("TextField.border");
JTextField dummy = new JTextField();
this.dummyInsets = border.getBorderInsets(dummy);
String path = "find-16x16.png";
InputStream is = Main.class.getClassLoader().getResourceAsStream(path);
setIcon(new ImageIcon(ImageIO.read(is)));
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int textX = 2;
if(this.icon!=null){
int iconWidth = icon.getIconWidth();
int iconHeight = icon.getIconHeight();
int x = dummyInsets.left + 5;//this is our icon's x
textX = x+iconWidth+2; //this is the x where text should start
int y = (this.getHeight() - iconHeight)/2;
icon.paintIcon(this, g, x, y);
}
setMargin(new Insets(2, textX, 2, 2));
}
}
除了注释之外,如果您从 paintComponent
方法中剪切代码并将其粘贴到构造函数中,您将获得所需的结果。
实际绘画的paintComponent
方法,即空白字段背景并绘制文本。一般来说,除非你特别想改变组件的绘制方式,否则你应该不需要覆盖这个方法。
当我调整 window 大小时,我的自定义 JTextField 改变了大小。它只执行一次,这是我第一次调整 window 的大小。与paintComponent
:
setMargin(new Insets(2, 25, 2, 2));
运行 在我调整 window 之前,该命令不会调整文本字段的大小。在调整 window、运行 后,该命令导致 JTextField 变大。 window 的实际大小无关紧要。第一次更改 window 大小时,JTextField 变大,然后保持大直到时间结束。我希望在我启动程序后字段就很大,显然,我不希望它随机改变大小。
如何固定此 JTextField 的大小,使其不会随机更改?
这里是完整的 class:
/**
* From https://gmigdos.wordpress.com/2010/03/30/java-a-custom-jtextfield-for-searching/
* @author Georgios Migdos <cyberpython@gmail.com> */
public class JIconTextField extends JTextField {
private Icon icon;
private Insets dummyInsets;
public JIconTextField(int columns) throws IOException {
super(columns);
Border border = UIManager.getBorder("TextField.border");
JTextField dummy = new JTextField();
this.dummyInsets = border.getBorderInsets(dummy);
String path = "find-16x16.png";
InputStream is = Main.class.getClassLoader().getResourceAsStream(path);
setIcon(new ImageIcon(ImageIO.read(is)));
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int textX = 2;
if(this.icon!=null){
int iconWidth = icon.getIconWidth();
int iconHeight = icon.getIconHeight();
int x = dummyInsets.left + 5;//this is our icon's x
textX = x+iconWidth+2; //this is the x where text should start
int y = (this.getHeight() - iconHeight)/2;
icon.paintIcon(this, g, x, y);
}
setMargin(new Insets(2, textX, 2, 2));
}
}
除了注释之外,如果您从 paintComponent
方法中剪切代码并将其粘贴到构造函数中,您将获得所需的结果。
实际绘画的paintComponent
方法,即空白字段背景并绘制文本。一般来说,除非你特别想改变组件的绘制方式,否则你应该不需要覆盖这个方法。