这是我到目前为止的代码。
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);
}//
}
}
编码在没有组合框的情况下运行良好,数据在控制台选项卡中显示为dsiplayed。
我希望通过从组合框中选择数据而不是输入输入来更轻松。 那么,我如何从组合框中显示所选项目?
答案 0 :(得分:0)
您需要获得&#34;所选项目&#34;来自组合框。
然后,您可以使用从组合框中抓取的数据更新标签,文本区域或其他任何内容。
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