Почему ComboBox не ссылается sh на новое значение - PullRequest
0 голосов
/ 22 апреля 2020

Я все еще учусь создавать GUI. Во-первых, я нажимаю JComboBox cb[0] и выбираю управление, и все остальные JComboBox, кажется, следуют за оператором if, но после того, как я изменяю JComboBox cb[0] на management, другие JComboBox не следуют за оператором if. Остальные JComboBox все еще придерживаются заявления оператора if.

import java.util.*;
import javax.swing.*;
import java.io.File;
import java.awt.event.*;
import java.awt.*;
import javax.swing.filechooser.*;
import java.io.IOException;

public class testGUI {
    JFrame f[]=new JFrame[10];
    JPanel pn[]=new JPanel[10];
    JLabel l[]=new JLabel[10];
    JButton bt[]=new JButton[10];
    JTextField tf[]=new JTextField[10];
    JTextArea ts[]=new JTextArea[10];
    JOptionPane op[]=new JOptionPane[10];
    JComboBox cb[]=new JComboBox[10];
    JRadioButton rb[]=new JRadioButton[10];
    ButtonGroup bg[]=new ButtonGroup[10];

    public testGUI() {
        f[4]=new JFrame("Register User");
        f[4].setDefaultCloseOperation(f[0].EXIT_ON_CLOSE);
        f[4].setSize(630,600);
        f[4].setResizable(false);
        f[4].setLayout(null);

        l[0]=new JLabel("Matric Number");
        l[0].setBounds(100,20,100,30);
        f[4].add(l[0]);
        tf[0]=new JTextField();
        tf[0].setBounds(220,25,200,30);
        f[4].add(tf[0]);

        l[1]=new JLabel("Job");
        l[1].setBounds(50,100,80,30);
        f[4].add(l[1]);
        String job[]= {"Management","Staff","Student"};
        cb[0]=new JComboBox(job);
        cb[0].setBounds(110,100,100,30);
        f[4].add(cb[0]);
        cb[0].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String s=(String) cb[0].getSelectedItem();
                if(s.equals("Management")) {
                    cb[2].addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            String m=(String) cb[2].getSelectedItem();
                            if(m.equals("Car")) {
                                cb[3].removeAllItems();
                                cb[3].addItem("Management");
                                cb[3].addItem("Car");
                                f[4].repaint();
                                System.out.println("eee");
                            }
                            else if(m.equals("Bike")) {
                                cb[3].removeAllItems();
                                cb[3].addItem("Management");
                                cb[3].addItem("Bike");
                                f[4].repaint();
                                System.out.println("hhh");
                            }
                            else {
                                cb[3].removeAllItems();
                                cb[3].addItem("N/A");
                                f[4].repaint();
                                System.out.println("fff");
                            }
                        }
                    }); 
                }
                else if(s.equals("Staff")) {
                    cb[2].addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            String m=(String) cb[2].getSelectedItem();
                            if(m.equals("Car")) {
                                cb[3].removeAllItems();
                                cb[3].addItem("Staff");
                                cb[3].addItem("Car");
                                f[4].add(cb[3]);
                                f[4].repaint();
                                System.out.println("aaa");
                            }
                            else if(m.equals("Bike")) {
                                cb[3].removeAllItems();
                                cb[3].addItem("Staff");
                                cb[3].addItem("Bike");
                                cb[3].setBounds(450,200,100,30);
                                f[4].add(cb[3]);
                                f[4].repaint();
                                System.out.println("www");
                            }
                            else {
                                cb[3].removeAllItems();
                                cb[3].addItem("N/A");
                                f[4].add(cb[3]);
                                f[4].repaint();
                                System.out.println("qqq");
                            }
                        }
                    }); 
                }
                else {
                    cb[2].addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            String m=(String) cb[2].getSelectedItem();
                            if(m.equals("Car")) {
                                cb[3].removeAllItems();
                                cb[3].addItem("Car");
                                f[4].add(cb[3]);
                                f[4].repaint();
                                System.out.println("ooo");
                            }
                            else if(m.equals("Bike")) {
                                cb[3].removeAllItems();
                                cb[3].addItem("Bike");
                                f[4].add(cb[3]);
                                f[4].repaint();
                                System.out.println("lll");
                            }
                            else {
                                cb[3].removeAllItems();
                                cb[3].addItem("N/A");
                                f[4].add(cb[3]);
                                f[4].repaint();
                                System.out.println("mmm");
                            }
                        }
                    }); 
                }
            } 
        }); 

        l[2]=new JLabel("No. of Vehicle");
        l[2].setBounds(350,100,100,30);
        f[4].add(l[2]);
        String vcNo[]= {"1","2","3"};
        cb[1]=new JComboBox(vcNo);
        cb[1].setBounds(450,100,100,30);
        f[4].add(cb[1]);

        l[3]=new JLabel("Vehicle Type");
        l[3].setBounds(80,150,100,30);
        f[4].add(l[3]);
        String vcType[]= {"Car","Bike","Bus","Lorry"};
        cb[2]=new JComboBox(vcType);
        cb[2].setBounds(70,200,100,30);
        f[4].add(cb[2]);

        l[4]=new JLabel("Plate No");
        l[4].setBounds(290,150,100,30);
        f[4].add(l[4]);
        tf[1]=new JTextField();
        tf[1].setBounds(250,200,130,30);
        f[4].add(tf[1]);

        l[5]=new JLabel("Parking Lot");
        l[5].setBounds(460,150,100,30);
        f[4].add(l[5]);
        cb[3]=new JComboBox();
        /*String pLot[]= {"Management","Staff","Car","Bike"};
        cb[3]=new JComboBox(pLot);*/
        cb[3].setBounds(450,200,100,30);
        f[4].add(cb[3]);

        bt[0]=new JButton("REGISTER");
        bt[0].setBounds(230,470,150,80);
        bt[0].setVisible(true);
        bt[0].setBackground(Color.GREEN);
        f[4].add(bt[0]);

        f[4].setVisible(true);
    }

    public static void main(String[]args) {
        new testGUI();
    }
}

Я уже пытался использовать f[4].repaint() для обновления sh кадра, но ничего не произошло.

...