java - 为什么按箭头键时 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
}
}
解决方案
推荐阅读
- delphi - Delphi中TToolButton Caption的多行文本
- laravel - Vue js PATCH http://127.0.0.1:8000/api/auth/profile 401(未经授权)
- markdown - 在 Markdown 中格式化深层列表
- php - 匹配令牌及其可能重叠的上下文
- javascript - mapbox-gl-js 路线不准确
- rdf - 与“小”个体打交道的最佳方法是什么?
- c++ - 使用管道从 STDIN 读取分叉进程的问题
- php - 如何将数组添加到我的 php 变量中,循环不适用于数组
- javascript - 正则表达式匹配字符串的句子和单词
- bash - 如何在别名或函数中表达多个键值对?