我有一个运动程序,应该对时间和名称进行排序,然后在GUI上显示它们。但是,GUI在我实际调用它之前加载,因此我更新的名称和时间都没有传输到GUI。有没有办法阻止GUI早期加载?
以下是我用它来称呼它。
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
DisplayGUI.runGUI();
}
});
这是GUI的实际代码。
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class DisplayGUI implements ActionListener{
JFrame frame;
JPanel contentPane;
JComboBox sizeType;
JLabel sizeListPrompt, convertNew;
String alpWinner;
public DisplayGUI(){
/// Create and setup the frame
frame = new JFrame("2002 Winter Olympics");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a content pane with a BoxLayout and empty borders
contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Create a combo box and descriptive label
sizeListPrompt = new JLabel("Select an event");
sizeListPrompt.setAlignmentX(JLabel.LEFT_ALIGNMENT);
contentPane.add(sizeListPrompt);
String[] names = {"Alpine Skiing", "Bobsleigh", "Curling","Figure Skating",
"Luge", "Skeleton", "Ski Jumping", "Snowboarding", "Speed Skating", "Super G"};
sizeType = new JComboBox(names);
sizeType.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
sizeType.setSelectedIndex(0);
sizeType.addActionListener(this);
contentPane.add(sizeType);
// create and add a label that will display the Latin names
convertNew = new JLabel("Gold: \n Silver: \n Bronze: ");
convertNew.setBorder(BorderFactory.createEmptyBorder(20,10, 10, 10));
contentPane.add(convertNew);
//Add a content pane to the frame
frame.setContentPane(contentPane);
// size and display the frame
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
JComboBox comboBox = (JComboBox)event.getSource();
String convertType = (String)comboBox.getSelectedItem();
AlpineSkiing apski = new AlpineSkiing();
Bobsleigh bob = new Bobsleigh();
Curling curl = new Curling();
FigureSkating fig = new FigureSkating();
SuperG g = new SuperG();
Luge l = new Luge();
Skeleton bones = new Skeleton();
SkiJumping jump = new SkiJumping();
Snowboarding board = new Snowboarding();
SpeedSkating speed = new SpeedSkating();
// alpWinner = apski.getWinners();
String alpwin;
String bobwin, curlwin, figwin, lugewin, boneswin, jumpwin, boardwin, speedwin, gwin;
//apski.theWinners = null;
if (convertType == "Alpine Skiing"){
//AlpineSkiing.createArray(apski.totalComp, apski.change, apski.counter);
convertNew.setText("The winners are:" +alpWinner);
} else if (convertType == "Bobsleigh"){
convertNew.setText(bob.getWinners());
} else if (convertType == "Curling"){
convertNew.setText(curl.getWinners());
} else if (convertType == "Figure Skating"){
convertNew.setText(fig.getWinners());
} else if (convertType == "Luge"){
convertNew.setText(l.getWinners());
} else if (convertType == "Skeleton"){
convertNew.setText(bones.getWinners());
} else if (convertType == "Ski Jumping"){
convertNew.setText(jump.getWinners());
} else if (convertType == "Snowboarding"){
convertNew.setText(board.getWinners());
} else if (convertType == "Speed Skating"){
convertNew.setText(speed.getWinners());
} else if (convertType == "Super G"){
convertNew.setText(g.getWinners());
}
}
public static void runGUI(){
JFrame.setDefaultLookAndFeelDecorated(true);
DisplayGUI myProgram = new DisplayGUI();
}
}
答案 0 :(得分:0)
我认为你的问题是在比较convertType
时。您想要比较内容时比较参考。要比较内容,请在每个convertType.equals("Your string")
语句中使用else if
,这样可以解决您的问题。例如。 if(convertType.equals("Alpine Skiing"){ ...}