显示没有发生应有的变化,但是代码看起来还不错吗?

时间:2019-05-13 14:20:28

标签: java swing user-interface

我正在创建一个可视化排序的程序,我的代码似乎还不错。但是,当我运行它时,它似乎并没有更新GUI。

我已经尝试过验证和重新验证,它的确改变了GUI,但是它大部分只是破坏了它。

public void createWindow(Container win) {
        bars = new JPanel();
        bars.setLayout(new GridBagLayout());
        c = new GridBagConstraints();

        startup = true;

        updateBars();

        startup = false;

        bars.setBackground(Color.GRAY);
        bars.setBorder(BorderFactory.createMatteBorder(4, 4, 4, 2, Color.BLACK));

        win.setLayout(new GridBagLayout());
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 2;
        c.gridwidth = 3;
        Dimension d2 = new Dimension(325, 250);
        bars.setPreferredSize(d2);
        bars.setMaximumSize(d2);
        bars.setSize(d2);
        win.add(bars, c);



        JPanel sorts = new JPanel();
        sorts.setBackground(Color.GRAY);
        sorts.setBorder(BorderFactory.createMatteBorder(4, 2, 2, 4, Color.BLACK));

        JLabel sortPrompt = new JLabel("Choose a sort: ");
        sortPrompt.setFont(sortPrompt.getFont().deriveFont(14.0f));
        sortPrompt.setHorizontalAlignment(SwingConstants.LEFT);
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        sorts.add(sortPrompt, c);

        String sortNames[] = {"Bogo Sort", "Gnome Sort", "Insertion Sort", "Slow sort"};
        JComboBox sortAlgs = new JComboBox(sortNames);
        sortAlgs.setFont(sortAlgs.getFont().deriveFont(14.0f));
        sortAlgs.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sortChosen = (String)(sortAlgs.getSelectedItem());
                System.out.println(sortChosen);
            }
        });
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        sorts.add(sortAlgs, c);



        JLabel delayPrompt = new JLabel("Choose a delay time: ");
        delayPrompt.setFont(delayPrompt.getFont().deriveFont(13.0f));
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 3;
        sorts.add(delayPrompt, c);

        String delays[] = {"1", "2", "3", "4", "5", "10", "25", "50", "100"};
        JComboBox delayTimes = new JComboBox(delays);
        delayTimes.setFont(delayTimes.getFont().deriveFont(13.0f));
        delayTimes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                delayTime = Integer.valueOf((String)(delayTimes.getSelectedItem()));
            }
        });
        c.gridx = 1;
        c.gridy = 2;
        sorts.add(delayTimes, c);



        JButton start = new JButton("Start");
        start.setFont(start.getFont().deriveFont(16.0f));
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sortCompleted = 0;
                while (sortCompleted != 1) {
                    startBogoSort();
                }

            }
        });
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridheight = 1;
        c.gridy = 3;
        sorts.add(start, c);

        JButton stop = new JButton("Stop");
        stop.setFont(stop.getFont().deriveFont(16.0f));
        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sortCompleted = 1;
            }
        });
        c.gridwidth = 1;
        c.gridx = 1;
        c.gridheight = 1;
        c.gridy = 3;
        sorts.add(stop, c);

        c.gridx = 3;
        c.gridy = 0;
        c.gridheight = 2;
        c.gridwidth = 2;
        Dimension d3 = new Dimension(250, 250);
        sorts.setPreferredSize(d3);
        win.add(sorts, c);

    }

    public void updateBars() {
        n = 10;
        arr = new ArrayList<Integer>(n);
        for (int i = 0; i < n; i++) {
            arr.add(i + 1);
        }
        if (startup == true) {
            Collections.shuffle(arr);
        }


        int maxHeight = 0;
        for (int i = 0; i < arr.size(); i++) {
            int height = arr.get(i);
            if (height > maxHeight) {
                maxHeight = height;
            }
            int yPos = n - height * 20;
            Rectangle r = new Rectangle();
            r.setBounds(i * 20, (yPos * 2) + ((height / 2) * 20), 20, height * 20);
            if (i == 0) {
                if (arr.get(i) > arr.get(i + 1)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
                } else if (arr.get(i) < arr.get(i + 1)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 0, Color.BLACK));
                }
            } else if (i == arr.size() - 1) {
                if (arr.get(i - 1) > arr.get(i)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1, Color.BLACK));
                } else if (arr.get(i - 1) < arr.get(i)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
                }
            } else {
                if (arr.get(i - 1) > arr.get(i) && arr.get(i) > arr.get(i + 1)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1, Color.BLACK));
                } else if (arr.get(i - 1) < arr.get(i) && arr.get(i) > arr.get(i + 1)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
                } else if (arr.get(i - 1) > arr.get(i) && arr.get(i) < arr.get(i + 1)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.BLACK));
                } else if (arr.get(i - 1) < arr.get(i) && arr.get(i) < arr.get(i + 1)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 0, Color.BLACK));
                }
            }

            c.gridheight = height;
            c.ipadx = 20;
            c.ipady = height * 20;
            c.anchor = GridBagConstraints.SOUTH;
            c.gridy = n - height;
            bars.add(r, c);
        }
    }

    public void startBogoSort() {
        while (sortCompleted != 1) {
            int totalSize = arr.size() - 1;
            for (int i = 1; i < totalSize; i++) {
                if (arr.get(i - 1) > arr.get(i)) {
                    sortCompleted = 0;
                    i = totalSize + 1;
                } else {
                    sortCompleted = 1;
                }
            }

            if (sortCompleted == 0) {
                bars.removeAll();

                Collections.shuffle(arr);
            }
        }
    }

抱歉,有些混乱,但是应该显示一个面板,其中包含需要排序的条形图,并且应该显示它随着时间的推移对条形图进行排序(延迟尚未实现,但我至少应该看到最后一个结果)。但是,实际上发生的是它当前冻结了。就像我之前说过的那样,我尝试了validate()和revalidate()方法,但它们似乎只是在进一步破坏代码。

1 个答案:

答案 0 :(得分:0)

我发现了这种情况下的错误。我需要在我的updateBars()方法中创建GridBagConstraints和GridBagLayout的新实例。另外,我使用TimerTask和Timer来更新GUI。原来,这是比我最初想的要简单得多的解决方案。

这是我更新的代码:

public void createWindow(Container win) {
        bars = new JPanel();
        c = new GridBagConstraints();

        startup = true;

        updateBars();

        startup = false;

        bars.setBackground(Color.GRAY);
        bars.setBorder(BorderFactory.createMatteBorder(4, 4, 4, 2, Color.BLACK));

        win.setLayout(new GridBagLayout());
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 2;
        c.gridwidth = 3;
        bars.setBounds(0, 0, 325, 200);
        c.fill = c.VERTICAL;
        win.add(bars, c);





        JPanel sorts = new JPanel();
        sorts.setLayout(new GridBagLayout());
        sorts.setBackground(Color.GRAY);
        sorts.setBorder(BorderFactory.createMatteBorder(4, 2, 4, 4, Color.BLACK));

        JLabel sortPrompt = new JLabel("Choose a sort: ");
        sortPrompt.setFont(sortPrompt.getFont().deriveFont(15.0f));
        c.ipady = 20;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 1;
        sorts.add(sortPrompt, c);

        String sortNames[] = {"Bogo Sort", "Gnome Sort", "Insertion Sort", "Bubble Sort"};
        JComboBox sortAlgs = new JComboBox(sortNames);
        sortAlgs.setFont(sortAlgs.getFont().deriveFont(15.0f));
        sortAlgs.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sortChosen = (String)(sortAlgs.getSelectedItem());
                System.out.println(sortChosen);
            }
        });
        c.anchor = GridBagConstraints.WEST;
        c.ipady = 20;
        c.gridx = 2;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        sorts.add(sortAlgs, c);



        JLabel delayPrompt = new JLabel("Choose a delay time: ");
        delayPrompt.setFont(delayPrompt.getFont().deriveFont(15.0f));
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 2;
        c.anchor = GridBagConstraints.CENTER;
        sorts.add(delayPrompt, c);

        String delays[] = {"1", "2", "3", "4", "5", "10", "25", "50", "100", "500", "1000"};
        JComboBox delayTimes = new JComboBox(delays);
        delayTimes.setFont(delayTimes.getFont().deriveFont(15.0f));
        delayTimes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                delayTime = Integer.valueOf((String)(delayTimes.getSelectedItem()));
                System.out.println(delayTime);
            }
        });
        c.anchor = GridBagConstraints.WEST;
        c.gridx = 2;
        c.gridy = 1;
        c.gridwidth = 1;
        sorts.add(delayTimes, c);



        JButton start = new JButton("Start");
        start.setFont(start.getFont().deriveFont(15.0f));
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sortCompleted = 0;
                if (sortChosen.equals(null)) {
                    sortChosen= "Bogo Sort";
                }

                if (sortChosen.equals("Bogo Sort")) {
                    startBogoSort();
                } else if (sortChosen.equals("Insertion Sort")) { 
                    startInsertionSort();
                } else if (sortChosen.equals("Bubble Sort")) {
                    startBubbleSort();
                } else if (sortChosen.equals("Gnome Sort")) {
                    System.out.println("This sort is not implemented yet");
                }

            }
        });
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridheight = 1;
        c.gridy = 3;
        c.anchor = GridBagConstraints.CENTER;
        sorts.add(start, c);

        JButton shuffle = new JButton("Shuffle");
        shuffle.setFont(shuffle.getFont().deriveFont(15.0f));
        shuffle.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                TimerTask shuffleTask = new TimerTask () {
                    public void run() {
                        bars.removeAll();

                        Collections.shuffle(arr);

                        updateBars();
                        bars.revalidate();
                    }
                };

                Timer shuffler = new Timer();
                shuffler.schedule(shuffleTask, 10);

            }
        });
        c.gridwidth = 1;
        c.gridx = 1;
        c.gridheight = 1;
        c.gridy = 3;
        sorts.add(shuffle, c);

        JButton stop = new JButton("Stop");
        stop.setFont(stop.getFont().deriveFont(15.0f));
        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (sortChosen.equals("Bogo Sort")) {
                    bogo.cancel();
                    bogo.purge();
                } else if (sortChosen.equals("Bubble Sort")) {
                    bubble.cancel();
                    bubble.purge();
                }
            }
        });
        c.gridwidth = 1;
        c.gridx = 2;
        c.gridheight = 1;
        c.gridy = 3;
        sorts.add(stop, c);

        blank = new JLabel("      ");
        c.gridx = 1;
        c.gridy = 5;
        c.gridwidth = 2;
        sorts.add(blank, c);

        c.gridx = 3;
        c.gridy = 0;
        c.gridheight = 2;
        c.gridwidth = 2;
        Dimension d3 = new Dimension(360, bars.getHeight());
        sorts.setPreferredSize(d3);
        win.add(sorts, c);

    }

    public void updateBars() {
        bars.setLayout(new GridBagLayout());
        c = new GridBagConstraints();

        if (startup == true) {
            n = 10;
            arr = new ArrayList<Integer>(n);
            for (int i = 0; i < n; i++) {
                arr.add(i + 1);
            }
        }



        int maxHeight = 0;
        for (int i = 0; i < arr.size(); i++) {
            int height = arr.get(i);
            if (height > maxHeight) {
                maxHeight = height;
            }
            int yPos = n - height * 20;
            Rectangle r = new Rectangle();
            r.setBounds(i * 20, (yPos * 2) + ((height / 2) * 20), 20, height * 20);
            if (i == 0) {
                if (arr.get(i) > arr.get(i + 1)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
                } else if (arr.get(i) < arr.get(i + 1)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 0, Color.BLACK));
                }
            } else if (i == arr.size() - 1) {
                if (arr.get(i - 1) > arr.get(i)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1, Color.BLACK));
                } else if (arr.get(i - 1) < arr.get(i)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
                }
            } else {
                if (arr.get(i - 1) > arr.get(i) && arr.get(i) > arr.get(i + 1)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1, Color.BLACK));
                } else if (arr.get(i - 1) < arr.get(i) && arr.get(i) > arr.get(i + 1)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
                } else if (arr.get(i - 1) > arr.get(i) && arr.get(i) < arr.get(i + 1)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.BLACK));
                } else if (arr.get(i - 1) < arr.get(i) && arr.get(i) < arr.get(i + 1)) {
                    r.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 0, Color.BLACK));
                }
            }

            c.gridheight = height;
            c.ipadx = 20;
            c.ipady = height * 20;
            c.anchor = GridBagConstraints.SOUTH;
            c.gridy = n - height;
            bars.add(r, c);
        }
    }

public void startBogoSort() {
        if (delayTime == 0) {
            delayTime = 1;
        }

        TimerTask bogoTask = new TimerTask() {
            public void run() {
                blank.setText("      ");

                for (int i = 1; i < arr.size(); i++) {
                    if (arr.get(i - 1) > arr.get(i)) {
                        sortCompleted = 0;
                        i = arr.size() + 1;
                    } else {
                        sortCompleted = 1;
                    }
                }

                if (sortCompleted == 0) {
                    bars.removeAll();

                    Collections.shuffle(arr);

                    updateBars();
                    bars.revalidate();
                } else {
                    blank.setText("Sort Completed");
                    bogo.cancel();
                    bogo.purge();
                }
            }
        };


        bogo.scheduleAtFixedRate(bogoTask, 0, delayTime);
    }