我正在尝试创建一个文本为“ Testing”的窗口。该文本仅在某些时间出现。有人知道解决此问题的方法吗? 这是代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class project_1_6_b {
public static void main(String[] args) throws BadLocationException {
JFrame mainFrame = new JFrame();
JTextPane textArea1 = new JTextPane();
Container cp = mainFrame.getContentPane();
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setBold(attributeSet, true);
mainFrame.setSize(900, 700);
mainFrame.setVisible(true);
mainFrame.setTitle("JFrame Test");
mainFrame.setResizable(false);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = mainFrame.getSize().width;
int h = mainFrame.getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
mainFrame.setLocation(x, y);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea1.setCharacterAttributes(attributeSet, true);
textArea1.setText("Testing");
attributeSet = new SimpleAttributeSet();
StyleConstants.setItalic(attributeSet, true);
StyleConstants.setForeground(attributeSet, Color.red);
StyleConstants.setBackground(attributeSet, Color.blue);
JScrollPane scrollPane = new JScrollPane(textArea1);
cp.add(scrollPane, BorderLayout.CENTER);
}
}
出现该窗口,有时它显示为空白,但有时还显示文本。