2

我正在尝试使用摆动布局实现一个简单的“控制台”,我将能够以不同的线条和不同的颜色向每个控制台添加简单的消息。

我已经能够做某事,但我仍然有问题。

我对此进行了 2 次尝试。

1:带网格布局:

    summeryGrid = new JPanel(new GridLayout(0, 1));
    summeryGrid.setBounds(10, 140, 600, 300);
    summeryGrid.add(createErrorMessage("some error message"));
    summeryGrid.add(createErrorMessage("some error message1"));
    summeryGrid.add(createErrorMessage("some error message2"));

这实际上工作正常。但我遇到的问题是,如果我没有足够的消息,它们之间就会存在巨大的差距。我确实尝试设置 hgap 和 vgap 但它们在这件事上没有影响。

2:尝试用流式布局。但是由于某种原因,那个该死的可怕布局似乎只能垂直而不是水平地工作。所以这不好,除非我能改变它的布局

4

1 回答 1

4

您可以使用单个 JEditorPane 来显示整个内容。

这是一个非常简单的例子:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class TestConsole {

    private static final String TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec purus sapien, molestie dapibus feugiat vitae, pharetra lobortis lacus. Proin metus neque, malesuada vel consectetur vel, imperdiet et mauris. Vivamus vel tortor ipsum, ac semper ipsum. Nam semper tellus et purus molestie vestibulum. Aliquam erat volutpat. Nam vulputate facilisis magna id sollicitudin. Donec rutrum lorem sit amet orci lacinia congue. Ut nec nibh ipsum, et ornare tellus. Etiam nisi massa, mollis eu viverra id, luctus sed massa. Donec tincidunt erat vel sapien varius ultricies. Vivamus dui diam, consequat nec facilisis ut, interdum at enim. Vestibulum vestibulum, lorem nec cursus eleifend, purus orci egestas quam, vel sodales sem magna at nibh. Cras id nibh eleifend turpis sollicitudin adipiscing. Nunc aliquet posuere vulputate. Suspendisse id augue ut quam mattis sollicitudin. ";

    private static final int ROWS = 24;
    private static final int COLUMNS = 80;

    private JEditorPane console;

    public void append(final String s, final Color color) {
        if (!SwingUtilities.isEventDispatchThread()) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    append(s, color);
                }
            });
            return;
        }
        Document document = console.getDocument();
        MutableAttributeSet sas = new SimpleAttributeSet();
        StyleConstants.setForeground(sas, color);
        try {
            document.insertString(document.getLength(), s, sas);
        } catch (BadLocationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void initUI() {
        JFrame frame = new JFrame(TestConsole.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        console = new JEditorPane() {
            @Override
            public Dimension getPreferredSize() {
                Dimension d = super.getPreferredSize();
                FontMetrics fm = getFontMetrics(getFont());
                int colWidth = fm.charWidth('m');
                int rowHeight = fm.getHeight();
                d.width = Math.max(d.width, rowHeight * ROWS);
                d.height = Math.max(d.height, colWidth * COLUMNS);
                return d;
            }
        };
        console.setEditable(false);
        console.setContentType("text/html");
        console.setForeground(Color.WHITE);
        console.setBackground(Color.BLACK);
        console.setFont(new Font("Consolas", Font.PLAIN, 14));
        frame.add(new JScrollPane(console));
        frame.pack();
        frame.setVisible(true);
        Timer t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            Random random = new Random();

            @Override
            public void run() {
                int start = random.nextInt(TEXT.length());
                int size = random.nextInt(TEXT.length() - start);
                append(TEXT.substring(start, start + size) + "\n", new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
            }
        }, new Date(), 1000);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestConsole().initUI();
            }
        });
    }

}
于 2013-01-21T12:14:20.653 回答