Swing/RoundedTextField のバックアップ(No.2)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/RoundedTextField へ行く。
- 1 (2010-10-04 (月) 21:07:50)
- 2 (2011-03-05 (土) 03:25:12)
- 3 (2011-03-11 (金) 03:07:02)
- 4 (2012-03-21 (水) 02:34:09)
- 5 (2012-12-26 (水) 06:29:53)
- 6 (2013-08-30 (金) 01:32:26)
- 7 (2013-09-06 (金) 15:49:16)
- 8 (2013-09-28 (土) 21:42:49)
- 9 (2014-11-13 (木) 01:29:40)
- 10 (2014-11-21 (金) 18:30:04)
- 11 (2014-11-25 (火) 03:03:31)
- 12 (2015-04-03 (金) 16:09:10)
- 13 (2016-10-16 (日) 21:33:46)
- 14 (2016-10-18 (火) 00:51:08)
- 15 (2017-04-07 (金) 13:51:51)
- 16 (2017-08-15 (火) 14:28:20)
- 17 (2018-02-24 (土) 19:51:30)
- 18 (2018-08-12 (日) 18:39:28)
- 19 (2020-05-15 (金) 02:42:48)
- 20 (2021-11-05 (金) 00:04:35)
TITLE:JTextFieldの角を丸める
Posted by aterai at 2010-10-04
JTextFieldの角を丸める
角丸のJTextFieldを作成します。コードはUnleash Your Creativity with Swing and the Java 2D API!、Swing - custom JTextFieldから引用しています。
- &jnlp;
- &jar;
- &zip;
サンプルコード
JTextField textField01 = new JTextField(20) {
//Unleash Your Creativity with Swing and the Java 2D API!
//http://java.sun.com/products/jfc/tsc/articles/swing2d/index.html
@Override protected void paintComponent(Graphics g) {
int w = getWidth();
int h = getHeight();
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(getBackground());
g.fillRoundRect(0, 0, w-1, h-1, h, h);
g2.setColor(Color.GRAY);
g2.drawRoundRect(0, 0, w-1, h-1, h, h);
super.paintComponent(g);
}
};
textField01.setOpaque(false);
textField01.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));
textField01.setText("aaaaaaaaaaa");
解説
- 上
- JTextField#paintComponent(...)をオーバーライド、BorderをEmptyBorder、JTextField#setOpaque(false);
- 参考: Unleash Your Creativity with Swing and the Java 2D API!
- 下
- RoundTextUIを作成して、JTextField#setUI(...)で設定
- 参考: Swing - custom JTextField
//http://forums.sun.com/thread.jspa?threadID=260846
class RoundTextUI extends BasicTextFieldUI {
public static ComponentUI createUI(JComponent c) {
return new RoundTextUI();
}
public void installUI(JComponent c) {
super.installUI(c);
c.setBorder(new RoundBorder());
c.setOpaque(false);
}
protected void paintSafely(Graphics g) {
JComponent c = getComponent();
if(!c.isOpaque()) {
g.setColor(c.getBackground());
g.fillRoundRect(0, 0, c.getWidth()-1, c.getHeight()-1, c.getHeight(), c.getHeight());
}
super.paintSafely(g);
}
private static class RoundBorder extends AbstractBorder {
@Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.GRAY);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawRoundRect(x, y, width-1, height-1, height, height);
}
@Override public Insets getBorderInsets(Component c) {
return new Insets(4, 8, 4, 8);
}
@Override public Insets getBorderInsets(Component c, Insets insets) {
insets.left = insets.right = 8;
insets.top = insets.bottom = 4;
return insets;
}
}
}
参考リンク
- Unleash Your Creativity with Swing and the Java 2D API!
- Swing - custom JTextField
- Border on an rounded JTextField? (Swing / AWT / SWT / JFace forum at JavaRanch)