在实施所有建议后,该按钮现在不会冻结程序,只是停止响应鼠标点击。按下之后,除了" Go Back"按钮,工作或。以前,甚至不是" Go Back"按钮工作。 这是完整和更新的代码:
package models;
// Various imports
@SuppressWarnings("unused")
public class AskTheAdmiralFrame extends JFrame {
/**
* Set the serial number and create components
*/
private static final long serialVersionUID = 4057461973369708227L;
private JPanel panel;
/**
* @wbp.nonvisual location=111,111
*/
private final JLabel textArea = new JLabel("I am Admiral Ting of the Chinese Navy.");
private final JLabel textArea2 = new JLabel("Welcome to the Battle of Yalu River interactive Historask tour.");
private final JLabel textArea3 = new JLabel("Please ask me any questions you have about the Battle of Yalu River.");
private final JLabel textArea4 = new JLabel();
private final JLabel textArea5 = new JLabel();
private final JButton button1 = new JButton("What is the Battle of Yalu River?");
private final JButton button2 = new JButton("Where was the Battle of Yalu River?(duh)");
private final JButton button3 = new JButton("When was the Battle of Yalu River?");
private final JButton button4 = new JButton("More details please.");
private final JButton button5 = new JButton("I think I know enought already. Ask me some questions");
private final JButton button6 = new JButton("More details please.");
private final JButton button7 = new JButton("More details please.");
private final JButton button8 = new JButton("More details please.");
private final JButton button9 = new JButton("What happened to the Chinese?");
private final JButton button10 = new JButton("What happened next?");
private final JButton button11 = new JButton("What happened to the Japanese?");
private final JButton button12 = new JButton("Tell me about more of the Chinese deaths.");
private final JButton button13 = new JButton("Tell me more about the admirals.");
private final JButton button14 = new JButton("Tell me more about the Chinese ships");
private final JButton button15 = new JButton("Tell me more about the Japanese ships");
private final JButton button16 = new JButton("Then why did the Chinese fleet lose even though they were so much more advanced?");
private final JButton button17 = new JButton("Go Back");
private final JTextField textField = new JTextField();
private final JTextField textField2 = new JTextField();
private final JTextField textField3 = new JTextField();
private final JTextField textField4 = new JTextField();
private final JTextField textField5 = new JTextField();
private final JLabel label = new JLabel("How many ships were destroyed in the battle?");
private final JLabel label2 = new JLabel("Which year did the battle take place in?");
private final JLabel label3 = new JLabel("Where was the battle?");
private final JLabel label4 = new JLabel("Who was the Japanese Admiral that visited the Beiyng Fleet?");
private final JLabel label5 = new JLabel("What time was the Japanese battle flag raised?");
private int score = 0;
private String scr = Integer.toString(score);
private String ans = textField.getText();
private String ans2 = textField2.getText();
private String ans3 = textField3.getText();
private String ans4 = textField4.getText();
private String ans5 = textField5.getText();
private final JLabel labela = new JLabel("Congratualtions! You have completed the quiz:) You have scored ");
private final JLabel labelb = new JLabel(scr);
private final JLabel labelc = new JLabel();
private final ImageIcon imgicn = new ImageIcon("resources/yalubg.jpg");
private final Image img = imgicn.getImage();
private final Image imgu = img.getScaledInstance(700, 500, 0);
JLabel bg = new JLabel(imgicn);
private final ImageIcon imgicno = new ImageIcon("resources/hicon.icns");
Image imgee = imgicno.getImage();
private final ImageIcon ad = new ImageIcon("resources/Di_Ruchang.jpg");
JLabel at = new JLabel(ad);
private final JLabel adText = new JLabel("Admiral Ting Juchang");
private final ImageIcon al = new ImageIcon("resources/al.jpg");
JLabel a = new JLabel(al);
private final JLabel alText = new JLabel("Admiral Liu Buchan");
private final ImageIcon is = new ImageIcon("resources/is.jpg");
JLabel ai = new JLabel(is);
private final JLabel isText = new JLabel("Admiral Sukeyuki Itoh");
private final ImageIcon tk = new ImageIcon("resources/tk.jpg");
JLabel bt = new JLabel(tk);
private final JLabel tkText = new JLabel("Admiral Tsuboi Togo");
public void addAll(){
if(panel.getComponents() != null){
panel.add(a);
panel.add(at);
panel.add(textArea);
panel.add(textArea2);
panel.add(textArea3);
panel.add(textArea4);
panel.add(textArea5);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button5);
}
}
/**
* Create the frame.
*/
public AskTheAdmiralFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setIconImage(imgee);
setTitle("Ask Admiral Ting");
setSize(1400, 1000);
panel = new JPanel();
setContentPane(panel);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
panel.add(textArea);
panel.add(textArea2);
panel.add(textArea3);
panel.add(textArea4);
panel.add(textArea5);
textArea.setSize(textArea.getPreferredSize());
/**
* Set buttons' responses
*/
button17.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent) {
dispose();
new HistoraskMenu();
}
});
panel.add(button17);
button1.setHorizontalAlignment(SwingConstants.LEFT);
button1.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent) {
textArea.setText("The Battle of Yalu River is a battle that took place");
textArea2.setText("between the Chinese Beiyang Fleet ");
textArea3.setText("and the Imperial Japanese Navy.");
textArea4.setText("Interesting?");
textArea5.setText("");
panel.remove(button1);
panel.add(button5);
panel.add(button4);
addAll();
}
});
getContentPane().add(button1);
button2.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent) {
textArea.setText("Actually, the Battle of Yalu River");
textArea2.setText("did not take place in Yalu River, ");
textArea3.setText("but in the Yellow Sea off the mouth of the Yalu River.");
textArea4.setText("");
textArea5.setText("");
panel.remove(button2);
panel.add(button5);
panel.add(button6);
addAll();
}
});
getContentPane().add(button2);
button3.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent) {
textArea.setText("The Battle took place on the 17 of September 1894.");
textArea2.setText("It was during the first Sino-Japanese War.");
textArea3.setText("Sino- is a prefix meaning Chinese or China.");
textArea4.setText("");
textArea5.setText("");
panel.remove(button3);
panel.add(button5);
panel.add(button7);
addAll();
}
});
getContentPane().add(button3);
button4.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent) {
textArea.setText("The Battle of Yalu River took place in the Yellow Sea, off the mouth of the Yalu River.");
textArea2.setText("The two fleets, the Imperial Japanese Navy and the Chinese Beiyang Fleet, met at 11.40 am in the morning.");
textArea3.setText("Each fleet began slowely advancing towards each other.");
textArea4.setText("It was a matter of nerves. Each fleet was out of firing range of the other");
textArea5.setText("Neither side wanted to fire too early and waste bullets, but neither did either want to shoot too late and be caught by the other fleet.");
panel.remove(button4);
panel.add(button10);
panel.add(button14);
panel.add(button15);
addAll();
}
});
button5.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent) {
panel.add(textField);
panel.add(label);
panel.remove(textArea);
panel.remove(textArea2);
panel.remove(textArea3);
panel.remove(textArea4);
panel.remove(textArea5);
panel.remove(button1);
panel.remove(button2);
panel.remove(button3);
panel.remove(button4);
panel.remove(button5);
panel.remove(button6);
panel.remove(button7);
panel.remove(button8);
panel.remove(button9);
panel.remove(button10);
panel.remove(button11);
panel.remove(button12);
panel.remove(button13);
panel.remove(button14);
panel.remove(button15);
panel.remove(button16);
if(ans.equals("5")){
panel.remove(label);
panel.remove(textField);
panel.add(label2);
panel.add(textField2);
score++;
} else {
panel.remove(label);
panel.remove(textField);
score--;
panel.add(label2);
panel.add(textField2);
}
if(ans2.equals("1894")){
panel.remove(label2);
panel.remove(textField2);
panel.add(label3);
panel.add(textField3);
score++;
} else {
panel.remove(label2);
panel.remove(textField2);
score --;
panel.add(label3);
panel.add(textField3);
}
if(ans3.equals("yellow sea")||
ans3 .equals("korea bay")||
ans3.equals("The Yellow Sea")||
ans3.equals("Korea Bay")){
panel.remove(label3);
panel.remove(textField3);
panel.add(label4);
panel.add(textField4);
score++;
} else {
panel.remove(label3);
panel.remove(textField3);
score--;
panel.add(label4);
panel.add(textField4);
}
if(ans4.equals("Admiral Togo")
||ans4.equals("admiral togo")) {
panel.remove(label4);
panel.remove(textField4);
panel.add(label5);
panel.add(textField5);
score++;
} else {
panel.remove(label4);
panel.remove(textField4);
score--;
panel.add(label5);
panel.add(textField5);
}
if(ans5.equals("12.03")
||ans5.equals("12.03pm")
||ans5.equals("12.03.p.m.")
||ans5.equals("12,03.pm")) {
panel.remove(label5);
panel.remove(textField5);
panel.add(labela);
panel.add(labelb);
switch(score){
case -5:
labelc.setText("points! You don't know much on the Battle of Yalu do you? "
+ "Try learning a bit more before trying the quiz again.:(");
break;
case -4:
labelc.setText("points! You don't know much on the Battle of Yalu do you? "
+ "Try learning a bit more before trying the quiz again.:(");
break;
case -3:
labelc.setText("points! You don't know much on the Battle of Yalu do you? "
+ "Try learning a bit more before trying the quiz again.:(");
break;
case -2:
labelc.setText("points! You don't know much on the Battle of Yalu do you? "
+ "Try learning a bit more before trying the quiz again.:(");
break;
case -1:
labelc.setText("points! You don't know much on the Battle of Yalu do you? "
+ "Try learning a bit more before trying the quiz again.:(");
break;
case 1:
labelc.setText("points! Your knowledge on the Battle of Yalu needs improvement.:(");
break;
case 2:
labelc.setText("points! Your knowledge on the Battle of Yalu is ok, "
+ "but you should try asking Admiral Ting a bit more");
break;
case 3:
labelc.setText("points! Your knowledge on the Battle of Yalu is satisfactory. Good job:)");
break;
case 4:
labelc.setText("points! Your knowledge on the Battle of Yalu is amazing!.:(");
break;
case 5:
labelc.setText("points! You have a perfect score!!!!:):):) Are you sure you didin't cheat?");
break;
}
panel.add(labelc);
}
}
});
panel.add(button5);
button6.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent){
textArea.setText("The Battle took place in the Yellow Sea.");
textArea2.setText("To be more specific, it took place in Korea Bay, ");
textArea3.setText("which is in the northern part of the Yellow Sea");
textArea4.setText("The Chinese were delivering reinforecements to Pyongyang ");
textArea5.setText("and the attack was actually unprovoked.");
addAll();
panel.remove(button6);
}
});
button7.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent){
textArea.setText("The Battle of Yalu River was the largest naval battle ");
textArea2.setText("of the first Sino-Japanese War. ");
textArea3.setText("The Chinese and Japanese fleets spotted each other at 11.40am");
textArea4.setText("The Japanese raised their battleflag at 12.03pm to signal preparation for battle.");
textArea5.setText("And the Chinese attacked them at 12.45pm, although they were still 6km away.");
addAll();
panel.remove(button7);
}
});
button8.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent){
textArea.setText("Also, the Chinese soldiers hadn't been trained enough in gunnery.");
textArea2.setText("It was also believed that there was a corrput saboteur within their ranks.");
textArea3.setText("Because many of the shells had ");
textArea4.setText("been filled with cement or porcelain.");
textArea5.setText("Some were also of the wrong caliber and thus could not be fired.");
addAll();
panel.remove(button8);
}
});
button9.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent){
textArea.setText("The Chinese lost the brutal navy battle. ");
textArea2.setText("Teir troops suffered many losses. In total, they loss");
textArea3.setText("700 men to death and 300 to injury.");
textArea4.setText("Five of their twelve ships were sunk.");
textArea5.setText("All of this happened inthe duration of 5 hours.");
panel.remove(button9);
panel.add(button11);
panel.add(button12);
addAll();
}
});
button10.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent){
textArea.setText("The Chinese Fleet fired first, ");
textArea2.setText("although the Japanese ships were still 6km away. ");
textArea3.setText("The Chinese fired too early");
textArea4.setText("and the Japanese saw the fire and smoke from the Chinese shells");
textArea5.setText("that fell into the water in front of them.");
panel.remove(button10);
panel.add(button11);
addAll();
}
});
button11.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent){
textArea.setText("Surprisingly, the Japanese won the short and gruesome battle.");
textArea2.setText("Not a single of their ships were sunken and only ninety");
textArea3.setText("of the Japanese troops were killed. Only 200 were wounded. ");
textArea4.setText("Not a single of their ships sank. However, the crew of Matsushima were ");
textArea5.setText("all burnt as a Chinese shell hit an ammuniton pile, causing it to burst with great heat.");
panel.remove(button11);
panel.add(button12);
panel.add(button13);
addAll();
}
});
button12.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent){
textArea.setText("The ship Lai-Yuen burnt with such heat that it glowed nearly ");
textArea2.setText("white-hot and many of the crew burned to death. ");
textArea3.setText("When the ship King-Yuen was hit, it started rolling from side to side until it fell over and didn't roll back. ");
textArea4.setText("One of the two battleships, Ting, was hit 300 times by the Japanese! It's size must have it an easy target. ");
textArea5.setText("The captain of Tsi-Yuen was decapitated for fleeing the battle and ramming another Chinese ship in the process");
panel.remove(button12);
panel.add(button13);
addAll();
}
});
button13.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent){
textArea.setText("The Chinese admirals were Admiral Ting and Admiral Liu.");
textArea2.setText("The Japanese Admirals were Admiral Togo and Admiral Itoh");
textArea3.setText("Interestingly, both Chinese admirals commited suicide by opium to uphold ");
textArea4.setText("their honour, during the Sino-Japanese War. This refusal to give in earned ");
textArea5.setText("them much respect from many Japanese and Chinese individuals.");
panel.remove(button13);
addAll();
}
});
button14.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent){
textArea.setText("The Chinese fleet had 12 ships. Their ships were generally larger than the Japanese ones, ");
textArea2.setText("and they had two huge battleships, 定远 and 鎮遠, bigger than anything the Japanese fleet had.");
textArea3.setText("The other ten ships were a comibnation of cruisers, torpedo boats and corvet.");
textArea4.setText("They were also equipped with ten to eight inch gun mounts. On top of that, ");
textArea5.setText("many skilled military advisers, like Major Constantin von Hanneken, W. F. Tyler and Philo McGiffen");
panel.add(button16);
panel.remove(button14);
addAll();
}
});
button15.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent){
textArea.setText("The Japanese fleet had 12 ships.");
textArea2.setText("Like the Chinese ships, they were relatively new, ");
textArea3.setText("the product of the country's modernisation over the past 30 years or so.");
textArea4.setText("Although smaller than the Chinese ships, they were faster ");
textArea5.setText("and their sailors were more disciplined and better trained.");
button15.remove(button15);
addAll();
}
});
button16.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent paramMouseEvent){
textArea.setText("Well, the Japanese ships were much faster and could manouveur around and dodge ");
textArea2.setText("the Chinese's shells and torpedoes and things.");
textArea3.setText("Admiral Togo of the Imperial Japanese Japanese Navy had previously paid a courtesy ");
textArea4.setText("call to the Chinese fleet and discovered that the guns were");
textArea5.setText("dirty and some also has washing hung over them.");
panel.remove(button16);
panel.add(button8);
addAll();
}
});
at.setText("Admiral Ting Juchang");
at.setForeground(Color.YELLOW);
at.setHorizontalTextPosition(JLabel.CENTER);
at.setVerticalTextPosition(JLabel.CENTER);
panel.add(at, FlowLayout.CENTER);
a.setText("Admiral Liu Buchan");
a.setForeground(Color.YELLOW);
a.setHorizontalTextPosition(JLabel.CENTER);
a.setVerticalTextPosition(JLabel.CENTER);
panel.add(a, FlowLayout.RIGHT);
ai.setText("Admiral Sukeyuki Itoh");
ai.setForeground(Color.YELLOW);
ai.setHorizontalTextPosition(JLabel.CENTER);
ai.setVerticalTextPosition(JLabel.CENTER);
panel.add(ai, FlowLayout.LEADING);
bt.setText("Admiral Tsuboi Togo");
bt.setForeground(Color.YELLOW);
bt.setHorizontalTextPosition(JLabel.CENTER);
bt.setVerticalTextPosition(JLabel.CENTER);
panel.add(bt, FlowLayout.LEFT);
panel.add(bg, FlowLayout.TRAILING);
setVisible(true);
}
}
答案 0 :(得分:0)
首先,可能导致问题的是您使用==来比较字符串。通常,您应该只使用==来比较原始值。否则,最好使用.equals方法。
如果你可以通过" jammed"来澄清你的意思,这将有助于调试这个问题。添加一些打印语句/使用调试器精确计算出程序停止的位置。我假设这是一个来自某种GUI琐事游戏的一类?据我所知,if语句不应该引起任何问题。我的猜测是你的程序中有其他东西导致了这个问题。
另外,我注意到的另一件事是你的得分和 - 在你的if(ans2 ==" 1984)案例中有一个空格。
如果您可以发布更多信息,我绝对可以帮助您。