我怎样才能让这个 JEditorPane 以我想要的方式显示?
How can I make this JEditorPane display the way I want?
开始显示是这样的:
然后正如预期的那样,当您用超出底部限制的文本填充它时,它看起来像这样:
但是文字有点偏边,这肯定与它激活的水平滚动条有关,但看起来真的很奇怪。那么如何才能禁用水平滚动条并让文本显示干净而不向右拖尾一点呢?
JEditorPane editorPane = new JEditorPane();
scrollPane.setViewportView(editorPane);
editorPane.setLineWrap(true);
editorPane.setLineWrap(真);不起作用,它说 "The method setLineWrap(boolean) is undefined for the type JEditorPane" 如果有办法在 JTextArea 中使用 HTML 告诉我;但我认为那行不通。我需要能够稍后添加 HTML。
完整代码:
import java.awt.EventQueue;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class MainGUI {
private JFrame frame;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainGUI window = new MainGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
// frame.setLocationRelativeTo(null);
frame.setBounds(500, 200, /*W*/600, /*H*/435);
// frame.setSize(/*W*/600, /*H*/435);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 564, 337);
frame.getContentPane().add(scrollPane);
JEditorPane editorPane = new JEditorPane();
scrollPane.setViewportView(editorPane);
textField = new JTextField();
textField.setBounds(10, 365, 564, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
}
}
如果编辑器窗格包含 HTML,自然行为是在达到最大宽度时换行。诀窍是告诉滚动窗格不要显示水平滚动条,因此它建议视图组件将大小限制为视图给定的任何宽度(该死的好)。
(实际的 MCVE)
import java.awt.*;
import javax.swing.*;
public class MainGUI {
private JFrame frame;
private JTextField textField;
private String[] loremIpsum = {
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ut nulla egestas arcu rhoncus molestie. Mauris eget consequat sem, a accumsan eros. Praesent sem massa, dignissim sit amet porta ut, interdum sit amet ligula. Pellentesque cursus odio vel vestibulum vulputate. Donec facilisis ex sed pellentesque laoreet. Praesent ut sem id lectus suscipit pulvinar. Mauris a eros dui. Nulla ultrices est eu lacinia sagittis. Vivamus ac erat sit amet dolor ultricies euismod. Curabitur condimentum in ante id mattis. Pellentesque non sagittis diam. Nulla dapibus lacus sagittis turpis vehicula, vitae eleifend odio euismod. Sed elementum facilisis nisl. Donec lectus nulla, rutrum ac sollicitudin pulvinar, tempus ut arcu. Maecenas ut vulputate purus. In luctus cursus magna sit amet facilisis.",
"Morbi vitae dictum enim, quis porttitor elit. Nam quis accumsan leo, id rhoncus tortor. Praesent tellus magna, ultrices quis nulla vel, fringilla ultricies purus. In hendrerit volutpat elit a posuere. Duis scelerisque, sapien eu faucibus eleifend, lectus lacus interdum diam, sed bibendum orci est ut urna. Quisque a sem iaculis ex aliquam tempor. In id dictum ex, non gravida lacus. Integer neque mauris, tristique eu ultrices quis, tristique pellentesque nunc. Donec egestas tristique dolor, ac sagittis ante elementum non.",
"Mauris sit amet ullamcorper erat. Nunc non condimentum augue. Vivamus et odio eget massa convallis vulputate nec ut lacus. Donec sapien dolor, facilisis sit amet venenatis sit amet, suscipit ut nisi. Praesent vitae lorem lacinia orci volutpat fermentum. Nullam dolor erat, facilisis vitae volutpat id, feugiat et augue. Nunc congue, lectus a vehicula lacinia, est libero volutpat quam, nec maximus ante mauris eu urna. Etiam nunc dolor, auctor et orci vitae, pulvinar pretium orci. Nam et est rhoncus, pharetra mauris id, condimentum leo. In fermentum non urna sed imperdiet. Cras sit amet nulla eget purus convallis accumsan. Donec dictum eget magna pulvinar scelerisque. Fusce efficitur nisl sed nibh vestibulum laoreet.",
"Morbi tincidunt eleifend nisl. Nullam libero massa, imperdiet vitae egestas eu, sodales ac lacus. Donec eu commodo dui. Suspendisse potenti. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean vel diam volutpat, gravida quam vitae, tristique dui. Cras aliquet gravida massa eget lobortis. Donec ullamcorper eu purus eget iaculis. Ut id leo erat. Etiam iaculis ante eu mi molestie suscipit.",
"Quisque dapibus consectetur mauris eu faucibus. Etiam ac varius odio. Nulla non elit velit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vivamus est leo, ultricies quis sem ut, euismod vestibulum augue. Donec auctor eu nisi a aliquam. Integer libero sapien, fringilla ac posuere ut, malesuada ut leo. Maecenas ut ante non arcu molestie gravida nec eu neque. Vivamus elit nisi, auctor at luctus eget, accumsan id dolor."
};
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainGUI window = new MainGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("HTML in editor pane");
frame.setLocationByPlatform(true);
//frame.setBounds(500, 200, /*W*/ 600, /*H*/ 435); this is guessing!
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
StringBuilder sb = new StringBuilder(
"<html><body style='width: 400px;'>");
for (String s : loremIpsum) {
sb.append("<p>");
sb.append(s);
}
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setText(sb.toString());
JScrollPane scrollPane = new JScrollPane(
editorPane,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
// this is an important line!
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
editorPane.setCaretPosition(0);
frame.getContentPane().add(scrollPane);
frame.pack();
// it should be the correct size needed to display the content,
// but now let's make it half that height.
Dimension d = frame.getSize();
d = new Dimension(d.width, d.height/2);
frame.setSize(d);
}
}
开始显示是这样的:
然后正如预期的那样,当您用超出底部限制的文本填充它时,它看起来像这样:
但是文字有点偏边,这肯定与它激活的水平滚动条有关,但看起来真的很奇怪。那么如何才能禁用水平滚动条并让文本显示干净而不向右拖尾一点呢?
JEditorPane editorPane = new JEditorPane();
scrollPane.setViewportView(editorPane);
editorPane.setLineWrap(true);
editorPane.setLineWrap(真);不起作用,它说 "The method setLineWrap(boolean) is undefined for the type JEditorPane" 如果有办法在 JTextArea 中使用 HTML 告诉我;但我认为那行不通。我需要能够稍后添加 HTML。
完整代码:
import java.awt.EventQueue;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class MainGUI {
private JFrame frame;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainGUI window = new MainGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
// frame.setLocationRelativeTo(null);
frame.setBounds(500, 200, /*W*/600, /*H*/435);
// frame.setSize(/*W*/600, /*H*/435);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 564, 337);
frame.getContentPane().add(scrollPane);
JEditorPane editorPane = new JEditorPane();
scrollPane.setViewportView(editorPane);
textField = new JTextField();
textField.setBounds(10, 365, 564, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
}
}
如果编辑器窗格包含 HTML,自然行为是在达到最大宽度时换行。诀窍是告诉滚动窗格不要显示水平滚动条,因此它建议视图组件将大小限制为视图给定的任何宽度(该死的好)。
(实际的 MCVE)
import java.awt.*;
import javax.swing.*;
public class MainGUI {
private JFrame frame;
private JTextField textField;
private String[] loremIpsum = {
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ut nulla egestas arcu rhoncus molestie. Mauris eget consequat sem, a accumsan eros. Praesent sem massa, dignissim sit amet porta ut, interdum sit amet ligula. Pellentesque cursus odio vel vestibulum vulputate. Donec facilisis ex sed pellentesque laoreet. Praesent ut sem id lectus suscipit pulvinar. Mauris a eros dui. Nulla ultrices est eu lacinia sagittis. Vivamus ac erat sit amet dolor ultricies euismod. Curabitur condimentum in ante id mattis. Pellentesque non sagittis diam. Nulla dapibus lacus sagittis turpis vehicula, vitae eleifend odio euismod. Sed elementum facilisis nisl. Donec lectus nulla, rutrum ac sollicitudin pulvinar, tempus ut arcu. Maecenas ut vulputate purus. In luctus cursus magna sit amet facilisis.",
"Morbi vitae dictum enim, quis porttitor elit. Nam quis accumsan leo, id rhoncus tortor. Praesent tellus magna, ultrices quis nulla vel, fringilla ultricies purus. In hendrerit volutpat elit a posuere. Duis scelerisque, sapien eu faucibus eleifend, lectus lacus interdum diam, sed bibendum orci est ut urna. Quisque a sem iaculis ex aliquam tempor. In id dictum ex, non gravida lacus. Integer neque mauris, tristique eu ultrices quis, tristique pellentesque nunc. Donec egestas tristique dolor, ac sagittis ante elementum non.",
"Mauris sit amet ullamcorper erat. Nunc non condimentum augue. Vivamus et odio eget massa convallis vulputate nec ut lacus. Donec sapien dolor, facilisis sit amet venenatis sit amet, suscipit ut nisi. Praesent vitae lorem lacinia orci volutpat fermentum. Nullam dolor erat, facilisis vitae volutpat id, feugiat et augue. Nunc congue, lectus a vehicula lacinia, est libero volutpat quam, nec maximus ante mauris eu urna. Etiam nunc dolor, auctor et orci vitae, pulvinar pretium orci. Nam et est rhoncus, pharetra mauris id, condimentum leo. In fermentum non urna sed imperdiet. Cras sit amet nulla eget purus convallis accumsan. Donec dictum eget magna pulvinar scelerisque. Fusce efficitur nisl sed nibh vestibulum laoreet.",
"Morbi tincidunt eleifend nisl. Nullam libero massa, imperdiet vitae egestas eu, sodales ac lacus. Donec eu commodo dui. Suspendisse potenti. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean vel diam volutpat, gravida quam vitae, tristique dui. Cras aliquet gravida massa eget lobortis. Donec ullamcorper eu purus eget iaculis. Ut id leo erat. Etiam iaculis ante eu mi molestie suscipit.",
"Quisque dapibus consectetur mauris eu faucibus. Etiam ac varius odio. Nulla non elit velit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vivamus est leo, ultricies quis sem ut, euismod vestibulum augue. Donec auctor eu nisi a aliquam. Integer libero sapien, fringilla ac posuere ut, malesuada ut leo. Maecenas ut ante non arcu molestie gravida nec eu neque. Vivamus elit nisi, auctor at luctus eget, accumsan id dolor."
};
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainGUI window = new MainGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("HTML in editor pane");
frame.setLocationByPlatform(true);
//frame.setBounds(500, 200, /*W*/ 600, /*H*/ 435); this is guessing!
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
StringBuilder sb = new StringBuilder(
"<html><body style='width: 400px;'>");
for (String s : loremIpsum) {
sb.append("<p>");
sb.append(s);
}
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setText(sb.toString());
JScrollPane scrollPane = new JScrollPane(
editorPane,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
// this is an important line!
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
editorPane.setCaretPosition(0);
frame.getContentPane().add(scrollPane);
frame.pack();
// it should be the correct size needed to display the content,
// but now let's make it half that height.
Dimension d = frame.getSize();
d = new Dimension(d.width, d.height/2);
frame.setSize(d);
}
}