Swing/HTMLColorCodes のバックアップ(No.14)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/HTMLColorCodes へ行く。
- 1 (2016-10-31 (月) 12:40:42)
- 2 (2017-01-17 (火) 14:59:10)
- 3 (2017-02-20 (月) 19:40:38)
- 4 (2017-02-28 (火) 17:51:53)
- 5 (2017-04-07 (金) 13:51:51)
- 6 (2017-12-13 (水) 16:03:15)
- 7 (2018-02-15 (木) 14:23:42)
- 8 (2019-08-30 (金) 18:05:38)
- 9 (2021-04-23 (金) 20:07:18)
- 10 (2022-09-19 (月) 09:37:22)
- 11 (2025-01-03 (金) 08:57:02)
- 12 (2025-01-03 (金) 09:01:23)
- 13 (2025-01-03 (金) 09:02:38)
- 14 (2025-01-03 (金) 09:03:21)
- 15 (2025-01-03 (金) 09:04:02)
- category: swing folder: HTMLColorCodes title: HTMLの16進数カラーコードからColorを生成する tags: [HTML, JLabel, Color] author: aterai pubdate: 2016-10-31T12:38:51+09:00 description: HTMLの16進数カラーコードからColorを生成して、JLabelの文字色を変更する方法をテストします。 image: https://drive.google.com/uc?id=1Vm61yca-8zEib19f6hRDxNtoX7gcUP6Ubg
Summary
HTML
の16
進数カラーコードからColor
を生成して、JLabel
の文字色を変更する方法をテストします。
Screenshot

Advertisement
Source Code Examples
private MainPanel() {
super(new BorderLayout());
Box box = Box.createVerticalBox();
box.add(makeLabel("new Color(0xff0000)", new Color(0xff0000)));
box.add(makeLabel("new Color(0x88_88_88)", new Color(0x88_88_88)));
box.add(makeLabel("new Color(Integer.parseInt(\"00ff00\", 16))",
new Color(Integer.parseInt("00ff00", 16))));
box.add(makeLabel("new Color(Integer.decode(\"#0000ff\"))",
new Color(Integer.decode("#0000ff"))));
box.add(makeLabel("Color.decode(\"#00ffff\")", Color.decode("#00ffff")));
JLabel label = new JLabel("<html><span style='color: #ff00ff'>#ff00ff");
label.setBorder(BorderFactory.createTitledBorder(
"new JLabel(\"<html><span style='color: #ff00ff'>#ff00ff\")"));
box.add(label);
box.add(Box.createVerticalGlue());
add(new JScrollPane(box));
setPreferredSize(new Dimension(320, 240));
}
private static JLabel makeLabel(String title, Color c) {
JLabel label = new JLabel(String.format("#%06x", c.getRGB() & 0xffffff)) {
@Override public Dimension getMaximumSize() {
Dimension d = super.getPreferredSize();
d.width = Short.MAX_VALUE;
return d;
}
};
label.setBorder(BorderFactory.createTitledBorder(title));
label.setForeground(c);
return label;
}
View in GitHub: Java, KotlinExplanation
new Color(0xff0000)
- 頭に
0x
をつけた16
進数表記の数値を使用してColor
を生成
- 頭に
new Color(0x88_88_88)
- 頭に
0x
をつけた16
進数表記の数値を使用してColor
を生成 2
桁ごとにアンダースコア_
を挿入して16
進数表記数値リテラルの可読性を向上させている
- 頭に
new Color(Integer.parseInt("00ff00", 16))
- Integer.parseInt(String, int) (Java Platform SE 8)
- 基数を
16
進にしてInteger.parseInt(String, int)
を使用し、文字列を整数に変換してColor
を生成 #00ff00
や0x00ff00
などはNumberFormatException
になる
new Color(Integer.decode("#0000ff"))
- Integer.decode(String) (Java Platform SE 8)
Integer.decode(String)
を使用して文字列を整数にデコード- 基数指定子のない
0000ff
や桁間のアンダースコアがある0x00_00_ff
はNumberFormatException
になる
Color.decode("#00ffff")
- Color.decode(String) (Java Platform SE 8)
- 内部で
Integer.decode(String)
を使用して文字列を整数にデコードし、Color
を生成
new JLabel("<html><span style='color: #ff00ff'>#ff00ff")
- 要素に
style
属性を追加してCSS
で文字色を指定したJLabel
を生成
- 要素に
参考リンク
- java - Is it possible to use Color Hex in JLabel like #02f7fc? - Stack Overflow
- [JDK-8293776] Adds CSS 4 and 8 digits hex coded Color - Java Bug System