【问题标题】:testing suite with swing带秋千的测试套件
【发布时间】:2014-03-04 23:55:49
【问题描述】:

我开始用 java 制作一个“测试套件”。我目前正在进行句子完成类型测试。基本上我有一个带有占位符的文本,用户应该在其中输入一些文本,稍后将对其进行评估。我发现通过使用FlowLayout,我可以将JLabels 和JTextFields 放在一起。问题是当一段文本太长时。它应该跨越多行,我不确定如何实际做到这一点。虽然没关系,但如果我将一个小文本从行尾推送到新行,但如果整个文本块长于行宽,我仍然会卡住。

而且我不想重新发明轮子,那么是否有任何用于测试套件的开源库?我的 googlefu 失败了。

【问题讨论】:

    标签: java swing jlabel jtextfield


    【解决方案1】:

    我发现这个问题的最佳解决方案是使用 Rob Camick 的 WrapLayout

    WrapLayout 本质上是FlowLayout 的扩展,当内容无法水平放置时,它会包裹内容。

    查看上面的链接博客,它解释了您遇到问题的原因。

    更新

    另一种选择是使用JTextPane 并将字段插入到,例如...

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.HeadlessException;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.JTextPane;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.StyledDocument;
    
    public class TestText {
    
        public static void main(String[] args) {
            new TestText();
        }
    
        public TestText() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JTextPane tp = new JTextPane();
                    tp.replaceSelection("Asd, asd, asd, fgh ");
                    addField(tp);
                    tp.replaceSelection(" more funky text here ");
                    addField(tp);
                    tp.replaceSelection(" and this must wrap on the edge. The color code of red is: #");
                    addField(tp);
                    tp.replaceSelection(". ");
                    tp.setEditable(false);
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JScrollPane(tp));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
                protected void addField(JTextPane tp) {
                    JTextField field = new JTextField(10);
                    field.setAlignmentY(0.75f);
                    tp.insertComponent(field);
                }
            });
        }
    
    }
    

    注意,编辑器本身是不可编辑的,但文本字段是……

    【讨论】:

    • 我早上会看看这个,但这不是只是重新排列整个 JLabel,如果它不适合那里,而不是拆分?
    • 是的,就是这样。另一种选择是使用JEditorPane 并将字段直接添加到编辑器中......
    • 谢谢!我真的忘了JTextPane
    猜你喜欢
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多