gpt4 book ai didi

java - 使用 Java Swing,激活阅读器的按钮不会显示所需的标签?

转载 作者:太空宇宙 更新时间:2023-11-04 14:27:57 25 4
gpt4 key购买 nike

我正在从文本文件中读取长引用,因此所有标签均已设置,但按钮不会显示该内容的 JLabel。我可以显示系统打印,但标签不会出现,因此按钮似乎没问题。非常具体地说,引用是“克里斯·塔克版本的马丁·路德·金演讲的 html 格式文本文档。

这是我迄今为止尝试过的:

package MenuBoxes;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class quoteReader extends JFrame{
private JFrame frame;
public quoteReader(){
initUI();
}
public final void initUI(){ {
String listArray[] = null;
JButton click = new JButton("click me");
click.setLocation(120,30);
click.setSize(100,100);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.add(click);
add(panel);
setTitle("Quote Reader");
setSize(500,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
click.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//nameLabel = name.getText();
//use .= for comparing strings
String[] listArray = new String [2];
try{
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Chrt\\workspace\\Finals\\Quotes.txt"));
int index = 0;
String line = "";
while((line = br.readLine()) != null) {
listArray[index] = line;
index++;
JLabel read = new JLabel(line);
System.out.println(line);
read.setLocation(120,90);
read.setSize(300,200);
read.setFont(new Font("Calibri",Font.BOLD,13));
panel.add(read);
add(panel);
setVisible(true);
}
br.close();
} catch (IOException ioe){
System.out.println("Cannot read");
System.exit(0);
}
}
}
);

}}}

最佳答案

向标签添加文本后需要重新绘制。

this.repaint();

此外,根据您的代码,在将文本添加到 JLabel 之前,您不会使 JFrame 可见。你应该添加

setVisible(true);

到你的 initUI() 方法。

关于java - 使用 Java Swing,激活阅读器的按钮不会显示所需的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26467780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com