絶対もっと簡単にできると思う^^;
Java
1 package MusicPlayer ;
2
3 import java . awt . BorderLayout ;
4 import java . awt . Color ;
5 import java . awt . EventQueue ;
6 import java . awt . Font ;
7 import java . awt . FontMetrics ;
8 import java . awt . GradientPaint ;
9 import java . awt . Graphics ;
10 import java . awt . Graphics2D ;
11 import java . awt . Point ;
12 import java . awt . Rectangle ;
13 import java . awt . SystemColor ;
14 import javax . swing . JFrame ;
15 import javax . swing . JLabel ;
16 import javax . swing . JMenu ;
17 import javax . swing . JMenuBar ;
18 import javax . swing . JMenuItem ;
19 import javax . swing . SwingUtilities ;
20
21 public class GUI {
22
23 private JFrame frmNovaMusicPlayer ;
24 private JLabel lblNovaMusicPlayer ;
25 private JMenuBar menuBar ;
26 private JMenu menu ;
27 private JMenuItem menuItem ;
28
29 31
32 public static void main ( String [ ] args ) {
33 EventQueue . invokeLater ( new Runnable ( ) {
34 public void run ( ) {
35 try {
36 GUI window = new GUI ( ) ;
37 window . frmNovaMusicPlayer . setVisible ( true ) ;
38 } catch ( Exception e ) {
39 e . printStackTrace ( ) ;
40 }
41 }
42 } ) ;
43 }
44
45 47
48 public GUI ( ) {
49 initialize ( ) ;
50 }
51
52 54
55 private void initialize ( ) {
56 frmNovaMusicPlayer = new JFrame ( ) ;
57 frmNovaMusicPlayer . setBackground ( SystemColor . textHighlight ) ;
58 frmNovaMusicPlayer . setTitle ( "Nova Music Player" ) ;
59 frmNovaMusicPlayer . setBounds ( 100 , 100 , 854 , 450 ) ;
60 frmNovaMusicPlayer . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ;
61 frmNovaMusicPlayer . getContentPane ( ) . setLayout ( new BorderLayout ( 0 , 0 ) ) ;
62
63 menuBar = new JMenuBar ( ) ;
64 menuBar . setBorderPainted ( false ) ;
65 menuBar . setBackground ( new Color ( 255 , 255 , 255 ) ) ;
66 menuBar . setFont ( new Font ( "Yu Gothic UI" , Font . PLAIN , 12 ) ) ;
67 frmNovaMusicPlayer . getContentPane ( ) . add ( menuBar , BorderLayout . NORTH ) ;
68
69 menu = new JMenu ( "ファイル" ) ;
70 menuBar . add ( menu ) ;
71
72 JMenuItem menuItem_1 = new JMenuItem ( "音楽ファイルを開く" ) ;
73 menu . add ( menuItem_1 ) ;
74
75 lblNovaMusicPlayer = new JLabel ( "Nova Music Player" ) {
76 @Override
77 public void paintComponent ( Graphics g ) {
78 final Graphics2D g2 = ( Graphics2D ) g . create ( ) ;
79 g2 . setFont ( getFont ( ) ) ;
80
81 Rectangle viewR = new Rectangle ( ) ;
82 viewR . width = getSize ( ) . width ;
83 viewR . height = getSize ( ) . height ;
84 Rectangle iconR = new Rectangle ( ) ;
85 Rectangle textR = new Rectangle ( ) ;
86 SwingUtilities . layoutCompoundLabel ( this , g2 . getFontMetrics ( ) ,
87 getText ( ) , getIcon ( ) ,
88 getVerticalAlignment ( ) , getHorizontalAlignment ( ) ,
89 getVerticalTextPosition ( ) , getHorizontalTextPosition ( ) ,
90 viewR , iconR , textR , getIconTextGap ( ) ) ;
91
92 FontMetrics fm = g2 . getFontMetrics ( ) ;
93
94 g2 . setPaint ( new GradientPaint ( new Point ( textR . x , textR . y ) , Color . WHITE ,
95 new Point ( textR . x , textR . y + textR . height ) , Color . PINK . darker ( ) ) ) ;
96
97 g2 . drawString ( getText ( ) , textR . x , textR . y + fm . getMaxAscent ( ) ) ;
98 g2 . dispose ( ) ;
99 }
100 } ;
101 lblNovaMusicPlayer . setToolTipText ( "" ) ;
102 lblNovaMusicPlayer . setForeground ( new Color ( 0 , 255 , 0 ) ) ;
103 lblNovaMusicPlayer . setFont ( new Font ( "源界明朝" , Font . PLAIN , 80 ) ) ;
104 lblNovaMusicPlayer . setBackground ( Color . WHITE ) ;
105 frmNovaMusicPlayer . getContentPane ( ) . add ( lblNovaMusicPlayer ) ;
106 }
107 }
追記 普通のクラス
Java
1 import java . awt . Color ;
2 import java . awt . EventQueue ;
3 import java . awt . Font ;
4 import java . awt . FontMetrics ;
5 import java . awt . GradientPaint ;
6 import java . awt . Graphics ;
7 import java . awt . Graphics2D ;
8 import java . awt . Point ;
9 import java . awt . Rectangle ;
10 import java . awt . SystemColor ;
11 import javax . swing . JFrame ;
12 import javax . swing . JLabel ;
13 import javax . swing . SwingUtilities ;
14
15
16 // JFrameを継承してこのアプリ用にカスタマイズしたGUIクラスを作る
17 public class GUI extends JFrame {
18 public static void main ( String [ ] args ) {
19 EventQueue . invokeLater ( new Runnable ( ) { // ここも匿名クラスです
20 public void run ( ) {
21 try {
22 GUI window = new GUI ( ) ;
23 window . setVisible ( true ) ;
24 } catch ( Exception e ) {
25 e . printStackTrace ( ) ;
26 }
27 }
28 } ) ;
29 }
30
31 public GUI ( ) {
32 // このアプリ専用のいろんなカスタマイズ
33 setBackground ( SystemColor . textHighlight ) ;
34 setTitle ( "Nova Music Player" ) ;
35 setBounds ( 100 , 100 , 854 , 450 ) ;
36 setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ;
37
38 // GradationLabelはJLabelの機能は当然全部持っているので、JLabel変数に入れても問題ない
39 JLabel lbl = new GradationLabel ( "Nova Music Player" ) ;
40 lbl . setToolTipText ( "" ) ;
41 lbl . setForeground ( new Color ( 0 , 255 , 0 ) ) ;
42 lbl . setFont ( new Font ( "源界明朝" , Font . PLAIN , 80 ) ) ;
43 lbl . setBackground ( Color . WHITE ) ;
44
45 add ( lbl ) ;
46 }
47 }
48
49 // JLabelを継承してグラデーションのテキストを表示するラベルに改造
50 // でもほとんどの機能はJLabelに任せる
51 class GradationLabel extends JLabel {
52 public GradationLabel ( String text ) {
53 super ( text ) ;
54 }
55
56 // paintComponent(表示するところ)だけをグラデーションするロジックに置き換える
57 @Override
58 public void paintComponent ( Graphics g ) {
59 final Graphics2D g2 = ( Graphics2D ) g . create ( ) ;
60 g2 . setFont ( getFont ( ) ) ;
61
62 Rectangle viewR = new Rectangle ( ) ;
63 viewR . width = getSize ( ) . width ;
64 viewR . height = getSize ( ) . height ;
65 Rectangle iconR = new Rectangle ( ) ;
66 Rectangle textR = new Rectangle ( ) ;
67 SwingUtilities . layoutCompoundLabel ( this , g2 . getFontMetrics ( ) ,
68 getText ( ) , getIcon ( ) ,
69 getVerticalAlignment ( ) , getHorizontalAlignment ( ) ,
70 getVerticalTextPosition ( ) , getHorizontalTextPosition ( ) ,
71 viewR , iconR , textR , getIconTextGap ( ) ) ;
72
73 FontMetrics fm = g2 . getFontMetrics ( ) ;
74
75 g2 . setPaint ( new GradientPaint ( new Point ( textR . x , textR . y ) , Color . WHITE ,
76 new Point ( textR . x , textR . y + textR . height ) , Color . PINK . darker ( ) ) ) ;
77
78 g2 . drawString ( getText ( ) , textR . x , textR . y + fm . getMaxAscent ( ) ) ;
79 g2 . dispose ( ) ;
80 }
81 }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/31 13:29
2020/07/31 13:33
2020/07/31 13:34
2020/07/31 13:39
2020/07/31 13:42
2020/07/31 13:42
2020/07/31 15:28
2020/07/31 16:07
2020/07/31 16:26
2020/07/31 16:41 編集
2020/07/31 20:39
2020/07/31 21:39
2020/08/01 01:33
2020/08/01 09:23
2020/08/01 09:24
2020/08/01 10:30
2020/08/01 11:37
2020/08/01 11:52
2020/08/01 13:43
2020/08/01 13:49
2020/08/02 08:53
2020/08/02 09:56
2020/08/02 10:15
2020/08/02 10:37
2020/08/02 10:45
2020/08/02 10:55
2020/08/02 11:02
2020/08/02 11:23
2020/08/02 11:38
2020/08/02 11:53
2020/08/02 12:03
2020/08/02 12:48
2020/08/02 13:27
2020/08/02 13:57
2020/08/02 14:36
2020/08/03 10:21
2020/08/03 10:42
2020/08/04 04:02
2020/08/04 09:01
2020/08/04 09:18
2020/08/04 09:33
2020/08/04 09:50
2020/08/04 10:20
2020/08/04 10:29
2020/08/04 10:38
2020/08/04 10:45
2020/08/04 10:49
2020/08/04 10:52
2020/08/04 11:00
2020/08/04 11:11
2020/08/04 11:20
2020/08/04 11:43
2020/08/04 11:48
2020/08/04 11:50 編集
2020/08/04 11:52
2020/08/04 11:56
2020/08/04 12:03
2020/08/04 12:14