2017-11-18 272 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); 

    } 
} 

回答