JComboBox - Java (241591)

Узнай цену своей работы

Формулировка задачи:

Помогите разобраться с JCOMBOBOX. Есть два combobox. Когда пользователь выбирает объект, как поймать его выбор.
Листинг программы
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. public class EShop {
  6. private JTextField df;
  7. public void setDisplayValue(String val){
  8. df.setText(val);
  9. }
  10. public String getDisplayValue(){
  11. return df.getText();
  12. }
  13. public static void main(String[] args) {
  14.  
  15. JPanel p1;
  16. JButton button0;
  17. JPanel esh = new JPanel();
  18. JTextField dfield;
  19. button0 = new JButton("Buy");
  20. p1 = new JPanel();
  21. String [] items1 = {
  22. "Bike Arlenok",
  23. "Bike Ukraine"
  24. };
  25. JComboBox comboBox = new JComboBox(items1);
  26. Integer [] items2 = {
  27. 1,
  28. 2,
  29. 3,
  30. 4,
  31. 5,
  32. 6,
  33. 7,
  34. 8,
  35. 9,
  36. 10,
  37. 15,
  38. 20,
  39. 25,
  40. };
  41. JComboBox cb = new JComboBox(items2);
  42. JLabel label1 = new JLabel("Enter bike");
  43. JLabel label2 = new JLabel("how much");
  44. JTextField field = new JTextField(10);
  45. esh.add(label1);
  46. esh.add(comboBox);
  47. esh.add(label2);
  48. esh.add(cb);
  49. esh.add(button0);
  50. esh.add(field);
  51. p1 = new JPanel();
  52. GridLayout gl = new GridLayout(10,30);
  53. p1.setLayout(gl);
  54.  
  55. JFrame frame = new JFrame("E-Shop");
  56. frame.setContentPane(esh);
  57. frame.setVisible(true);
  58. frame.setSize(250, 300);
  59. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  60.  
  61. ActionListener actionListener = new ActionListener() {
  62. @Override
  63. public void actionPerformed(ActionEvent e) {
  64.  
  65. }
  66. };
  67. comboBox.addActionListener(actionListener);
  68. cb.addActionListener(actionListener);
  69. button0.addActionListener(actionListener);
  70. }
  71. }

Решение задачи: «JComboBox»

textual
Листинг программы
  1. String st=cb.getSelectedItem().toString();

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 4.222 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут