【发布时间】:2021-06-01 16:00:31
【问题描述】:
我想创建一个基于我的参数 Component 返回 JTextField/JPasswordField 的方法,并且我希望在组件初始化后覆盖paintmethod,有什么解决方案吗?
这是我的代码:
public static Component Create(int size,int x, int y, int width, int height, Component type) {
Component element;
if (type instanceof JTextField) {
element = new JTextField(size);
}else if (type instanceof JPasswordField) {
element = new JPasswordField(size);
}
element{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override protected void paintComponent(Graphics g) {
if (!isOpaque() && getBorder() instanceof RoundedCornerBorder) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setPaint(getBackground());
g2.fill(((RoundedCornerBorder) getBorder()).getBorderShape(
0, 0, getWidth() - 1, getHeight() - 1));
g2.dispose();
}
super.paintComponent(g);
}
@Override public void updateUI() {
super.updateUI();
setOpaque(false);
setBorder(new RoundedCornerBorder());
}
};
element.setBounds(x, y,width, height);
element.setBackground(culoare);
return element;
}
【问题讨论】:
标签: java methods overriding