【问题标题】:JTextArea in GridBagLayout wrapping too soonGridBagLayout 中的 JTextArea 包装太快了
【发布时间】:2019-04-17 17:40:43
【问题描述】:

名为txtaObservationJTextArea 占用了其容器GridBagLayout 的五个水平槽。它的文本应该在到达第五个插槽时换行,在名为lblObservationLimit 的标签下方,但是它的换行太快了,就在第一个插槽中。如何让它包裹在正确的插槽?

JDViewCustomer.java

package test2;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Objects;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class JDViewCustomer extends javax.swing.JDialog {

    private static final long serialVersionUID = 1L;

    private JTextArea txtaObservation;
    private JPanel panelCustomerData;

    private final CustomerData customerData;
    private JLabel lblObservationLimit;


    public JDViewCustomer(java.awt.Frame parent, boolean modal, CustomerData customerData) {
        super(parent, modal);
        this.customerData = Objects.requireNonNull(customerData);

        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        layoutCustomerDataSection();
        addCustomerData();

        initComponents();
    }

    public void layoutCustomerDataSection() {
        panelCustomerData = new JPanel();
        panelCustomerData.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 1, true), new EmptyBorder(10, 10, 10, 10)));
        panelCustomerData.setBackground(Color.WHITE);

        getContentPane().add(panelCustomerData);

        GridBagLayout gbl_panelCustomerData = new GridBagLayout();
        gbl_panelCustomerData.columnWidths = new int[]{58, 199, 38, 102, 27, 138, 0};
        gbl_panelCustomerData.rowHeights = new int[]{0, 14, 0};
        gbl_panelCustomerData.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        gbl_panelCustomerData.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        panelCustomerData.setLayout(gbl_panelCustomerData);

        lblObservationLimit = new JLabel("Observation limit");
        GridBagConstraints gbc_lblObservationLimit = new GridBagConstraints();
        gbc_lblObservationLimit.insets = new Insets(0, 0, 5, 0);
        gbc_lblObservationLimit.gridx = 5;
        gbc_lblObservationLimit.gridy = 0;
        panelCustomerData.add(lblObservationLimit, gbc_lblObservationLimit);

        txtaObservation = new JTextArea("Observation text");
        txtaObservation.setFont(new Font("Tahoma", Font.PLAIN, 11));
        txtaObservation.setLineWrap(true);
        txtaObservation.setWrapStyleWord(true);
        GridBagConstraints gbc_txtaObservation = new GridBagConstraints();
        gbc_txtaObservation.gridwidth = 5;
        gbc_txtaObservation.anchor = GridBagConstraints.NORTHWEST;
        gbc_txtaObservation.gridx = 1;
        gbc_txtaObservation.gridy = 1;
        panelCustomerData.add(txtaObservation, gbc_txtaObservation);
    }

    public void addCustomerData() {
        txtaObservation.setText(customerData.getObservation());
    }

    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("View customer");

        pack();
        setLocationRelativeTo(getParent());
    }
}

CustomerData.java

package test2;

public class CustomerData {

    private final String observation;

    public CustomerData(String observation) {
        this.observation = observation;
    }

    public String getObservation() {
        return observation;
    }
}

Test2.java

package test2;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Test2 {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        CustomerData customerData = new CustomerData("Testing a long observation text that should wrap only when reaching the observation limit.");
        new JDViewCustomer(frame, true, customerData).setVisible(true);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

【问题讨论】:

  • tente com gbc_txtaObservation.fill = GridBagConstraints.HORIZONTAL; (e talvez ...gridx = 0) []s
  • @CarlosHeubeger 谢谢,但您的评论应该是英文的 :)
  • 好吧,就三个字...不会有太大的区别,只是想炫耀一点[:-)(别说我你没听懂)
  • @CarlosHeuberger 我明白了,.fill 工作了!把它变成一个答案,我很乐意接受它。

标签: java swing dialog jtextarea gridbaglayout


【解决方案1】:

对 JDViewCustomer.java 试试这个——cmets 用“// kwb”标记。直到现在我才意识到这是非官方的回答!

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Objects;
import javax.swing.BorderFactory;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class JDViewCustomer extends javax.swing.JDialog {

    private static final long serialVersionUID = 1L;

    private JTextArea txtaObservation;
    private JPanel panelCustomerData;

    private final CustomerData customerData;
    private JLabel lblObservationLimit;


    public JDViewCustomer(java.awt.Frame parent, boolean modal, CustomerData customerData) {
        super(parent, modal);
        this.customerData = Objects.requireNonNull(customerData);

        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        layoutCustomerDataSection();
        addCustomerData();

        initComponents();
    }

    public void layoutCustomerDataSection() {
        panelCustomerData = new JPanel();
        panelCustomerData.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 1, true), new EmptyBorder(10, 10, 10, 10)));
        panelCustomerData.setBackground(Color.WHITE);

        getContentPane().add(panelCustomerData);

        GridBagLayout gbl_panelCustomerData = new GridBagLayout();
        gbl_panelCustomerData.columnWidths = new int[]{58, 199, 38, 102, 27, 138, 0};
        gbl_panelCustomerData.rowHeights = new int[]{0, 14, 0};
        gbl_panelCustomerData.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        gbl_panelCustomerData.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        panelCustomerData.setLayout(gbl_panelCustomerData);

        lblObservationLimit = new JLabel("Observation limit");
        GridBagConstraints gbc_lblObservationLimit = new GridBagConstraints();
        gbc_lblObservationLimit.insets = new Insets(0, 0, 5, 0);
        gbc_lblObservationLimit.gridx = 5;
        gbc_lblObservationLimit.gridy = 0;
// kwb set gridheight to total number of rows
        gbc_lblObservationLimit.gridheight = 2;
        panelCustomerData.add(lblObservationLimit, gbc_lblObservationLimit);

        txtaObservation = new JTextArea("Observation text");
// kwb just to help view issue
        txtaObservation.setBorder(BorderFactory.createLineBorder(Color.blue));
        txtaObservation.setFont(new Font("Tahoma", Font.PLAIN, 11));
        txtaObservation.setLineWrap(true);
        txtaObservation.setWrapStyleWord(true);
        GridBagConstraints gbc_txtaObservation = new GridBagConstraints();
        gbc_txtaObservation.gridwidth = 5;
        gbc_txtaObservation.anchor = GridBagConstraints.NORTHWEST;
// kwb fill all available horizontal space
        gbc_txtaObservation.fill = GridBagConstraints.HORIZONTAL;
// kwb change to start in column 0, adjust as necessary
        gbc_txtaObservation.gridx = 0;
        gbc_txtaObservation.gridy = 1;
        panelCustomerData.add(txtaObservation, gbc_txtaObservation);
    }

    public void addCustomerData() {
        txtaObservation.setText(customerData.getObservation());
    }

    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("View customer");

        pack();
        setLocationRelativeTo(getParent());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-29
    • 2013-08-29
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2016-03-21
    相关资源
    最近更新 更多