我正在制作一个infoscreen来显示文字和图片。关于它的文本部分,我有一个问题。
我有一个JFrame作为显示器的主窗口。在这个框架中,我添加了jpanels来显示文本和图片。然后当它显示时,我再次移除面板。这部分很容易用图片,因为我知道我想要显示多长时间。但是,对于文本,我需要将线程置于睡眠状态,然后在我的scrollPane到达底部时将其唤醒。
这是我添加面板的地方
for (File f : files) {
String fileName = f.getName();
if (fileName.endsWith(".txt")) {
TextPanel txtpan = new TextPanel(f, this, current.getFormat().getFont());
add(txtpan);
pack();
//Input sleep here
remove(txtpan);
}
if (fileName.endsWith(".png") || fileName.endsWith(".jpg") || fileName.endsWith("bmp")) {
AlbumPanel albumpan = new AlbumPanel(connect, f, this, current.getFormat().getPicColor(), current.getFormat().getPicLength());
add(albumpan);
pack();
try {
Thread.sleep(current.getFormat().getPicLength() * 1000 + 520);
} catch (InterruptedException ex) {
}
remove(albumpan);
}
这是我添加的textpanel
public class TextPanel extends JPanel {
File file;
JFrame presWin;
ArrayList<String> lines;
Font font;
JTextArea textArea;
public TextPanel(File file, JFrame presWin, Font font) {
this.file = file;
this.presWin = presWin;
this.font = font;
this.setLayout(new GridBagLayout());
this.setBackground(Color.white);
readFile();
if (!lines.isEmpty()) {
displayWords();
}
}
private void readFile() {
lines = new ArrayList();
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while ((line = in.readLine()) != null) {
lines.add(line);
}
in.close();
} catch (Exception ex) {
}
}
private void displayWords() {
textArea = new JTextArea();
textArea.setFont(font);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
for (String s : lines) {
textArea.append(s + System.getProperty("line.separator"));
}
JScrollPane scrollpane = new JScrollPane(textArea);
scrollpane.setPreferredSize(this.getPreferredSize());
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scrollpane.setViewportView(textArea);
System.out.println(scrollpane.getVerticalScrollBar().getMaximum());
Timer t=new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
scrollpane.getVerticalScrollBar().setValue(scrollpane.getVerticalScrollBar().getValue()+1);
if (scrollpane.getVerticalScrollBar().getValue()>=scrollpane.getVerticalScrollBar().getMaximum()) {
((Timer)e.getSource()).stop();
}
}
});
t.setRepeats(true);
t.start();
add(scrollpane);
repaint();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(presWin.getWidth(), presWin.getHeight());
}
我试过在textpanel中等待一个布尔值的while循环