fork download
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package swingdemo;
  7.  
  8. import java.awt.FlowLayout;
  9. import java.awt.Font;
  10. import java.awt.HeadlessException;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.util.Random;
  14. import java.util.Vector;
  15. import javax.swing.BoxLayout;
  16. import javax.swing.JButton;
  17. import javax.swing.JComboBox;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JList;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.JPanel;
  23. import javax.swing.JScrollPane;
  24.  
  25. /**
  26.  *
  27.  * @author hi
  28.  */
  29. public class JComboboxJListUI extends JFrame {
  30. JComboBox cbo;
  31. JList jlist;
  32. JButton jbt;
  33.  
  34. public JComboboxJListUI() throws HeadlessException {
  35. this.setSize(400,600);
  36. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37. this.setLocationRelativeTo(null);
  38. this.setVisible(true);
  39. creatAndShow();
  40. addEvent();
  41. }
  42.  
  43. private void creatAndShow() {
  44. JPanel jpn = new JPanel();
  45. jpn.setLayout(new BoxLayout(jpn,BoxLayout.Y_AXIS));
  46.  
  47. String arr[] = {"Xuất sắc","Giỏi","Khá","Trung bình"};
  48. cbo = new JComboBox(arr);
  49. jpn.add(cbo);
  50.  
  51.  
  52. JPanel jpnlist = new JPanel();
  53. jpnlist.setLayout(new FlowLayout());
  54. JLabel jlb = new JLabel("Chọn số : ");
  55. jlb.setFont(new Font("Font.BOLD",20,20));
  56. JList list = jlist();
  57. JScrollPane sc = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  58.  
  59. jpnlist.add(jlb);
  60. jpnlist.add(sc);
  61.  
  62. JPanel jpnbt = new JPanel();
  63. jbt = new JButton("OK");
  64. jbt.setFont(new Font("Font.BOLD",20,20));
  65. jpnbt.add(jbt);
  66.  
  67.  
  68. jpn.add(jpnlist);
  69. jpn.add(jpnbt);
  70. this.getContentPane().add(jpn);
  71.  
  72.  
  73. }
  74.  
  75. public JList jlist()
  76. {
  77. Random rd = new Random();
  78. Vector<Integer> vec = new Vector<>();
  79. for(int i=0;i<100;i++)
  80. {
  81.  
  82. int x = rd.nextInt(300);
  83. vec.add(x);
  84. }
  85.  
  86. jlist = new JList(vec);
  87. return jlist;
  88. }
  89.  
  90. public void addEvent()
  91. {
  92. cbo.addActionListener(new ActionListener() {
  93. @Override
  94. public void actionPerformed(ActionEvent e) {
  95. int vt = cbo.getSelectedIndex();
  96. if(vt!=-1)
  97. {
  98. JOptionPane.showMessageDialog(rootPane,"Vị trí được chọn : "+(vt+1)+" "+"Phần tử được chọn là :"+cbo.getSelectedItem());
  99.  
  100. }
  101.  
  102. else
  103. {
  104. JOptionPane.showMessageDialog(rootPane,"Không có phần tử nào được chọn");
  105. }
  106.  
  107. }
  108. });
  109.  
  110. jbt.addActionListener(new ActionListener() {
  111. @Override
  112. public void actionPerformed(ActionEvent e) {
  113. Object obj = jlist.getSelectedValue();
  114. int vt = jlist.getSelectedIndex();
  115. JOptionPane.showMessageDialog(rootPane, "Phần tử được chọn là " + obj + " "+ "Vị trí được chọn là : "+vt );
  116.  
  117. }
  118. });
  119. }
  120.  
  121. public static void main(String[] args) {
  122. new JComboboxJListUI();
  123. }
  124.  
  125. }
  126.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:29: error: class JComboboxJListUI is public, should be declared in a file named JComboboxJListUI.java
public class JComboboxJListUI extends JFrame {
       ^
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
stdout
Standard output is empty