JOptionPane表现得很奇怪

时间:2015-02-16 09:16:57

标签: java jbutton joptionpane

我正在尝试使Jbutton正常工作。我的程序要求用户点击"提示"按钮。当用户单击时,将弹出一个Joption窗格,其中包含一个提示。下次他点击时,会出现下一个提示,并从堆栈中删除最后一个提示,依此类推。我添加了一堆字符串,其中存储了提示,然后添加到actionListener。这一切都运行得有点好,但不是从堆栈中删除元素,每次用户按下" ok" “JOption”窗格中的按钮。可能是什么问题? 谢谢。

    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();



          }
      }

1 个答案:

答案 0 :(得分:0)

如果您的代码中包含此内容:hints.getList();,更具体地说,在执行的操作中,则每次都从文件中加载。将此行hints.getList();放在Hint类的构造函数中。我不确定框架是否每次都会创建一个新的Hint类,所以如果它不能解决问题,你可能想要使用一个静态构造函数来加载你的堆栈。