JOptionPane 表现怪异
JOptionPane is acting weird
我正在尝试使 Jbutton 正常工作。我的程序要求用户单击 "hint" 按钮。当用户单击时,将弹出一个 Joption 窗格,其中包含一个提示。下次他点击时,下一个提示出现,最后一个提示从堆栈中删除,依此类推。我添加了一堆字符串,其中存储了提示,然后将其添加到 actionListener。一切都很好,但不是从堆栈中删除元素,每次用户按下 JOption 窗格中的 "ok" 按钮时,我的堆栈的大小都会增加。可能是什么问题?
谢谢。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*-------------------------------------------------------
* Class GUI creates an user friendly interface for the puzzle game.
* It contains buttons, and clues that aids and interects with the player
*
--------------------------------------------------------*/
public class Gui extends JFrame{
private JButton hint;
private JPanel window1;
private JLabel label;
private Puzzle gamePanel;
private Display display;
private Hints hints;
private Stack theStack;
public Gui() throws FileNotFoundException, IOException{
window1 = new JPanel(); // panel for buttons
hint = new JButton("HINT");
window1.add(hint); //adds button to panel
hint.addActionListener(new Hint());
add(window1,BorderLayout.SOUTH);//buttons attach to frame.
private class Hint implements ActionListener{
public void actionPerformed(ActionEvent event){
try {
hints.getList(); // adds to the stack. Stack content comes from file
// a different class but called here.
Stack s = hints.getStack(); // return the stack
if(hint.isSelected()){ //if user hits the button
display.hint(line); //ignore this method.
}
else{
JOptionPane.showMessageDialog(window1,s.pop()); // pop from the stack
System.out.print(s.size()); // I added this just to show how my stack size is growing
}
if(s.isEmpty()){
JOptionPane.showMessageDialog(window1,"You are out of hints");
}
}catch (IOException ex) {
Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
public class Hints{
private Stack stack;
private File f;
private String line;
private Scanner scanner;
public Hints(){
f= new File("hints.txt");
stack = new Stack();
}
public Stack getList() throws FileNotFoundException, IOException{
scanner = new Scanner(f);
while(scanner.hasNextLine()){
line = scanner.nextLine();
stack.add(line);
}
scanner.close();
return stack;
}
public Stack getStack(){
return stack;
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFrame;
public class Puzzle_Tester {
public static void main(String[] args) throws FileNotFoundException, IOException{
Gui g = new Gui();
Hints h = new Hints();
g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g.pack();
// g.setSize(1900,1900);
g.setVisible(true);
Display display=new Display();
}
}
如果您的代码中有此内容:hints.getList();
,更具体地说是在您执行的操作中,那么您每次都是从文件中加载。将这一行 hints.getList();
放在 Hint
class 的构造函数中。我不确定框架是否每次都会创建一个新的 Hint
class,所以如果它不能解决问题,您可能需要使用静态构造函数来加载堆栈。
我正在尝试使 Jbutton 正常工作。我的程序要求用户单击 "hint" 按钮。当用户单击时,将弹出一个 Joption 窗格,其中包含一个提示。下次他点击时,下一个提示出现,最后一个提示从堆栈中删除,依此类推。我添加了一堆字符串,其中存储了提示,然后将其添加到 actionListener。一切都很好,但不是从堆栈中删除元素,每次用户按下 JOption 窗格中的 "ok" 按钮时,我的堆栈的大小都会增加。可能是什么问题? 谢谢。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*-------------------------------------------------------
* Class GUI creates an user friendly interface for the puzzle game.
* It contains buttons, and clues that aids and interects with the player
*
--------------------------------------------------------*/
public class Gui extends JFrame{
private JButton hint;
private JPanel window1;
private JLabel label;
private Puzzle gamePanel;
private Display display;
private Hints hints;
private Stack theStack;
public Gui() throws FileNotFoundException, IOException{
window1 = new JPanel(); // panel for buttons
hint = new JButton("HINT");
window1.add(hint); //adds button to panel
hint.addActionListener(new Hint());
add(window1,BorderLayout.SOUTH);//buttons attach to frame.
private class Hint implements ActionListener{
public void actionPerformed(ActionEvent event){
try {
hints.getList(); // adds to the stack. Stack content comes from file
// a different class but called here.
Stack s = hints.getStack(); // return the stack
if(hint.isSelected()){ //if user hits the button
display.hint(line); //ignore this method.
}
else{
JOptionPane.showMessageDialog(window1,s.pop()); // pop from the stack
System.out.print(s.size()); // I added this just to show how my stack size is growing
}
if(s.isEmpty()){
JOptionPane.showMessageDialog(window1,"You are out of hints");
}
}catch (IOException ex) {
Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
public class Hints{
private Stack stack;
private File f;
private String line;
private Scanner scanner;
public Hints(){
f= new File("hints.txt");
stack = new Stack();
}
public Stack getList() throws FileNotFoundException, IOException{
scanner = new Scanner(f);
while(scanner.hasNextLine()){
line = scanner.nextLine();
stack.add(line);
}
scanner.close();
return stack;
}
public Stack getStack(){
return stack;
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFrame;
public class Puzzle_Tester {
public static void main(String[] args) throws FileNotFoundException, IOException{
Gui g = new Gui();
Hints h = new Hints();
g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g.pack();
// g.setSize(1900,1900);
g.setVisible(true);
Display display=new Display();
}
}
如果您的代码中有此内容:hints.getList();
,更具体地说是在您执行的操作中,那么您每次都是从文件中加载。将这一行 hints.getList();
放在 Hint
class 的构造函数中。我不确定框架是否每次都会创建一个新的 Hint
class,所以如果它不能解决问题,您可能需要使用静态构造函数来加载堆栈。