gpt4 book ai didi

java - JTextArea 和 GridBagLayout : how to avoid changing column width as we write some text?

转载 作者:行者123 更新时间:2023-11-30 11:03:43 27 4
gpt4 key购买 nike

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

  • 包含文本编辑器的左栏(我选择了 JTextArea);
  • 包含单个按钮 (JButton) 的中心列;
  • 包含另一个文本编辑器的右栏。

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

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

这是我的代码:

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);

关于java - JTextArea 和 GridBagLayout : how to avoid changing column width as we write some text?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30226870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com