Java get Border color and size

JTextField tf = new JTextField();
tf.setBorder(new LineBorder(Color.red, 2));
Border border = tf.getBorder();

我怎样才能获得边框的颜色和大小?


To get the border color:

 ((LineBorder)JTextField.getBorder()).getLineColor();

and this just a thought about how to get the border size, if you assume that the border size is the same as the component size you can cast JTextField to JComponent and get the size of JTextField:

 ((JComponent)JTextField).getSize();

but you should use it after putting the JTextField in its container, otherwise it will return (0,0).


JTextField tf = new JTextField();
tf.setBorder(new LineBorder(Color.red, 2));
LineBorder border = (LineBorder) tf.getBorder();
System.out.println("Border color = "+  border.getLineColor() 
                          + "  size= " + border.getThickness());

JTextField.setPreferredSize(new Dimension(350, 20));
链接地址: http://www.djcxy.com/p/10592.html

上一篇: 点击跟踪Windows应用程序

下一篇: Java获取边框颜色和大小