「adding a window to a container」
「コンテナにウィンドウを追加する」
by Google翻訳
スライダー3つでRGBを選ぶパネルなら、こんな感じでしょうか。
java
1 package teratail_java . q367113 ;
2
3 import java . awt . * ;
4
5 import javax . swing . * ;
6 import javax . swing . event . ChangeEvent ;
7 import javax . swing . event . ChangeListener ;
8
9 public class MainFrame extends JFrame {
10 public static void main ( String [ ] args ) {
11 new MainFrame ( ) . setVisible ( true ) ;
12 }
13
14 MainFrame ( ) {
15 super ( ) ;
16 setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ;
17
18 JTextField rgbTextField = new JTextField ( ) ;
19 rgbTextField . setEditable ( false ) ;
20
21 RGBSliderPanel rgbSliderPanel = new RGBSliderPanel ( ) ;
22 rgbSliderPanel . setChangeListener ( new RGBSliderPanel . Listener ( ) {
23 @Override
24 public void onChange ( int red , int green , int blue ) {
25 rgbTextField . setText ( "red=" + red + ", green=" + green + ", blue=" + blue ) ;
26 }
27 } ) ;
28
29 add ( rgbTextField , BorderLayout . NORTH ) ;
30 add ( rgbSliderPanel , BorderLayout . CENTER ) ;
31
32 pack ( ) ;
33 }
34 }
35
36 class RGBSliderPanel extends JPanel {
37 interface Listener {
38 void onChange ( int red , int green , int blue ) ;
39 }
40 private Listener listener ;
41 private int red , green , blue ;
42
43 RGBSliderPanel ( ) {
44 super ( new GridBagLayout ( ) ) ;
45 setBorder ( BorderFactory . createEmptyBorder ( 10 , 10 , 10 , 10 ) ) ;
46
47 JLabel redLabel = new JLabel ( "R" ) ;
48 JSlider redSlider = new JSlider ( JSlider . HORIZONTAL , 0 , 255 , 0 ) ;
49 redSlider . addChangeListener ( new ChangeListener ( ) {
50 @Override
51 public void stateChanged ( ChangeEvent e ) {
52 red = ( ( JSlider ) e . getSource ( ) ) . getValue ( ) ;
53 if ( listener != null ) listener . onChange ( red , green , blue ) ;
54 }
55 } ) ;
56
57 JLabel greenLabel = new JLabel ( "G" ) ;
58 JSlider greenSlider = new JSlider ( JSlider . HORIZONTAL , 0 , 255 , 0 ) ;
59 greenSlider . addChangeListener ( new ChangeListener ( ) {
60 @Override
61 public void stateChanged ( ChangeEvent e ) {
62 green = ( ( JSlider ) e . getSource ( ) ) . getValue ( ) ;
63 if ( listener != null ) listener . onChange ( red , green , blue ) ;
64 }
65 } ) ;
66
67 JLabel blueLabel = new JLabel ( "B" ) ;
68 JSlider blueSlider = new JSlider ( JSlider . HORIZONTAL , 0 , 255 , 0 ) ;
69 blueSlider . addChangeListener ( new ChangeListener ( ) {
70 @Override
71 public void stateChanged ( ChangeEvent e ) {
72 blue = ( ( JSlider ) e . getSource ( ) ) . getValue ( ) ;
73 if ( listener != null ) listener . onChange ( red , green , blue ) ;
74 }
75 } ) ;
76
77 GridBagConstraints gbc = new GridBagConstraints ( ) ;
78 gbc . gridx = 0 ;
79 gbc . gridy = 0 ;
80 add ( redLabel , gbc ) ;
81 gbc . gridx ++ ;
82 add ( Box . createHorizontalStrut ( 5 ) , gbc ) ;
83 gbc . gridx ++ ;
84 add ( redSlider , gbc ) ;
85 gbc . gridx = 0 ;
86 gbc . gridy ++ ;
87 add ( Box . createVerticalStrut ( 10 ) , gbc ) ;
88 gbc . gridx = 0 ;
89 gbc . gridy ++ ;
90 add ( greenLabel , gbc ) ;
91 gbc . gridx = 2 ;
92 add ( greenSlider , gbc ) ;
93 gbc . gridx = 0 ;
94 gbc . gridy ++ ;
95 add ( Box . createVerticalStrut ( 10 ) , gbc ) ;
96 gbc . gridx = 0 ;
97 gbc . gridy ++ ;
98 add ( blueLabel , gbc ) ;
99 gbc . gridx = 2 ;
100 add ( blueSlider , gbc ) ;
101 }
102
103 void setChangeListener ( Listener listener ) {
104 this . listener = listener ;
105 if ( listener != null ) listener . onChange ( red , green , blue ) ; //初期表示用
106 }
107 }