Загвоздка по JComboBox - Java

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

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

Приветствую коллеги! Пишу программу для заполнения формы документа. Реализовал вывод данных на форму документа из фрейма для заполнения, но возникла задача с JComboBox так как на документ выводится данные находящиеся в начале списка и выбор пользователя совершенно не влияет на вывод данных из JCB(выводится только первый элемент в списке).
Листинг программы
  1. public class CreateForm extends JFrame implements ActionListener {
  2.  
  3. SpringLayout sl = new SpringLayout();
  4. JPanel panel = new JPanel(sl);
  5. JLabel jlFormSp; // форма справки
  6. JButton searchJB;
  7. JComboBox jtFormSp; // форма справки
  8. JLabel jlNumberSp; // номер справки
  9. JTextField jtNumberSp; // номер справки
  10. JCheckBox jCBox; //автомат номера справки
  11. JLabel jlDateSp; // дата справки
  12. String[] days = {"01","02","03","04","05","06","07","08","09","10",
  13. "11","12","13","14","15","16","17","18","19","20",
  14. "21","22","23","24","25","26","27","28","29","30","31"};
  15. String [] months = {"января", "февраля", "марта", "апреля","мая",
  16. "июня", "июля", "августа", "сентября", "октября",
  17. "ноября", "декабря"};
  18. String[] forms = {"1","2","3"};
  19. String[] wFromSp = {"первой форме", "второй форме", "третьей форме"};
  20. String[] litFormSp = {"А/","Б/","Д/"};
  21. JComboBox<?> daysJCB; // список дней
  22. JComboBox<?> monthsJCB; // список месяцев
  23. JTextField yearsJTF; // поле год
  24. JTextField secondNameJTF; // Фамилия
  25. JLabel secondNameJL; // Фамилия
  26. JTextField nameJTF; // Имя
  27. JLabel nameJL; //Имя
  28. JTextField fatherNameJTF; // Отчество
  29. JLabel fatherNameJL; // Отчество
  30. JLabel numDopJL; // Номер допуска
  31. JTextField numDopJTF; // Номер допуска
  32. JLabel whatDateJL;
  33. JComboBox<?> whatDateDayJCB;
  34. JComboBox<?> whatDateMonthJCB;
  35. JTextField whatDateYearJTF;
  36. JLabel whatDateAcceptJL;
  37. JComboBox<?> whatDateAcceptDayJCB;
  38. JComboBox<?> whatDateAcceptMonthJCB;
  39. JTextField whatDateAcceptYearJTF;
  40. JLabel whatFinDateJL;
  41. JComboBox<?> whatFinDateDayJCB;
  42. JComboBox<?> whatFinDateMonthJCB;
  43. JTextField whatFinDateYearJTF;
  44. JLabel whatFormSpJL;
  45. JComboBox<?> whatFormSpJCB;
  46. JComboBox<?> litDopJCB;
  47.  
  48. String formSpCF;
  49. String whatFormSpCF;
  50. String numberSpCF;
  51. String daysSpCF;
  52. String monthsSpCF;
  53. String litDopCF;
  54. String whatDaysDopCF;
  55. String whatMonthsDopCF;
  56. String wDateAcceptDayCF;
  57. String wDateAcceptMonthCF;
  58. String wDateFinDayCF;
  59. String wDateFinMonthCF;
  60.  
  61. public CreateForm(String string) {
  62. setSize(1000, 800);
  63. setLocation(200, 200);
  64. panel.setSize(100, 50);
  65. panel.add(jlFormSp = new JLabel("Форма справки"));
  66. panel.add(jlNumberSp = new JLabel("Номер справки"));
  67. panel.add(jtFormSp = new JComboBox (forms)); // форма справки
  68. panel.add(jtNumberSp = new JTextField (4)); // номер справки
  69. panel.add(jCBox = new JCheckBox("Автоматически")); jCBox.setEnabled(true); // автомат номера справки
  70. panel.add(jlDateSp = new JLabel("Дата создания справки"));
  71. panel.add(daysJCB = new JComboBox<Object>(days)); // дни даты
  72. panel.add(monthsJCB = new JComboBox<Object>(months)); // месяцы даты
  73. panel.add(yearsJTF = new JTextField(4));
  74. panel.add(secondNameJTF = new JTextField(20));
  75. panel.add(secondNameJL = new JLabel("Фамилия"));
  76. panel.add(nameJTF = new JTextField(20));
  77. panel.add(nameJL = new JLabel("Имя"));
  78. panel.add(fatherNameJTF = new JTextField(20));
  79. panel.add(fatherNameJL = new JLabel("Отчество"));
  80. panel.add(searchJB = new JButton("Создать форму"));
  81. panel.add(numDopJL = new JLabel("Номер допуска"));
  82. panel.add(numDopJTF = new JTextField(10));
  83. panel.add(whatDateJL = new JLabel("от..."));
  84. panel.add(whatDateDayJCB = new JComboBox<Object>(days));
  85. panel.add(whatDateMonthJCB = new JComboBox<Object>(months));
  86. panel.add(whatDateYearJTF = new JTextField(5));
  87. panel.add(whatDateAcceptJL = new JLabel("Дата утверждения"));
  88. panel.add(whatDateAcceptDayJCB = new JComboBox(days));
  89. panel.add(whatDateAcceptMonthJCB = new JComboBox<Object>(months));
  90. panel.add(whatDateAcceptYearJTF = new JTextField(5));
  91. panel.add(whatFinDateJL = new JLabel("Дата окончания справки"));
  92. panel.add(whatFinDateDayJCB = new JComboBox<Object>(days));
  93. panel.add(whatFinDateMonthJCB = new JComboBox<Object>(months));
  94. panel.add(whatFinDateYearJTF = new JTextField(5));
  95. panel.add(whatFormSpJCB = new JComboBox<Object>(wFromSp));
  96. panel.add(whatFormSpJL = new JLabel("создать справку по ..."));
  97. panel.add(litDopJCB = new JComboBox<Object>(litFormSp));
  98.  
  99. sl.putConstraint(SpringLayout.WEST, jlFormSp, 15, SpringLayout.WEST, panel);
  100. sl.putConstraint(SpringLayout.NORTH, jlFormSp, 15, SpringLayout.WEST, panel);
  101. sl.putConstraint(SpringLayout.WEST, jtFormSp, 130, SpringLayout.WEST, panel);
  102. sl.putConstraint(SpringLayout.NORTH, jtFormSp, 15, SpringLayout.WEST, panel);
  103. sl.putConstraint(SpringLayout.NORTH, jlNumberSp, 50, SpringLayout.WEST, panel);
  104. sl.putConstraint(SpringLayout.WEST, jlNumberSp, 15, SpringLayout.WEST, panel);
  105. sl.putConstraint(SpringLayout.NORTH, jtNumberSp, 50, SpringLayout.WEST, panel);
  106. sl.putConstraint(SpringLayout.WEST, jtNumberSp, 130, SpringLayout.WEST, panel);
  107. sl.putConstraint(SpringLayout.NORTH, jCBox, 90, SpringLayout.WEST,panel);
  108. sl.putConstraint(SpringLayout.WEST, jCBox, 350, SpringLayout.WEST, panel);
  109. sl.putConstraint(SpringLayout.NORTH, jlDateSp, 90, SpringLayout.WEST, panel);
  110. sl.putConstraint(SpringLayout.WEST, jlDateSp, 15, SpringLayout.WEST, panel);
  111. sl.putConstraint(SpringLayout.NORTH, daysJCB, 90, SpringLayout.WEST, panel);
  112. sl.putConstraint(SpringLayout.WEST, daysJCB, 160, SpringLayout.WEST, panel);
  113. sl.putConstraint(SpringLayout.NORTH, monthsJCB, 90, SpringLayout.WEST, panel);
  114. sl.putConstraint(SpringLayout.WEST, monthsJCB, 210, SpringLayout.WEST, panel);
  115. sl.putConstraint(SpringLayout.NORTH, yearsJTF, 90, SpringLayout.WEST, panel);
  116. sl.putConstraint(SpringLayout.WEST, yearsJTF, 300, SpringLayout.WEST, panel);
  117. sl.putConstraint(SpringLayout.WEST, secondNameJL, 15, SpringLayout.WEST, panel);
  118. sl.putConstraint(SpringLayout.NORTH, secondNameJL, 130, SpringLayout.WEST, panel);
  119. sl.putConstraint(SpringLayout.NORTH, secondNameJTF, 130, SpringLayout.WEST, panel);
  120. sl.putConstraint(SpringLayout.WEST, secondNameJTF, 130, SpringLayout.WEST, panel);
  121. sl.putConstraint(SpringLayout.WEST, nameJL, 15, SpringLayout.WEST, panel);
  122. sl.putConstraint(SpringLayout.NORTH, nameJL, 170, SpringLayout.WEST, panel);
  123. sl.putConstraint(SpringLayout.NORTH, nameJTF, 170, SpringLayout.WEST, panel);
  124. sl.putConstraint(SpringLayout.WEST, nameJTF, 130, SpringLayout.WEST, panel);
  125. sl.putConstraint(SpringLayout.NORTH, fatherNameJL, 210, SpringLayout.WEST, panel);
  126. sl.putConstraint(SpringLayout.WEST, fatherNameJL, 15, SpringLayout.WEST, panel);
  127. sl.putConstraint(SpringLayout.NORTH, fatherNameJTF, 210, SpringLayout.WEST, panel);
  128. sl.putConstraint(SpringLayout.WEST, fatherNameJTF, 130, SpringLayout.WEST, panel);
  129. sl.putConstraint(SpringLayout.NORTH, numDopJL, 250, SpringLayout.WEST, panel);
  130. sl.putConstraint(SpringLayout.WEST, numDopJL, 15, SpringLayout.WEST, panel);
  131. sl.putConstraint(SpringLayout.NORTH, numDopJTF, 250, SpringLayout.WEST, panel);
  132. sl.putConstraint(SpringLayout.WEST, numDopJTF, 180, SpringLayout.WEST, panel);
  133. sl.putConstraint(SpringLayout.NORTH, whatDateJL, 250, SpringLayout.WEST, panel);
  134. sl.putConstraint(SpringLayout.WEST, whatDateJL, 320, SpringLayout.WEST, panel);
  135. sl.putConstraint(SpringLayout.NORTH, whatDateDayJCB, 250, SpringLayout.WEST, panel);
  136. sl.putConstraint(SpringLayout.WEST, whatDateDayJCB, 360, SpringLayout.WEST, panel);
  137. sl.putConstraint(SpringLayout.NORTH, whatDateMonthJCB, 250, SpringLayout.WEST, panel);
  138. sl.putConstraint(SpringLayout.WEST, whatDateMonthJCB, 410, SpringLayout.WEST, panel);
  139. sl.putConstraint(SpringLayout.WEST, whatDateYearJTF, 500, SpringLayout.WEST, panel);
  140. sl.putConstraint(SpringLayout.NORTH, whatDateYearJTF, 250, SpringLayout.WEST, panel);
  141. sl.putConstraint(SpringLayout.WEST, whatDateAcceptJL, 245, SpringLayout.WEST, panel);
  142. sl.putConstraint(SpringLayout.NORTH, whatDateAcceptJL, 300, SpringLayout.WEST, panel);
  143. sl.putConstraint(SpringLayout.NORTH, whatDateAcceptDayJCB, 300, SpringLayout.WEST, panel);
  144. sl.putConstraint(SpringLayout.WEST, whatDateAcceptDayJCB,360, SpringLayout.WEST, panel);
  145. sl.putConstraint(SpringLayout.NORTH, whatDateAcceptMonthJCB, 300, SpringLayout.WEST, panel);
  146. sl.putConstraint(SpringLayout.WEST, whatDateAcceptMonthJCB, 410, SpringLayout.WEST, panel);
  147. sl.putConstraint(SpringLayout.NORTH, whatDateAcceptYearJTF, 300, SpringLayout.WEST, panel);
  148. sl.putConstraint(SpringLayout.WEST, whatDateAcceptYearJTF, 500, SpringLayout.WEST, panel);
  149. sl.putConstraint(SpringLayout.NORTH, whatFinDateJL, 350, SpringLayout.WEST, panel);
  150. sl.putConstraint(SpringLayout.WEST, whatFinDateJL, 15, SpringLayout.WEST, panel);
  151. sl.putConstraint(SpringLayout.NORTH, whatFinDateDayJCB, 350, SpringLayout.WEST, panel);
  152. sl.putConstraint(SpringLayout.WEST, whatFinDateDayJCB, 200, SpringLayout.WEST, panel);
  153. sl.putConstraint(SpringLayout.NORTH, whatFinDateMonthJCB, 350, SpringLayout.WEST, panel);
  154. sl.putConstraint(SpringLayout.WEST, whatFinDateMonthJCB, 250, SpringLayout.WEST, panel);
  155. sl.putConstraint(SpringLayout.NORTH, whatFinDateYearJTF, 350, SpringLayout.WEST, panel);
  156. sl.putConstraint(SpringLayout.WEST, whatFinDateYearJTF, 340, SpringLayout.WEST, panel);
  157. sl.putConstraint(SpringLayout.NORTH, whatFormSpJCB, 15, SpringLayout.WEST, panel);
  158. sl.putConstraint(SpringLayout.WEST, whatFormSpJCB, 320, SpringLayout.WEST, panel);
  159. sl.putConstraint(SpringLayout.NORTH, whatFormSpJL, 15, SpringLayout.WEST, panel);
  160. sl.putConstraint(SpringLayout.WEST, whatFormSpJL, 190, SpringLayout.WEST, panel);
  161. sl.putConstraint(SpringLayout.NORTH, litDopJCB, 250, SpringLayout.WEST, panel);
  162. sl.putConstraint(SpringLayout.WEST, litDopJCB, 130, SpringLayout.WEST, panel);
  163.  
  164. sl.putConstraint(SpringLayout.NORTH, searchJB, 400, SpringLayout.WEST, panel);
  165. sl.putConstraint(SpringLayout.WEST, searchJB, 25, SpringLayout.WEST, panel);
  166. formSpCF = (String) jtFormSp.getSelectedItem();
  167. whatFormSpCF = (String) whatFormSpJCB.getSelectedItem();
  168. daysSpCF = (String) daysJCB.getSelectedItem();
  169. monthsSpCF = (String) monthsJCB.getSelectedItem();
  170. litDopCF = (String) litDopJCB.getSelectedItem();
  171. whatDaysDopCF = (String) whatDateDayJCB.getSelectedItem();
  172. whatMonthsDopCF = (String) whatDateMonthJCB.getSelectedItem();
  173. wDateAcceptDayCF = (String) whatDateAcceptDayJCB.getSelectedItem();
  174. wDateAcceptMonthCF = (String) whatDateAcceptMonthJCB.getSelectedItem();
  175. wDateFinDayCF = (String) whatFinDateDayJCB.getSelectedItem();
  176. wDateFinMonthCF = (String) whatFinDateMonthJCB.getSelectedItem();
  177.  
  178. searchJB.addActionListener(this);
  179.  
  180. setContentPane(panel);
  181. setVisible(true);
  182.  
  183. }
  184.  
  185. public void actionPerformed(ActionEvent sjb) {
  186. if (sjb.getActionCommand().equals("Создать форму")){
  187. new FillForm(this);
  188. }
  189. }
  190.  
  191. }
Листинг программы
  1. class FillForm extends JFrame {
  2. public Image spravkaDva;
  3. SpringLayout inSL = new SpringLayout();
  4. JPanel panel;
  5. String name;
  6. String formSp;
  7. String numberSp;
  8. String whatFromSpJCBFF;
  9. String numberSpFF;
  10. String daysSpFF;
  11. String monthsSpFF;
  12. String yearsSpFF;
  13. String secondNameFF;
  14. String fatherNameFF;
  15. String litDopFF;
  16. String numDopFF;
  17. String wDaysDopFF;
  18. String wMonthsDopFF;
  19. String wYearsDopFF;
  20. String wAcceptDayFF;
  21. String wDateAcceptMonthFF;
  22. String wDateAcceptYearFF;
  23. String wDateFinDayFF;
  24. String wDateFinMonthFF;
  25. String wDateFinYearFF;
  26. public FillForm(CreateForm form) {
  27. super ("Форма справки");
  28. int width = 1174;
  29. int hegth = 1000;
  30. setBounds(0, 0, width, hegth);
  31. ImageIcon ii = new ImageIcon("C:/Users/User/Desktop/Java/Lesson0/src/Spravka3.jpg");
  32. spravkaDva = ii.getImage();
  33. panel = new ImagePanel(spravkaDva);
  34. panel.setBounds(0, 0, 400, 400);
  35.  
  36. formSp = form.formSpCF;
  37. whatFromSpJCBFF = form.whatFormSpCF;
  38. daysSpFF = form.daysSpCF;
  39. monthsSpFF = form.monthsSpCF;
  40. litDopFF = form.litDopCF;
  41. wDaysDopFF = form.whatDaysDopCF;
  42. wMonthsDopFF = form.whatMonthsDopCF;
  43. wAcceptDayFF = form.wDateAcceptDayCF;
  44. wDateAcceptMonthFF = form.wDateAcceptMonthCF;
  45. wDateFinDayFF = form.wDateFinDayCF;
  46. wDateFinMonthFF = form.wDateFinMonthCF;
  47. numberSpFF = form.jtNumberSp.getText();
  48. name = form.nameJTF.getText();
  49. yearsSpFF = form.yearsJTF.getText();
  50. secondNameFF = form.secondNameJTF.getText();
  51. fatherNameFF = form.fatherNameJTF.getText();
  52. numDopFF = form.numDopJTF.getText();
  53. wYearsDopFF = form.whatDateYearJTF.getText();
  54. wDateAcceptYearFF = form.whatDateAcceptYearJTF.getText();
  55. wDateFinYearFF = form.whatFinDateYearJTF.getText();
  56.  
  57. getContentPane().add(panel);
  58. setVisible(true);
  59. }
  60. private class ImagePanel extends JPanel{
  61. private Image img;
  62. public ImagePanel(Image img) {
  63. this.img = img;
  64. setLayout(null);
  65. }
  66. public void paintComponent(Graphics g) {
  67. g = (Graphics2D)g;
  68. g.drawImage(img, 0, 0, null);
  69. g.setFont(new Font("Times new Roman", Font.ROMAN_BASELINE, 26));
  70. g.drawString(name, 400, 435);
  71. g.drawString(formSp, 115, 347);
  72. g.drawString(whatFromSpJCBFF, 535, 137);
  73. g.drawString(numberSpFF, 170, 347);
  74. g.drawString(daysSpFF, 94, 390);
  75. g.drawString(monthsSpFF, 200, 387);
  76. g.drawString(yearsSpFF, 370, 390);
  77. g.drawString(secondNameFF, 530, 385);
  78. g.drawString(fatherNameFF, 520, 435);
  79. g.drawString(litDopFF, 250, 490);
  80. g.drawString(numDopFF, 280, 490);
  81. g.drawString(wDaysDopFF, 480, 490);
  82. g.drawString(wMonthsDopFF, 580, 490);
  83. g.drawString(wYearsDopFF, 740, 490);
  84. g.drawString(wAcceptDayFF, 108, 515);
  85. g.drawString(wDateAcceptMonthFF, 200, 515);
  86. g.drawString(wDateAcceptYearFF, 420, 515);
  87. g.drawString(wDateFinDayFF, 330, 610);
  88. g.drawString(wDateFinMonthFF, 380, 610);
  89. g.drawString(wDateFinYearFF, 540, 610);
  90. }
  91. }
  92.  
  93. }
Пожалуйста помогите разобраться...

Решение задачи: «Загвоздка по JComboBox»

textual
Листинг программы
  1. String frm;
  2. ............
  3. public FillForm(CreateForm form) {
  4. ...........
  5. frm = form.jtFormSp.getSelectedItem().toString();
  6. ...........
  7. }
  8.  
  9. public void paintComponent(Graphics g) {
  10. ............
  11. g.drawString(frm, 20, 20);
  12. }

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


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

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

15   голосов , оценка 3.933 из 5

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

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

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