JAVA - 根据另一个下拉菜单下拉菜单项

时间:2017-11-18 01:59:19

标签: java combobox

我正在尝试制作2018年世界杯计划,该计划为每个参与的团队提供百分比和选择。我已经弄清楚了所有的数学和所有与之相关的东西,但是我无法找到如何从一个下拉菜单中选择下一个的选择,以此类推四组。我已经在本网站和其他网站上查找过相关信息,但由于我对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); 

    }
}

1 个答案:

答案 0 :(得分:0)

您需要使用事件和听众。

检查this other post它可能会有所帮助。