Java 如何创建和使用JTextPane
我想用Java创建一个程序,它是这样做的: 一个包含按钮和组件(JTextArea、JTextPane等)的窗口,这些组件无法修改,其中会根据某些工作的执行显示一些字符串。 如图所示,如果作业成功,文本将为黑色,如果有错误,文本将标记为红色 我使用JTextArea和JButton成功地完成了所有操作,但我发现不能逐行更改字符串的颜色 我读到应该使用JTextPane,但我一直无法使用它。 以下是我编写的代码:Java 如何创建和使用JTextPane,java,swing,jtextarea,jtextpane,Java,Swing,Jtextarea,Jtextpane,我想用Java创建一个程序,它是这样做的: 一个包含按钮和组件(JTextArea、JTextPane等)的窗口,这些组件无法修改,其中会根据某些工作的执行显示一些字符串。 如图所示,如果作业成功,文本将为黑色,如果有错误,文本将标记为红色 我使用JTextArea和JButton成功地完成了所有操作,但我发现不能逐行更改字符串的颜色 我读到应该使用JTextPane,但我一直无法使用它。 以下是我编写的代码: public class Example { public Example
public class Example {
public Example() {
JFrame frame = new JFrame();
JTextPane pane = new JTextPane();
JButton button = new JButton("Button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane.setPreferredSize(new Dimension(200, 200));
frame.add(pane);
frame.add(button);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new Example();
}
}
当我运行程序时,会创建以下内容:
文本窗格在哪里
另外,在我使用append()在JTextArea中添加文本之前,我还没有找到类似于JTextPane的方法?你如何使用它?
如何更改单行的颜色
我读过、看过并尝试过互联网上的各种例子,但我什么都做不完。。。
有类似的例子吗
我为“通用”请求道歉
谢谢
试试这个例子
试试这个例子我建议您查看JavaFX,它有一个更好的UI。
但是,您已经在使用JPanel和JButton。您可以使用JLabel显示所需的文本,并使用
JLabel.setTextFill(Color.WHITESMOKE)
设置文本的颜色 我建议您查看JavaFX,它有更好的用户界面。
但是,您已经在使用JPanel和JButton。您可以使用JLabel显示所需的文本,并使用
JLabel.setTextFill(Color.WHITESMOKE)
设置文本的颜色 这是一个更好的开始:
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class test {
public test() throws BadLocationException {
JFrame frame = new JFrame();
DefaultStyledDocument document = new DefaultStyledDocument();
JTextPane pane = new JTextPane(document);
JPanel mainPanel = new JPanel();
JButton button = new JButton("Button");
button.setPreferredSize(new Dimension(100, 40));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane.setPreferredSize(new Dimension(200, 200));
mainPanel.add(button);
frame.getContentPane().add(pane, BorderLayout.CENTER);
frame.getContentPane().add(mainPanel, BorderLayout.WEST);
StyleContext context = new StyleContext();
// build a style
Style style = context.addStyle("test", null);
// set some style properties
StyleConstants.setForeground(style, Color.BLACK);
document.insertString(0, "Four: success \n", style);
StyleConstants.setForeground(style, Color.RED);
document.insertString(0, "Three: error \n", style);
document.insertString(0, "Two: error \n", style);
StyleConstants.setForeground(style, Color.BLACK);
// add some data to the document
document.insertString(0, "One: success \n", style);
// StyleConstants.setForeground(style, Color.blue);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) throws BadLocationException {
new test();
}
}
这是非常基本的,但应该让你开始。当您必须添加文本和层叠颜色时,可以创建不同的方法。这是一个更好的开始:
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class test {
public test() throws BadLocationException {
JFrame frame = new JFrame();
DefaultStyledDocument document = new DefaultStyledDocument();
JTextPane pane = new JTextPane(document);
JPanel mainPanel = new JPanel();
JButton button = new JButton("Button");
button.setPreferredSize(new Dimension(100, 40));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane.setPreferredSize(new Dimension(200, 200));
mainPanel.add(button);
frame.getContentPane().add(pane, BorderLayout.CENTER);
frame.getContentPane().add(mainPanel, BorderLayout.WEST);
StyleContext context = new StyleContext();
// build a style
Style style = context.addStyle("test", null);
// set some style properties
StyleConstants.setForeground(style, Color.BLACK);
document.insertString(0, "Four: success \n", style);
StyleConstants.setForeground(style, Color.RED);
document.insertString(0, "Three: error \n", style);
document.insertString(0, "Two: error \n", style);
StyleConstants.setForeground(style, Color.BLACK);
// add some data to the document
document.insertString(0, "One: success \n", style);
// StyleConstants.setForeground(style, Color.blue);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) throws BadLocationException {
new test();
}
}
这是非常基本的,但应该让你开始。当您必须添加文本和层叠颜色时,可以创建不同的方法。为什么要在窗格中添加空颜色样式?能不能只做一次?在StyleConstants中设置颜色做什么?为什么要在窗格中添加空颜色样式?能不能只做一次?在StyleConstants中设置颜色做什么?