我想实现以下目标:
+------------------Other container(s)-----------------+
| +------JScrollPane (vertical)-------+ |
| | JTextField | |
| | Box.createRigidArea (vertical) | |
| | JTextArea | |
| | { etc.. any other J-component } | |
| | | |
| | | |
| | | |
| | | |
| +-----------------------------------+ |
+-----------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
我能得到的最接近的是以下(伪)代码:
JPanel container = new JPanel(new BorderLayout());
JPanel innerContainer = new JPanel();
innerContainer.setLayout(new BoxLayout(_innerContainer, BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(innerContainer);
container.add(scrollPane, BorderLayout.NORTH);
Run Code Online (Sandbox Code Playgroud)
每当我想添加组件时,它们都会添加到内部容器中:
innerContainer.add(new JTextField());
innerContainer.add(Box.createRigidArea(new Dimension(0, …Run Code Online (Sandbox Code Playgroud) 我不理解JTextPane中的包装行为.如果我插入一个短文本,然后是一个JComponent然后再短文本我可以在一行中看到插入的东西,如果框架当然足够大.但是如果文本更长,那么它需要几行,组件总是放在一个新行中.
我已经认识到,在将一个组件插入JTextPane后,它的文本会变长一个字符.因此,如果一个组件被JTextPane视为一个字符,为什么它不像一个字符呢?可能它取决于java版本?我使用Java(TM)SE运行时环境(版本1.7.0-b147)
下面是我的代码(使用shortText/longText实例化变量currentText以重现上述行为):
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 …Run Code Online (Sandbox Code Playgroud)