2017-05-05 59 views
1

这是我迄今为止的代码。如何从combobox java中显示选定的项目?

import java.util.*; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.Action; 
import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import java.awt.event.*; 



import java.awt.Component; 


public class MAIN { 
    public static void main(String[] args) { 

     String grade = null, sub = null; 
     int x = 0; 
     ALevel[] array = new ALevel[3]; 

     for (x = 0; x < 3; x++) { 
     JFrame frame = new JFrame("COURSE CHECKER"); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(500, 500); 
     frame.setLocation(430, 100); 

     JPanel panel = new JPanel(); 
     panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 

     frame.add(panel); 

     JLabel lbl = new JLabel("Select your subject(s):"); 
     lbl.setAlignmentX(Component.CENTER_ALIGNMENT); 
     //lbl.setVisible(true); 

     panel.add(lbl); 

     //define items in a String array 
     String[] choices = { "MATHEMATICS", "FURTHER MATHEMATICS", "ACCOUNTING", "ECONOMICS", 
          "MANAGEMENT OF BUSINESS", "COMPUTER STUDIES", "PHYSICS", "CHEMISTRY", 
          "BIOLOGY", "DESIGN & TECHNOLOGY", "GEOGRAPHY"}; 


     //create combobox/dropdown with the fixed array 
     final JComboBox<String> cb = new JComboBox<String>(choices); 

     cb.setMaximumSize(cb.getPreferredSize()); 
     cb.setAlignmentX(Component.CENTER_ALIGNMENT); 
     //cb.setVisible(true); 
     panel.add(cb); 

     frame.setVisible(true); 

     array[x] = new ALevel(grade, sub); 
     array[x].gradecheck(sub, array[x].getaGrade()); 
    } 

     cb.addActionListener(new ActionListener(){ 
      public void actionPerformed(Action e){ 
       String data = "Subject Selected: " + cb.getItemAt(cb.getSelectedIndex()); 
        lbl.setText(data); 
      } 

     }; 

     JLabel lbl1 = new JLabel("Select your grades"); 
     lbl1.setAlignmentX(Component.CENTER_ALIGNMENT); 
     //lbl.setVisible(true); 

     panel.add(lbl1); 

     //define items in a String array 
     String[] choices1 = { "A", "B", "C", "D", "E"}; 

     //create combobox/dropdown with the fixed array 
     final JComboBox<String> cb1 = new JComboBox<String>(choices1); 

     cb1.setMaximumSize(cb1.getPreferredSize()); 
     cb1.setAlignmentX(Component.CENTER_ALIGNMENT); 
     //cb.setVisible(true); 
     panel.add(cb1); 

     JButton btn1 = new JButton("OK"); 
     btn1.setAlignmentX(Component.CENTER_ALIGNMENT); 
     panel.add(btn1); 

     frame.setVisible(true); 


    for (x = 0; x < 3; x++) { 
     System.out.println("\nSubject: " + array[x].getaSub() + "\nGrade:" + array[x].getaGrade()); 
    } 
    System.out.println("\nPoint: " + array[0].getaScore()); 



     // 
     Scanner input = new Scanner(System.in); 
     String grade = null, sub = null; 
     int x = 0; 
     ALevel[] array = new ALevel[3]; 

     for (x = 0; x < 3; x++) { 
      System.out.print("Enter subject: "); 
      sub = input.nextLine(); 
      System.out.print("Enter grade: "); 
      grade = input.nextLine(); 

      array[x] = new ALevel(grade, sub); 
      array[x].gradecheck(sub, array[x].getaGrade()); 
     } 

     for (x = 0; x < 3; x++) { 
      System.out.println("\nSubject: " + array[x].getaSub() + "\nGrade:" + array[x].getaGrade()); 
     } 
     System.out.println("\nPoint: " + array[0].getaScore()); 
     System.out.println("You are eligible for: "); 

     for (x = 0; x < 3; x++) { 
      if (array[x].getSubjects() == 3) { 
       if (array[x].isMathcheck() == true && array[x].getSubscoreE() >= 3 && array[x].getaScore() >= 200) { 
        array[x].SCI(); 
       } 
       if (array[x].getSubscoreE() >= 3 && array[x].getaScore() >= 200) { 
        array[x].SOB(); 
       } 
       if (array[x].isMathcheck() == true && array[x].getSubscoreS() >= 2 && array[x].getaScore() >= 240) { 
        array[x].CEPA(); 
       } 
       if (array[x].isMathcheck() == true && array[x].isPhyscheck() == true && array[x].getSubscoreS() >= 2 
         && array[x].getaScore() >= 220) { 
        array[x].E3PA(); 
       } 
       if (array[x].isMathcheck() == true && array[x].isPhyscheck() == true && array[x].isChemcheck() == true 
         && array[x].getaScore() >= 240) { 
        array[x].PCEPA(); 
       } 
       if (array[x].isMathcheck() == true && array[x].isPhyscheck() == true && array[x].getSubscoreS() >= 2 
         && array[x].getaScore() >= 240) { 
        array[x].MEPA(); 
       } 
      } 
      if (array[x].getSubjects() == 2) { 
       if (array[x].isMathcheck() == true && array[x].getSubscoreE() >= 2 && array[x].getaScore() >= 180) { 
        array[x].SCI(); 
       } 
       if (array[x].getSubscoreE() >= 2 && array[x].getaScore() >= 180) { 
        array[x].SOB(); 
       } 
       if (array[x].isMathcheck() == true && array[x].isPhyscheck() == true && array[x].getaScore() >= 180) { 
        array[x].E3PA(); 
       } 
       if (array[x].isMathcheck() == true && array[x].isPhyscheck() == true && array[x].getaScore() >= 180) { 
        array[x].MEPA(); 
       } 
      } 
      if (array[x].getaScore() < 180) { 
       System.out.print("None"); 
      } 
      System.exit(0); 
     }// 
    } 
} 

编码工作正常,没有组合框和数据在控制台选项卡中dsiplay。

我想通过从组合框中选择数据而不是输入输入来简化它。 那么,我如何显示从组合框中选择的项目?

+0

'cb.getSelectedItem()',不是吗?我不确定你在问什么。 – DCON

+0

如果你编辑你编写的问题和邮政编码,这对其他人来说会更容易帮助你。 – VGR

回答

0

您需要从组合框中获取“selected item”。

然后,您可以更新您的标签,文本区域或其他任何与您从组合框中抓取的数据。

JComboBox cb = (JComboBox)e.getSource(); 
String myName = (String)cb.getSelectedItem(); 
updateLabel(myName); 

注意cb.getSelectedItem()。这实际上是从组合框中获取数据。

下面是有关使用Java组合框一些更多的信息的好资源:https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html

+1

谢谢!这很有帮助。 – user3439948

+0

@ user3439948没问题。如果它帮助你将其标记为“接受的答案”(复选标记),以便其他用户可以看到它,那么当你已经解决问题时,人们不必再费心回答你的问题。 – coinbird