正如标题所说,我有一个在单击按钮后更新的 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;
}
}
控制器:
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();
}
}
}
}