Tôi không hiểu hành vi gói trong JTextPane. Nếu tôi chèn một văn bản ngắn, sau đó một JComponent và sau đó một lần nữa các văn bản ngắn tôi có thể nhìn thấy những thứ chèn vào trong một dòng nếu khung là đủ lớn của khóa học. Nhưng nếu văn bản dài hơn nhiều thì phải mất vài dòng thì thành phần luôn được đặt trong một dòng mới.Làm thế nào để bọc văn bản xung quanh các thành phần trong một JTextPane?
Tôi đã nhận ra rằng sau khi một thành phần đã được chèn vào một JTextPane, văn bản của nó sẽ dài hơn một ký tự. Vì vậy, nếu một thành phần được xem xét bởi một JTextPane như là một nhân vật tại sao nó không cư xử như một nhân vật? Có thể nó phụ thuộc vào phiên bản java? Tôi sử dụng Java (TM) SE Runtime Environment (xây dựng 1.7.0-b147)
Dưới đây là mã của tôi (nhanh chóng currentText biến với shortText/longText để tạo lại hành vi nêu):
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
String shortText = "one two three four five six seven";
String longText = "A text component that can be marked up with attributes that are represented graphically. You can find how-to information and examples of using text panes in Using Text Components, a section in The Java Tutorial. This component models paragraphs that are composed of runs of character level attributes. Each paragraph may have a logical style attached to it which contains the default attributes to use if not overridden by attributes set on the paragraph or character run. Components and images may be embedded in the flow of text.";
String currentText = shortText;
try {
// insert text before the component
textPane.getDocument().insertString(textPane.getDocument().getLength(), currentText,
new SimpleAttributeSet());
textPane.setSelectionStart(textPane.getDocument().getLength());
textPane.setSelectionEnd(textPane.getDocument().getLength());
JComboBox component = new JComboBox();
component.setMaximumSize(component.getPreferredSize());
textPane.insertComponent(component);
// insert text after the component
textPane.getDocument().insertString(textPane.getDocument().getLength(), currentText,
new SimpleAttributeSet());
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
textPane.setEditable(false);
frame.add(new JScrollPane(textPane));
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Có, nó hoạt động. Tại sao bạn nghĩ rằng nó không phải là một ý tưởng tốt để sử dụng một JTextPane? Tôi muốn tạo một chương trình để thực hiện các bài tập. Bài tập phải chứa các hộp tổ hợp và trường văn bản. Người dùng cũng phải có khả năng "gạch dưới" (chọn) văn bản. Làm thế nào để làm điều đó theo cách khác? Sử dụng JLabels hoặc JTextFields bị vô hiệu hóa? Bạn không thể chọn văn bản của họ, phải không? Thậm chí nếu bạn có thể tôi nghĩ rằng sẽ không tốt nếu bạn thêm rất nhiều yếu tố này mỗi khi bạn có một đoạn văn bản. – ka3ak
@ ka3ak tốt, đó có thể là một trong vài trường hợp khi cách dễ nhất để làm việc :) –