次のJTextFieldクラスがあります。RoundedJTextFieldを作成します。JPasswordFieldはJTextFieldから継承すると思ったので、今度はJPasswordFieldに同じ設定を使用したいと思いました。次のことができます:JPasswordField new_field = new RoundField(SOME Parameters);
しかし、それは大きな災害でした。コードを繰り返さずにJPasswordFieldを丸める方法はありますか?
public class RoundField extends JTextField {
public RoundField(String text, int x, int y, int width, int height) {
setText(text);
setBounds(x, y, width, height);
setForeground(Color.GRAY);
setHorizontalAlignment(JTextField.CENTER);
setOpaque(false);
setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4));
}
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRoundRect(0, 0, getWidth(), getHeight(), 8, 8);
super.paintComponent(g);
}
}
PS:必要に応じて、setTextをコンストラクターから移動してもかまいません。