首页 > 解决方案 > 为什么按箭头键时 JLabel 不更改文本?

问题描述

我的代码基本上是创建一个类似视觉小说的程序,当您单击“开始对话”并按左右箭头键时,该程序会根据列表更改行。但是,即使在按下“开始对话”之后,对话框中的 JLabel 也不会 setText()。有什么帮助吗?

这是我的代码。希望它可能会有所帮助。

package FourthQuarter;

import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionEvent;

public class FormativeAssessment1 implements KeyListener, ActionListener{
    JLabel DialogueBox;
    JButton StartDialogueButton;

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FormativeAssessment1 window = new FormativeAssessment1();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public FormativeAssessment1() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        
        
        frame = new JFrame();
        
        frame.setBounds(100, 100, 881, 490);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        JPanel panel = new JPanel();
        panel.setBounds(10, 319, 845, 121);
        frame.getContentPane().add(panel);
        
        JLabel DialogueBox = new JLabel("");
        panel.add(DialogueBox);
        
        JButton StartDialogueButton = new JButton("Start Dialogue");
        StartDialogueButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                 String strLine = "";
                    ArrayList < String > list = new ArrayList < String > ();
                    try {
                      BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Miko\\eclipse-workspace\\AltAssOne\\src\\FourthQuarter\\Script.txt"));
                      while (strLine != null) {
                        strLine = br.readLine();
                        if (strLine == null) {
                             break;
                        }
                        list.add(strLine);
                      }
//                    System.out.println(Arrays.toString(list.toArray()));
                      br.close();
                    }  catch (IOException ex) {
                      System.err.println("Unable to read the file.");
                    }


                //    ListIterator it = list.ListIterator();
                    ListIterator<String> it = list.listIterator();
                    
                    frame.addKeyListener(new KeyAdapter() {
                        @Override
                        public void keyTyped(KeyEvent e) {
                            
                            int keyCode = e.getKeyCode();
                            int IntegerQueue = 0;
                            
                            
                            switch(keyCode) {
                            case  KeyEvent.VK_RIGHT: 
                                if (it.hasNext()) {
                                    DialogueBox.setText((String) it.next());
                                    IntegerQueue = IntegerQueue+1;
                                }
                                break;
                            case KeyEvent.VK_LEFT:
                                if (it.hasNext()) {
                                    DialogueBox.setText((String) it.previous());
                                    IntegerQueue = IntegerQueue - 1;
                                }
                                break;                          }
                            if (keyCode == KeyEvent.VK_RIGHT){
                                //pressing right brings you to the next line in the list of lines. 
                                if (it.hasNext()) {
                                    DialogueBox.setText((String) it.next());
                                    IntegerQueue = IntegerQueue+1;
                                }
                            }
                            if (keyCode == KeyEvent.VK_LEFT){
                                //pressing right brings you to the previous line in the list of lines. 
                                if (it.hasNext()) {
                                    DialogueBox.setText((String) it.previous());
                                    IntegerQueue = IntegerQueue - 1;
                                }
                            }
                            
                        }
                    });
            }

        });
        StartDialogueButton.setBounds(10, 294, 126, 14);
        frame.getContentPane().add(StartDialogueButton);
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        
    }
}

标签: javalist

解决方案


推荐阅读