fork download
  1.  
  2. import java.awt.Color;
  3. import java.awt.Container;
  4. import java.awt.Dimension;
  5. import java.awt.FlowLayout;
  6. import java.awt.Font;
  7. import javax.swing.JFrame;
  8. import javax.swing.JList;
  9. import javax.swing.JPanel;
  10. import javax.swing.JScrollPane;
  11. import javax.swing.event.ListSelectionEvent;
  12. import javax.swing.event.ListSelectionListener;
  13.  
  14. /*
  15.  * To change this license header, choose License Headers in Project Properties.
  16.  * To change this template file, choose Tools | Templates
  17.  * and open the template in the editor.
  18.  */
  19.  
  20. /**
  21.  *
  22.  * @author ta
  23.  */
  24. public class DemoJcomJlist extends JFrame{
  25.  
  26. JList<String> jlist ;
  27. Color [] color = {Color.BLUE,Color.BLACK,Color.DARK_GRAY,Color.PINK,Color.darkGray} ;
  28. String [] colorname = { "BLUE" , "BLACK" , "DARK_GRAY" , "PINK" , "DARK_GRAY" };
  29.  
  30. public DemoJcomJlist(String title) {
  31. super(title);
  32. this.setSize(200,250);
  33. this.getContentPane().setLayout(new FlowLayout());
  34.  
  35. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36. this.setLocationRelativeTo(null);
  37. this.setVisible(true);
  38. creatAndShow();
  39.  
  40. }
  41.  
  42. public void creatAndShow() {
  43.  
  44.  
  45. Container con = this.getContentPane();
  46. JPanel jpn = new JPanel(new FlowLayout());
  47. jpn.setBackground(Color.red);
  48.  
  49.  
  50. Font font = new Font("Font.BOLD",20,20);
  51. jlist = createJlist();
  52. // KHong hien la cai dimension
  53. // jlist.setPreferredSize(new Dimension(100,150));
  54.  
  55. jpn.add(jlist);
  56. con.add(jpn);
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. }
  64.  
  65.  
  66.  
  67. public JList createJlist()
  68. {
  69.  
  70. jlist = new JList<String>(colorname);
  71. jlist.addListSelectionListener(new ListSelectionListener() {
  72. @Override
  73. public void valueChanged(ListSelectionEvent e) {
  74. changebackground();
  75. }
  76. });
  77.  
  78. return jlist;
  79.  
  80. }
  81.  
  82.  
  83.  
  84. public void changebackground()
  85. {
  86. getContentPane().setBackground(color[jlist.getSelectedIndex()]);
  87.  
  88. }
  89.  
  90. public static void main(String[] args) {
  91. new DemoJcomJlist("Color");
  92.  
  93. }
  94.  
  95.  
  96. }
  97.  
  98. import java.awt.Color;
  99. import java.awt.Container;
  100. import java.awt.Dimension;
  101. import java.awt.FlowLayout;
  102. import java.awt.Font;
  103. import javax.swing.JFrame;
  104. import javax.swing.JList;
  105. import javax.swing.JPanel;
  106. import javax.swing.JScrollPane;
  107. import javax.swing.event.ListSelectionEvent;
  108. import javax.swing.event.ListSelectionListener;
  109.  
  110. /*
  111.  * To change this license header, choose License Headers in Project Properties.
  112.  * To change this template file, choose Tools | Templates
  113.  * and open the template in the editor.
  114.  */
  115.  
  116. /**
  117.  *
  118.  * @author ta
  119.  */
  120. public class DemoJcomJlist extends JFrame{
  121.  
  122. JList<String> jlist ;
  123. Color [] color = {Color.BLUE,Color.BLACK,Color.DARK_GRAY,Color.PINK,Color.darkGray} ;
  124. String [] colorname = { "BLUE" , "BLACK" , "DARK_GRAY" , "PINK" , "DARK_GRAY" };
  125.  
  126. public DemoJcomJlist(String title) {
  127. super(title);
  128. this.setSize(200,250);
  129. this.getContentPane().setLayout(new FlowLayout());
  130.  
  131. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  132. this.setLocationRelativeTo(null);
  133. this.setVisible(true);
  134. creatAndShow();
  135.  
  136. }
  137.  
  138. public void creatAndShow() {
  139.  
  140.  
  141. Container con = this.getContentPane();
  142. JPanel jpn = new JPanel(new FlowLayout());
  143. jpn.setBackground(Color.red);
  144.  
  145.  
  146. Font font = new Font("Font.BOLD",20,20);
  147. jlist = createJlist();
  148. // KHong hien la cai dimension
  149. // jlist.setPreferredSize(new Dimension(100,150));
  150.  
  151. jpn.add(jlist);
  152. con.add(jpn);
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. }
  160.  
  161.  
  162.  
  163. public JList createJlist()
  164. {
  165.  
  166. jlist = new JList<String>(colorname);
  167. jlist.addListSelectionListener(new ListSelectionListener() {
  168. @Override
  169. public void valueChanged(ListSelectionEvent e) {
  170. changebackground();
  171. }
  172. });
  173.  
  174. return jlist;
  175.  
  176. }
  177.  
  178.  
  179.  
  180. public void changebackground()
  181. {
  182. getContentPane().setBackground(color[jlist.getSelectedIndex()]);
  183.  
  184. }
  185.  
  186. public static void main(String[] args) {
  187. new DemoJcomJlist("Color");
  188.  
  189. }
  190.  
  191.  
  192. }
  193.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:98: error: class, interface, or enum expected
import java.awt.Color;
^
Main.java:99: error: class, interface, or enum expected
import java.awt.Container;
^
Main.java:100: error: class, interface, or enum expected
import java.awt.Dimension;
^
Main.java:101: error: class, interface, or enum expected
import java.awt.FlowLayout;
^
Main.java:102: error: class, interface, or enum expected
import java.awt.Font;
^
Main.java:103: error: class, interface, or enum expected
import javax.swing.JFrame;
^
Main.java:104: error: class, interface, or enum expected
import javax.swing.JList;
^
Main.java:105: error: class, interface, or enum expected
import javax.swing.JPanel;
^
Main.java:106: error: class, interface, or enum expected
import javax.swing.JScrollPane;
^
Main.java:107: error: class, interface, or enum expected
import javax.swing.event.ListSelectionEvent;
^
Main.java:108: error: class, interface, or enum expected
import javax.swing.event.ListSelectionListener;
^
11 errors
stdout
Standard output is empty