使用jframe的组件时从用户检索输入

时间:2018-08-07 21:13:38

标签: jframe jpanel jtextfield jcombobox jradiobutton

我已经创建了一个我希望用户填写的表单的基本模板。但是,每当我运行该程序并尝试输入信息时,当我打印出所输入的内容时,我只会得到一个空字符串。

这是我无法弄清楚的代码部分:

   JPanel p = new JPanel();
   GroupLayout layout = new GroupLayout(p);
   p.setLayout(layout);

   //Here I want the user to click on a button and for the program to save what the user clicked

   String type = "";

   JRadioButton mov = new JRadioButton("Movie");
   if(mov.isSelected()) type += "Movie";

   JRadioButton tv = new JRadioButton("TV Show");
   if(tv.isSelected()) type += "TV Show";

   ButtonGroup group = new ButtonGroup();
   group.add(mov);
   group.add(tv);      

   //Here I want the user to imput the name of what he is watching, and I want to be able to save that name.

   JLabel name = new JLabel("Name: ");

   JTextField name1 = new JTextField();
   String name2 = name1.getText();

   //Here i want the user to input the genre, and i want to be able to save the genre.

   JLabel genre = new JLabel("Genre: ");

   JTextField genre1 = new JTextField();
   String genre2 = genre1.getText();

   //Here i want the user to rate what he is watching by selecting a number in the combobox, and i want to store what is selected.

   JLabel rating = new JLabel("Rating: ");

   String score[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
   JComboBox rating1 = new JComboBox(score);
   String rating2 = rating1.getSelectedItem();

   //Below is the submit button. When the user clicks submit, i want to print out everything that was inputted into the form.

   JButton submit = new JButton("Submit");
   AddBH bh4 = new AddBH(this, type1, name2, genre2, rating2);
   submit.addActionListener(bh4);

该按钮可以正常工作,但是它会继续打印空字符串以表示类型,名称和流派,而对于评级,即使我选择另一个数字,它仍会打印0。我不确定我在做什么错。

0 个答案:

没有答案