2017-05-05 98 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

相關問題