JTextArea 和 GridBagLayout:如何避免在我们写一些文本时改变列宽?

JTextArea and GridBagLayout: how to avoid changing column width as we write some text?

我想设计一个 JFrame,其布局包含 3 列:

我设法使用 GridBagLayout class 做到了这一点。 但是,当我在一个编辑器中写一些文本时,编辑器的宽度变大而另一个编辑器的宽度变小。

可以帮我解决这个问题吗?

这是我的代码:

JFrame frame = new JFrame("My frame");
frame.setSize(1000, 1000);
frame.setLocation(500, 0;

Container pane = frame.getContentPane();
GridBagLayout layout = new GridBagLayout();
pane.setLayout(layout);

JTextArea textEditor = new JTextArea();

// LEFT TEXT EDITOR
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1;
constraints.weighty = 1;
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.WHITE);
leftPanel.add(textEditor);
layout.setConstraints(leftPanel, constraints);
pane.add(leftPanel);

// CENTER BUTTON
constraints = new GridBagConstraints();
constraints.gridx = 1;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.NONE;
JButton button = new JButton("Do");
layout.setConstraints(button, constraints);
pane.add(button);

// RIGHT TEXT EDITOR
constraints = new GridBagConstraints();
constraints.gridx = 2;
constraints.gridy = 0;
contraintes.fill = GridBagConstraints.BOTH;
contraintes.weightx = 1;
contraintes.weighty = 1;
JPanel rightPanel = new JPanel();
rightPanel.setBackground(Color.WHITE);
rightPanel.add(textEditor);
miseEnPage.setConstraints(rightPanel, constraints);
pane.add(rightPanel);

在您的代码中,我认为您应该使用 2 个 JTextAreas

并设置每个 JTextArea

setLineWrap(true)

喜欢

JFrame frame = new JFrame("My frame");
        frame.setSize(500, 500);
        frame.setLocation(500, 0);

        Container pane = frame.getContentPane();
        GridBagLayout layout = new GridBagLayout();
        pane.setLayout(layout);

        JTextArea textEditor = new JTextArea();//JTextArea 1
        textEditor.setLineWrap(true);//setLineWrap(true)
        JTextArea textEditor1 = new JTextArea();//JTextArea 2
        textEditor1.setLineWrap(true);//setLineWrap(true)
        // LEFT TEXT EDITOR
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weightx = 1;
        constraints.weighty = 1;
        JPanel leftPanel = new JPanel();
        leftPanel.setBackground(Color.WHITE);
        leftPanel.add(textEditor);
        layout.setConstraints(leftPanel, constraints);
        pane.add(leftPanel);

        // CENTER BUTTON
        constraints = new GridBagConstraints();
        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.fill = GridBagConstraints.NONE;
        JButton button = new JButton("Do");
        layout.setConstraints(button, constraints);
        pane.add(button);

        // RIGHT TEXT EDITOR
        constraints = new GridBagConstraints();
        constraints.gridx = 2;
        constraints.gridy = 0;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weightx = 1;
        constraints.weighty = 1;
        JPanel rightPanel = new JPanel();
        rightPanel.setBackground(Color.WHITE);
        rightPanel.add(textEditor1);
        layout.setConstraints(rightPanel, constraints);
        pane.add(rightPanel);