2017-11-18 286 views
0

我正在尝试制作一个世界杯2018年计划,为每个参与团队提供百分比和选择。我已经想通了所有的数学和一切,但是我很难找到如何从一个下拉菜单中选择更改下一个选项的选项等等。我已经在这个网站和其他网站上寻找信息,但是因为我对java很陌生,所以我不容易找出该怎么做。JAVA - 基于另一个下拉菜单下拉菜单项

我已经在下面列出了我的代码,有四个下拉菜单,并且想知道如何根据您在第一组中做出的选择给出第二组中的正确选择,依此类推...

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.BoxLayout; 
import java.awt.Component; 

public class Worldcup { 

public static void main(String[] args) { 

    JFrame frame = new JFrame("A Simple GUI"); 
    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); 

    //POT 1 

    JLabel lbl = new JLabel("Select one of the possible choices and click OK"); 
    lbl.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.add(lbl); 

    String[] choices = { "Russia", "Germany", "France", "Portugal", 
         "Belgium", "Poland","Brazil", "Argentina"}; 

    final JComboBox<String> cb = new JComboBox<String>(choices); 

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

    JButton btn = new JButton("OK"); 
    btn.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.add(btn); 
    frame.setVisible(true); 


// POT 2 

JLabel lbl2 = new JLabel("Select one of the possible choices and click OK"); 
    lbl2.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.add(lbl2); 

    String[] choices2 = { "Spain", "Switzerland", "England", "Croatia", 
         "Peru", "Colombia","Uruguay", "Mexico"}; 

    final JComboBox<String> cb2 = new JComboBox<String>(choices2); 

    cb2.setMaximumSize(cb2.getPreferredSize()); 
    cb2.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.add(cb2); 

    JButton btn2 = new JButton("OK"); 
    btn2.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.add(btn2); 
    frame.setVisible(true); 

// POT 3 

JLabel lbl3 = new JLabel("Select one of the possible choices and click OK"); 
    lbl3.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.add(lbl3); 

    String[] choices3 = { "Denmark", "Iceland", "Sweden", "Costa Rica", 
         "Senegal", "Egypt","Tunisia", "IRAN"}; 

    final JComboBox<String> cb3 = new JComboBox<String>(choices3); 

    cb3.setMaximumSize(cb3.getPreferredSize()); 
    cb3.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.add(cb3); 

    JButton btn3 = new JButton("OK"); 
    btn3.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.add(btn3); 
    frame.setVisible(true); 

// POT 4 

JLabel lbl4 = new JLabel("Select one of the possible choices and click OK"); 
    lbl4.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.add(lbl4); 

    String[] choices4 = { "Serbia", "Nigeria", "Morocco", "Australia", 
         "Japan", "South Korea","Crappy Arabia", "Panama"}; 

    final JComboBox<String> cb4 = new JComboBox<String>(choices4); 

    cb4.setMaximumSize(cb4.getPreferredSize()); 
    cb4.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.add(cb4); 

    JButton btn4 = new JButton("OK"); 
    btn4.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.add(btn4); 
    frame.setVisible(true); 

    } 
} 

回答