公共類 Frame2 擴展 JFrame {
private JPanel contentPane;
Frame1 frms = new Frame1();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame2 frame = new Frame2();
frame.setVisible(true);
frame.setResizable(false);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Frame2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(250, 100, 799, 526);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// ...
final JTextPanePlus pnText1 = new JTextPanePlus(); // final because listener requires this
pnText1.setImage(icon4, -70, -100, 600, 500);
pnText1.setVisible(false);
pnText1.setFont(new Font("Tahoma", Font.PLAIN , 16));
pnText1.setEditable(false);
pnText1.setForeground(Color.WHITE);
pnText1.setText("Some text dasdasdasdasdasdas");
pnText1.setBounds(60, 343, 527, 111);
contentPane.add(pnText1);
//...
backgroundLabel.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent arg0) {
pnText1.setVisible(true);
}
});
// ...
}
為 JTextPane 選擇影像的方法
公共類 JTextPanePlus 擴展 JTextPane {
Image image;
int x,y,width,height;
public JTextPanePlus(){
super();
}
public JTextPanePlus(String text){
super();
}
public void setImage(ImageIcon icon, int x, int y, int width, int height) {
this.image = icon.getImage();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
setOpaque(false);
repaint();
}
public void paint(Graphics g){
g.drawImage(image, x, y, width, height, null);
super.paint(g);
}
}
我無法使 JTextPane (JTextPanePlus) 的文本在每個字符之間出現略微延遲。基本上我想創造一種像小說或冒險一樣的效果。如果用戶不想等待整個文本出現,我可以添加一個“mouseListener”并使文本立即出現(停止該方法)。
現在的問題是我不知道如何做到這一點。這里的大多數主題都涵蓋了 JTextArea 的內容,我不確定 JTextPane 是否也可以這樣做。
I tried to do "for(char c : s.toCharArray())" but then I have no idea what to do with "c" and this method seems to only work for print. Tried to use "Thread.sleep" but this freezes the whole JFrame. Java.swing.timer could work but then JTextPane doesn't support ".append" when used.
I'm running out of options and I'm not an expert for this. This is my first time working with something this hard. Also I'm truly sorry if this wasn't explained at best.
uj5u.com熱心網友回復:
其基本思想是相同的JTextPane和JTextArea。問題是,JTextPane就它的渲染功能而言,它的功能更強大,并且不提供一些幫助方法JTextArea,例如append,相反,您必須自己這樣做。
話雖如此,這是一個非常基本的例子。此示例從嵌入的檔案資源加載“星球大戰,新希望”腳本并列印它,就像打字機一樣。
如果您點擊Stop,它將停止Timer并附加當前行的剩余文本。有趣的是,如果你再次開始它,它會從它停止的地方開始,整潔的副作用。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.Timer;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextPane textPane;
private Timer timer;
private List<String> lines;
private String currentLine;
private int currentLinePosition;
public TestPane() {
setLayout(new BorderLayout());
textPane = new JTextPane();
add(new JScrollPane(textPane));
JPanel actionsPane = new JPanel(new GridBagLayout());
JButton makeItSo = new JButton("Make it so");
JButton stop = new JButton("Stop");
actionsPane.add(makeItSo);
makeItSo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
makeItSo();
makeItSo.setEnabled(false);
stop.setEnabled(true);
}
});
actionsPane.add(stop);
stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stopTimer();
makeItSo.setEnabled(true);
stop.setEnabled(false);
}
});
add(actionsPane, BorderLayout.SOUTH);
loadScript();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
protected void loadScript() {
lines = new ArrayList<>(128);
try (BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/scripts/StarWarANewHope.txt")))) {
for (String line = br.readLine(); line != null; line = br.readLine()) {
lines.add(line);
}
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
protected int startPosition(String text) {
if (text.isEmpty() || text.isBlank()) {
return 0;
}
int index = 0;
while (Character.isWhitespace(text.charAt(index))) {
index ;
}
return index;
}
protected void insertWithOutError(String text) {
try {
insert(text);
} catch (BadLocationException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
protected void insert(String text) throws BadLocationException {
Document document = textPane.getDocument();
document.insertString(document.getLength(), text, null);
}
protected void stopTimer() {
if (timer != null) {
timer.stop();
timer = null;
}
if (currentLine != null) {
if (currentLinePosition < currentLine.length()) {
String text = currentLine.substring(currentLinePosition);
try {
insert(text);
insertWithOutError("\n");
} catch (BadLocationException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
currentLine = null;
}
protected void makeItSo() {
stopTimer();
if (lines.isEmpty()) {
return;
}
try {
// Pop the first line
currentLine = lines.remove(0);
int offset = startPosition(currentLine);
if (currentLine.isBlank() || offset == currentLine.length()) {
insertWithOutError("\n");
} else {
String leading = currentLine.substring(0, offset);
insert(leading);
}
currentLinePosition = 0;
currentLine = currentLine.trim();
timer = new Timer(5, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentLinePosition < currentLine.length()) {
try {
String next = currentLine.substring(currentLinePosition, currentLinePosition 1);
insert(next);
currentLinePosition ;
} catch (BadLocationException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
stopTimer();
}
} else {
insertWithOutError("\n");
makeItSo();
}
}
});
timer.start();
} catch (BadLocationException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
現在,JTextPane可以支持StyledDocuments,這使它變得有些復雜。你會注意到我什至沒有嘗試支持“屬性”,你必須自己解決這個問題??
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/370285.html
標籤:java swing animation jtextpane
