4

我在使用JTextArea. 我的实际设置有所不同,但效果仍然存在。这是问题的图像:

在此处输入图像描述

当拥有者调整大小仅比他们的首选大小所需的JDialog1 个像素时,文本区域突然调整大小。JTextArea在我的实际设置中,它们突然变高了。我正在使用GridBagLayout,但它似乎发生在其他布局中。为什么是这样?

这是重现上述效果的易于编译的代码。

import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.*;
import javax.swing.text.JTextComponent;

public class TextDemo extends JDialog implements ActionListener {
    private static final long serialVersionUID = -589374238138963529L;
    protected JTextField textField;
    protected JTextArea textArea;
    private final static String newline = "\n";

    private static final java.awt.Dimension SCREENSIZE = 
            java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    private static final java.awt.Point SCREENCENTER =
        new java.awt.Point(SCREENSIZE.width/2,SCREENSIZE.height/2);

    public TextDemo(Window owner, String shortMessage, String message, JComponent accessory) {
        super(owner);

        setTitle("Test");
        setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

        Icon icon = UIManager.getIcon("OptionPane.warningIcon");    

        JTextArea shortText = makeMultiLineLabel(true);
        shortText.setBorder(BorderFactory.createEtchedBorder());
        shortText.setFont(shortText.getFont().deriveFont(Font.BOLD));
        shortText.setRows(2);
        shortText.setColumns(20);
        shortText.setText(shortMessage); 

        JTextArea messageText = makeMultiLineLabel(true);
        messageText.setBorder(BorderFactory.createEtchedBorder());
        messageText.setFont(shortText.getFont().deriveFont(Font.PLAIN));
        messageText.setRows(4);
        messageText.setColumns(20);
        messageText.setText(message);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton("OK"));
        buttonPanel.add(new JButton("Cancel"));

        JPanel contentPanel = new JPanel();
        contentPanel.setOpaque(true);
        contentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 8, 9));
        contentPanel.setLayout(new GridBagLayout());
        GridBagConstraints c;

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.gridheight = 2;
        contentPanel.add(new JLabel(icon), c);

        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 0;
        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1.0;
        c.weightx = 1.0;
        contentPanel.add(shortText, c);

        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 1;
        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1.0;
        c.weightx = 1.0;
        contentPanel.add(messageText, c);

        if (accessory != null) {
            c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 2;
            c.gridwidth = 2;
            c.fill = GridBagConstraints.BOTH;
            c.weighty = 1.0;
            c.weightx = 1.0;
            contentPanel.add(accessory, c);
        }
        c = new GridBagConstraints();
        c.gridwidth = 2;
        c.gridx = 0;
        c.gridy = 3;
        contentPanel.add(buttonPanel, c);

        setContentPane(contentPanel);
    }

    public void actionPerformed(ActionEvent evt) {
        String text = textField.getText();
        textArea.append(text + newline);
        textField.selectAll();

        //Make sure the new text is visible, even if there
        //was a selection in the text area.
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TextDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {
            throw new Exception("Test");
        } catch (Exception e) {
            TextDemo t = new TextDemo(frame, "You won't get away with this!", 
                    "Alert! Alert! A chocy nut bar has been removed without payment!" +
                    " A chocy nut bar... has been REMOVED! WITHOUT PAYMENT! Alert, alert!",
                    getStackTraceTextArea(e));
            //Display the window.
            frame.pack();
            frame.setLocation(SCREENCENTER.x - frame.getSize().width/2,
                              SCREENCENTER.y - frame.getSize().height/2);
            frame.setVisible(true);

            t.setModal(true);
            t.pack();
            t.setLocation(getPos(t, t.getOwner()));
            t.setVisible(true);
        }
    }

    protected static JComponent getStackTraceTextArea(Throwable exception) {
        JTextArea textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setLineWrap(false);
        textArea.append(getTraceMessage(exception));
        textArea.setCaretPosition(0);
        JScrollPane scroll = new JScrollPane(textArea);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setPreferredSize(new Dimension(50, 140));
        return scroll;
    }

    private static final String getTraceMessage(Throwable exception) {
        StringBuilder out = new StringBuilder((new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"))
                .format(new Date())+": Unhandled Exception: \n"
                +exception.toString()+"\n\nStack Trace:\n");
        StackTraceElement[] stackTrace = exception.getStackTrace();
        for (int i = 0; i < stackTrace.length; i++) {
            String toAppend = stackTrace[i].toString();
            if (i != stackTrace.length-1) toAppend += "\n";
            out.append(toAppend);
        }
        return out.toString();
    }

    public static final JTextArea makeMultiLineLabel(boolean selectable) {
        JTextArea area = new JTextArea();
        area.setWrapStyleWord(true);
        area.setLineWrap(true);
        area.setFont(UIManager.getFont("Label.font"));
        area.setEditable(false);
        area.setCursor(null);
        area.setOpaque(false);
        area.setFocusable(selectable);
        area.setAlignmentX(JTextComponent.LEFT_ALIGNMENT);
        area.setMinimumSize(new Dimension(0,0));
        return area;
    }

    private static Point getPos(JDialog d, Window w) {
        return new Point(w.getX()+(w.getWidth ()-d.getWidth ())/2,
                         w.getY()+(w.getHeight()-d.getHeight())/2);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

编辑建议实施一些更改,问题仍然存在:

在此处输入图像描述

4

1 回答 1

5

您目睹的问题是GridBagLayout试图处理无法兑现preferredSize组件的情况,它会恢复为使用组件minimumSize...

您可以使用该GridBagConstraints#weightx属性强制组件始终填充其列宽...

c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
c.weighty = 1.0;
c.weightx = 1.0;
contentPanel.add(shortText, c);

c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.BOTH;
c.weighty = 1.0;
c.weightx = 1.0;
contentPanel.add(messageText, c);

这不会阻止它缩小,但会阻止它从一种尺寸“捕捉”到另一种尺寸。

尽量避免使用setPreferredSize,查看我应该避免在 Java Swing 中使用 set(Preferred|Maximum|Minimum)Size 方法吗?更多细节。而是使用...的rowsandcolumns属性JTextArea

shortText.setRows(2); // for example

就个人而言,我也会将JTextArea's 包裹在 a 中JScrollPane,然后为每个人提供足够的空间变得稍微不那么重要了

回馈...

现在,这个问题已经断章取义了,但在我看来,你会付出很多努力却收效甚微。

例如,相反,您可以使用JOptionPane和利用 Swing 的 HTML 呈现功能......

在此处输入图像描述

import java.awt.EventQueue;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class OptionPaneTest {

    public static void main(String[] args) {
        new OptionPaneTest();
    }

    public OptionPaneTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                StringBuilder sb = new StringBuilder(128);
                sb.append("<html><b><p align=center>You won't get away with this!</p></b><br>");
                sb.append("Alert! Alert! A chocy nut bar has been removed without payment!");
                sb.append("<br>A chocy nut bar... has been REMOVED! WITHOUT PAYMENT! Alert, alert!");

                JOptionPane.showMessageDialog(null, sb.toString(), "Alert", JOptionPane.WARNING_MESSAGE);

            }
        });
    }        
}

还...

我想你会发现使用...

frame.setLocationRelativeTo(null);

然后更容易,更省时......

frame.setLocation(SCREENCENTER.x - frame.getSize().width / 2,
                    SCREENCENTER.y - frame.getSize().height / 2);

这也意味着您也可以使用t.setLocationRelativeTo(frame)...

哦,红矮星参考也是 +1 ;)

从问题的更新更新

解决方案仍然(基本上)相同,使用JTextArea#setRowsJTextArea#setColumns...

你的代码...

在此处输入图像描述

我的代码...

在此处输入图像描述

JTextArea shortText = makeMultiLineLabel(true);
shortText.setBorder(BorderFactory.createEtchedBorder());
shortText.setFont(shortText.getFont().deriveFont(Font.BOLD));
//       FontMetrics fm = shortText.getFontMetrics(
//                shortText.getFont());
//        shortText.setPreferredSize(new Dimension(
//                Math.min(fm.stringWidth(shortMessage), 300),
//                fm.getHeight()));
shortText.setRows(2);
shortText.setColumns(20);
shortText.setText(shortMessage);

JTextArea messageText = makeMultiLineLabel(true);
messageText.setBorder(BorderFactory.createEtchedBorder());
messageText.setFont(shortText.getFont().deriveFont(Font.PLAIN));
//        fm = messageText.getFontMetrics(
//                messageText.getFont());
//        messageText.setPreferredSize(new Dimension(
//                Math.min(fm.stringWidth(message), 300),
//                fm.getHeight()));
messageText.setRows(4);
messageText.setColumns(20);
messageText.setText(message);

您可能还想看看 SwingXJXErrorDialog

于 2013-08-23T00:43:13.173 回答