当我这样做时:
LineBorder lineBorder =new LineBorder(Color.white, 8, true);
jTextField2.setBorder(lineBorder );
我得到的结果如下:
如果没有方形角可见且文字切成一半,我怎么能得到圆形边框?
非常感谢。
祝你好运
答案 0 :(得分:13)
您可以覆盖JTextFiled
构建自己的圆角JTextField
。您必须覆盖它的paintComponent()
,paintBorder()
和contains()
方法。您需要将roundRect绘制为文本字段的形状。
示例:
public class RoundJTextField extends JTextField {
private Shape shape;
public RoundJTextField(int size) {
super(size);
setOpaque(false); // As suggested by @AVD in comment.
}
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
super.paintComponent(g);
}
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
}
public boolean contains(int x, int y) {
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 15, 15);
}
return shape.contains(x, y);
}
}
要看到这个有效:
JFrame frame = new JFrame("Rounded corner text filed demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLayout(new FlowLayout());
JTextField field = new RoundJTextField(15);
frame.add(field);
frame.setVisible(true);
答案 1 :(得分:2)
答案 2 :(得分:1)
非常类似于@Harry Joy的回答 - 只是走了一段距离,如outlined in a recent answer
答案 3 :(得分:0)
这将修改您在整个应用程序中创建的任何JTextField
将它放在第一个窗口的开头,它将影响每个JTextField。
bob
自定义边框
UIManager.put("TextField.background", Color.WHITE);
UIManager.put("TextField.border", BorderFactory.createCompoundBorder(
new CustomeBorder(),
new EmptyBorder(new Insets(4,4,4,4))));