java - JFrame 组件有时无法加载

标签 java swing event-handling jframe

正如标题所说,我有一个 JFrame,它会在单击按钮后更新。 该框架有一个菜单栏、一些图像和一个按钮。大多数时候一切正常,但有时框架显示时没有按钮和图像(10% 的时间)。该程序不显示错误。我完全不知道为什么没有加载组件,尤其是因为它不经常发生。我做错了什么?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class FirstScreen extends JFrame {
    private static final long serialVersionUID = 1L;
    int counter;
    private JButton highcharts, fusioncharts, googleAPI, visualizationAPI, easyCharts, jqplot, bfo, chartfx;
    private JButton btn;
    private JMenuBar menuBar;
    private JMenu menu;
    private JMenuItem exit;

    public FirstScreen(Model model, int round) {
        ImageIcon h = new ImageIcon("images/highcharts"+model.getElements()[model.getCounter()]+".jpg");
        ImageIcon f = new ImageIcon("images/fusioncharts"+model.getElements()[model.getCounter()]+".jpg");
        ImageIcon g = new ImageIcon("images/googleAPI"+model.getElements()[model.getCounter()]+".jpg");
        ImageIcon v = new ImageIcon("images/visualization"+model.getElements()[model.getCounter()]+".jpg");
        ImageIcon ec = new ImageIcon("images/easycharts"+model.getElements()[model.getCounter()]+".jpg");
        ImageIcon j = new ImageIcon("images/jqplot"+model.getElements()[model.getCounter()]+".jpg");
        ImageIcon b = new ImageIcon("images/bfo"+model.getElements()[model.getCounter()]+".jpg");
        ImageIcon c = new ImageIcon("images/chartfx"+model.getElements()[model.getCounter()]+".jpg");

        highcharts = new JButton(h);
        fusioncharts = new JButton(f);
        googleAPI = new JButton(g);
        visualizationAPI = new JButton(v);
        easyCharts = new JButton(ec);
        jqplot = new JButton(j);
        bfo = new JButton(b);
        chartfx = new JButton(c);

        Random rand=new Random();
        int min = 0;
        int max = 9;
        int randomNum = rand.nextInt(max - min + 1) + min;     

        JPanel panelContent = new JPanel(new GridLayout(0,1));
        switch(randomNum) {
        case 0:         
            panelContent.add(wrap(fusioncharts));
            panelContent.add(wrap(googleAPI));
            panelContent.add(wrap(visualizationAPI));
            panelContent.add(wrap(easyCharts));
            panelContent.add(wrap(jqplot));
            panelContent.add(wrap(bfo));
            panelContent.add(wrap(chartfx));
            panelContent.add(wrap(highcharts));
            break;
        case 1:

            panelContent.add(wrap(fusioncharts));
            panelContent.add(wrap(googleAPI));
            panelContent.add(visualizationAPI);
            panelContent.add(wrap(easyCharts));
            panelContent.add(wrap(jqplot));
            panelContent.add(wrap(bfo));
            panelContent.add(wrap(chartfx));panelContent.add(wrap(highcharts));
            break;
        case 2:

            panelContent.add(wrap(fusioncharts));
            panelContent.add(wrap(googleAPI));
            panelContent.add(visualizationAPI);
            panelContent.add(wrap(easyCharts));
            panelContent.add(wrap(jqplot));
            panelContent.add(wrap(bfo));
            panelContent.add(wrap(chartfx));panelContent.add(wrap(highcharts));
            break;
        case 3:

            panelContent.add(wrap(fusioncharts));
            panelContent.add(wrap(googleAPI));
            panelContent.add(visualizationAPI);
            panelContent.add(wrap(easyCharts));
            panelContent.add(wrap(jqplot));
            panelContent.add(wrap(bfo));
            panelContent.add(wrap(chartfx));panelContent.add(wrap(highcharts));
            break;
        case 4:

            panelContent.add(wrap(fusioncharts));
            panelContent.add(wrap(googleAPI));
            panelContent.add(visualizationAPI);
            panelContent.add(wrap(easyCharts));
            panelContent.add(wrap(jqplot));
            panelContent.add(wrap(bfo));
            panelContent.add(wrap(chartfx));panelContent.add(wrap(highcharts));
            break;
        case 5:

            panelContent.add(wrap(fusioncharts));
            panelContent.add(wrap(googleAPI));
            panelContent.add(visualizationAPI);
            panelContent.add(wrap(easyCharts));
            panelContent.add(wrap(jqplot));
            panelContent.add(wrap(bfo));
            panelContent.add(wrap(chartfx));panelContent.add(wrap(highcharts));
            break;
        case 6:

            panelContent.add(wrap(fusioncharts));
            panelContent.add(wrap(googleAPI));
            panelContent.add(visualizationAPI);
            panelContent.add(wrap(easyCharts));
            panelContent.add(wrap(jqplot));
            panelContent.add(wrap(bfo));
            panelContent.add(wrap(chartfx));panelContent.add(wrap(highcharts));
            break;
        case 7:

            panelContent.add(wrap(fusioncharts));
            panelContent.add(wrap(googleAPI));
            panelContent.add(visualizationAPI);
            panelContent.add(wrap(easyCharts));
            panelContent.add(wrap(jqplot));
            panelContent.add(wrap(bfo));
            panelContent.add(wrap(chartfx));panelContent.add(wrap(highcharts));
            break;
        case 8:

            panelContent.add(wrap(fusioncharts));
            panelContent.add(wrap(googleAPI));            
            panelContent.add(visualizationAPI);
            panelContent.add(wrap(easyCharts));
            panelContent.add(wrap(jqplot));            
            panelContent.add(wrap(bfo));
            panelContent.add(wrap(chartfx));panelContent.add(wrap(highcharts));
            break;
        }
        JScrollPane scrollpane = new JScrollPane(panelContent);
        setTitle("Onderzoeksprogramma grafische libraries Dashboard 2.0");
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        menuBar = new JMenuBar();
        menu = new JMenu("Options");
        //menu.setForeground(Color.WHITE);
        menuBar.add(menu);              
        exit = new JMenuItem("Exit");
        menu.add(exit);
        setJMenuBar(menuBar);
        menuBar.setBackground(Color.BLACK);
        menuBar.setForeground(Color.WHITE);


        Container contents = getContentPane();
        JPanel jp = new JPanel();

        btn = new JButton("Ga verder");
        //jp.add(btn);
        //jp.add(scrollpane);
        jp.setOpaque(false);

        contents.add(scrollpane, BorderLayout.CENTER);  
        contents.add(btn, BorderLayout.SOUTH);  

        setUndecorated(true);
        setExtendedState(FirstScreen.MAXIMIZED_BOTH);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    }    
    public JButton getHighcharts() {
        return highcharts;
    }
    public void setHighcharts(JButton highcharts) {
        this.highcharts = highcharts;
    }
    public JButton getFusioncharts() {
        return fusioncharts;
    }
    public void setFusioncharts(JButton fusioncharts) {
        this.fusioncharts = fusioncharts;
    }
    public JButton getGoogleAPI() {
        return googleAPI;
    }
    public void setGoogleAPI(JButton googleAPI) {
        this.googleAPI = googleAPI;
    }
    public JButton getVisualizationAPI() {
        return visualizationAPI;
    }
    public void setVisualizationAPI(JButton visualizationAPI) {
        this.visualizationAPI = visualizationAPI;
    }
    public JButton getEasyCharts() {
        return easyCharts;
    }
    public void setEasyCharts(JButton easyCharts) {
        this.easyCharts = easyCharts;
    }
    public JButton getJqplot() {
        return jqplot;
    }
    public void setJqplot(JButton jqplot) {
        this.jqplot = jqplot;
    }
    public JButton getBfo() {
        return bfo;
    }
    public void setBfo(JButton bfo) {
        this.bfo = bfo;
    }
    public JButton getChartfx() {
        return chartfx;
    }
    public void setChartfx(JButton chartfx) {
        this.chartfx = chartfx;
    }
    public JButton getBtn() {
        return btn;
    }

    public void setBtn(JButton btn) {
        this.btn = btn;
    }
    public JMenuItem getExit() {
        return exit;
    }

    public void setExit(JMenuItem exit) {
        this.exit = exit;
    }

    public int getCounter() {
        return counter;
    }
    public void increaseCounter() {
        counter++;      
    }
    public void disableAll() {
        highcharts.setEnabled(false);
        fusioncharts.setEnabled(false);
        jqplot.setEnabled(false);
        visualizationAPI.setEnabled(false);
        googleAPI.setEnabled(false);
        easyCharts.setEnabled(false);
        bfo.setEnabled(false);
        chartfx.setEnabled(false);
        btn.setEnabled(true);       
    }

    public static JComponent wrap(JComponent comp)
    {
        JPanel panel = new JPanel();
        panel.add(comp);
        return panel;
    }
}

Controller :

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.UnsupportedLookAndFeelException;

public class Controller implements ActionListener {
    private Model model;
    private StartScreen startScreen;
    private FirstScreen firstScreen;
    private FinalScreen finalScreen;
    private Score score;

    public Controller(Model model) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
        this.model = model;
        startScreen = new StartScreen();
        startScreen.getButton().addActionListener(this); 
        startScreen.getExit().addActionListener(this); 
        startScreen.setVisible(true);
        score = new Score();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(startScreen.getExit()) || (firstScreen != null && e.getSource().equals(firstScreen.getExit()))) {           
            if(firstScreen != null) {
                firstScreen.setVisible(false);
                firstScreen.dispose();
            }
            else {
                startScreen.setVisible(false);
                startScreen.dispose();
            }
        }       
        else if (finalScreen!=null && (e.getSource().equals(finalScreen.getBtn()))) {
            System.exit(0);
        }
        else if(e.getSource().equals(startScreen.getButton())) {
            startScreen.setVisible(false);
            startScreen.dispose();
            firstScreen = new FirstScreen(model, 0);
            firstScreen.setVisible(true);

            firstScreen.getBtn().addActionListener(this);
            firstScreen.getExit().addActionListener(this);
            firstScreen.getHighcharts().addActionListener(this);
            firstScreen.getFusioncharts().addActionListener(this);
            firstScreen.getGoogleAPI().addActionListener(this);
            firstScreen.getVisualizationAPI().addActionListener(this);
            firstScreen.getEasyCharts().addActionListener(this);
            firstScreen.getJqplot().addActionListener(this);
            firstScreen.getBfo().addActionListener(this);
            firstScreen.getChartfx().addActionListener(this);
            firstScreen.getBtn().setEnabled(false);
        }
        else if (e.getSource().equals(firstScreen.getBtn())) {  
            model.increaseCounter();
            if(model.getCounter() >= 9) {
                finalScreen = new FinalScreen(score);
                finalScreen.getBtn().addActionListener(this);
                firstScreen.getExit().addActionListener(this);
                firstScreen.setVisible(false);
                firstScreen.dispose();
            }
            else {
                int temp = model.getCounter();
                firstScreen.setVisible(false);
                firstScreen.dispose();
                firstScreen = null;
                firstScreen = new FirstScreen(model, temp);
                firstScreen.setVisible(true);
            }
            firstScreen.getBtn().addActionListener(this);
            firstScreen.getExit().addActionListener(this);
            firstScreen.getHighcharts().addActionListener(this);
            firstScreen.getFusioncharts().addActionListener(this);
            firstScreen.getGoogleAPI().addActionListener(this);
            firstScreen.getVisualizationAPI().addActionListener(this);
            firstScreen.getEasyCharts().addActionListener(this);
            firstScreen.getJqplot().addActionListener(this);
            firstScreen.getBfo().addActionListener(this);
            firstScreen.getChartfx().addActionListener(this);
            firstScreen.getBtn().setEnabled(false);

            if(model.getCounter() == 8) {
                firstScreen.getBtn().setText("Finish");
            }

        }   
        else if(e.getSource().equals(firstScreen.getHighcharts())) {
            if(firstScreen.getCounter() == 0) {
                score.setHighcharts(3);
            }
            else if(firstScreen.getCounter() == 1) {
                score.setHighcharts(2);
            }
            else {
                score.setHighcharts(1);
            }   
            firstScreen.getHighcharts().setEnabled(false);
            firstScreen.increaseCounter();
            if(firstScreen.getCounter()==3) {
                firstScreen.disableAll();
            }           
        }
        else if(e.getSource().equals(firstScreen.getFusioncharts())) {
            if(firstScreen.getCounter() == 0) {
                score.setFusioncharts(3);
            }
            else if(firstScreen.getCounter() == 1) {
                score.setFusioncharts(2);
            }
            else {
                score.setFusioncharts(1);
            }
            firstScreen.getFusioncharts().setEnabled(false);
            firstScreen.increaseCounter();
            if(firstScreen.getCounter()==3) {
                firstScreen.disableAll();
            }           
        }
        else if(e.getSource().equals(firstScreen.getGoogleAPI())) {
            if(firstScreen.getCounter() == 0) {
                score.setGoogleAPI(3);
            }
            else if(firstScreen.getCounter() == 1) {
                score.setGoogleAPI(2);
            }
            else {
                score.setGoogleAPI(1);
            }
            firstScreen.getGoogleAPI().setEnabled(false);
            firstScreen.increaseCounter();
            if(firstScreen.getCounter()==3) {
                firstScreen.disableAll();
            }           
        }
        else if(e.getSource().equals(firstScreen.getVisualizationAPI())) {
            if(firstScreen.getCounter() == 0) {
                score.setVisualization(3);
            }
            else if(firstScreen.getCounter() == 1) {
                score.setVisualization(2);
            }
            else {
                score.setVisualization(1);
            }
            firstScreen.getVisualizationAPI().setEnabled(false);
            firstScreen.increaseCounter();
            if(firstScreen.getCounter()==3) {
                firstScreen.disableAll();
            }           
        }
        else if(e.getSource().equals(firstScreen.getEasyCharts())) {
            if(firstScreen.getCounter() == 0) {
                score.setEasyCharts(3);
            }
            else if(firstScreen.getCounter() == 1) {
                score.setEasyCharts(2);
            }
            else {
                score.setEasyCharts(1);
            }
            firstScreen.getEasyCharts().setEnabled(false);
            firstScreen.increaseCounter();
            if(firstScreen.getCounter()==3) {
                firstScreen.disableAll();
            }           
        }
        else if(e.getSource().equals(firstScreen.getJqplot())) {
            if(firstScreen.getCounter() == 0) {
                score.setJqplot(3);
            }
            else if(firstScreen.getCounter() == 1) {
                score.setJqplot(2);
            }
            else {
                score.setJqplot(1);
            }
            firstScreen.getJqplot().setEnabled(false);
            firstScreen.increaseCounter();
            if(firstScreen.getCounter()==3) {
                firstScreen.disableAll();
            }           
        }
        else if(e.getSource().equals(firstScreen.getBfo())) {
            if(firstScreen.getCounter() == 0) {
                score.setBfo(3);
            }
            else if(firstScreen.getCounter() == 1) {
                score.setBfo(2);
            }
            else {
                score.setBfo(1);
            }
            firstScreen.getBfo().setEnabled(false);
            firstScreen.increaseCounter();
            if(firstScreen.getCounter()==3) {
                firstScreen.disableAll();
            }           
        }
        else if(e.getSource().equals(firstScreen.getChartfx())) {
            if(firstScreen.getCounter() == 0) {
                score.setChartfx(3);
            }
            else if(firstScreen.getCounter() == 1) {
                score.setChartfx(2);
            }
            else {
                score.setChartfx(1);
            }

            firstScreen.getChartfx().setEnabled(false);
            firstScreen.increaseCounter();
            if(firstScreen.getCounter()==3) {
                firstScreen.disableAll();
            }           
        }
    }   
}

最佳答案

确保在您的 main 方法中,您正在事件调度线程的上下文中加载主框架

public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            //.. Create UI
        }
    });
}

此外,我会在创建任何 UI 组件之前设置 LookAndFeel。它不太可能产生任何重大影响,但它并非真正设计用于中途切换

关于java - JFrame 组件有时无法加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13018010/

相关文章:

java - 一次发送多个 jMS 消息并保持顺序

java - HTML特殊字符解码

java - 睡在 SwingWorker 里?

java - 是否可以使用 java Swing DnD api 在 mousePressed 上开始拖动?

java - 在 Java 或 Groovy 中转换 UTC 时间 T0 本地时间

java - 测试时如何修复未登录错误?

java - 相对于面板java绘制椭圆

javascript - 如何在网站上实现键盘功能快捷键?

C# 事件空检查(来自另一个类)

python - 在 windows 7 中使用 python 监视键盘事件