gpt4 book ai didi

java - JTextArea位置,setBounds不起作用?

转载 作者:行者123 更新时间:2023-11-30 09:01:33 26 4
gpt4 key购买 nike

我想要一个位于特定位置的 JTextArea。我尝试了几种方法,比如使用不同的 LayoutManager,根本没有 LayoutManagersetLayout(null) 等。无论我做什么,它看起来都是 setBounds()setLocation()setSize() 在这里不起作用,但我读到它并说它应该起作用。那我做错了什么?

JTextArea 总是太高,如果我更改 setBounds() 中的参数,位置也不会改变。

public class textarea extends JPanel {

public static void main(String[] args){
JFrame frame = new JFrame("text area");
textarea content = new textarea();
frame.setContentPane(content);
frame.setLocation(120,70);
frame.pack();
frame.setVisible(true);
frame.setSize(700,500);
}

JPanel PanelForText;

public textarea(){
setBackground(Color.LIGHT_GRAY);
setLayout(new FlowLayout(FlowLayout.CENTER,50,50));

txtArea txt = new txtArea();

PanelForText = new JPanel();
PanelForText.setPreferredSize(new Dimension(500,300));
PanelForText.setBorder(BorderFactory.createEtchedBorder());
PanelForText.add(txt);

add(PanelForText);
}
}


public class txtArea extends JPanel {

boolean textAreaCreated = false;

public txtArea(){
setBackground(Color.WHITE);
setPreferredSize(new Dimension(496, 290));
}

public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.GRAY);
g.fillRect(50, 25, 400, 245);
if (!textAreaCreated)
createTextArea();
}

public void createTextArea() {
JTextArea Text = new JTextArea();
Text.setBounds(500,300,300,300);
Text.setOpaque(false);
Text.setWrapStyleWord(true);
Text.setLineWrap(true);
Text.setBorder(BorderFactory.createLineBorder(Color.RED));
add(Text);
textAreaCreated = true;
}
}

这是我想要的样子: enter image description here

这是它目前的样子: enter image description here

我做了一些教程,其中他们使用了添加到 JPanelJTextField,但我想知道我是否可以只使用 JTextFieldJTextArea 获取更多文本,而无需先将其添加到面板!

就像我说的,我在查找“如何设置 JTextArea 位置”,它说要使用 setBounds()。显然那是不正确的。所以,我只想知道如何做得更好。另外:我确实阅读了很多关于 LayoutManager 的内容,但对我来说尝试使用它比仅仅阅读它更有帮助......

我对行和列进行了尝试,但这并没有改变 JTextArea 不在正确位置的事实。

我所做的是(在 CreateTextArea 方法中):

public void createTextArea() {  
JTextArea Text = new JTextArea(5,1);
Text.setOpaque(false);
Text.setWrapStyleWord(true);
Text.setLineWrap(true);
Text.setBorder(BorderFactory.createLineBorder(Color.RED));
add(Text);
textAreaCreated = true;
}

最佳答案

您可以通过使用 GridBagLayout 居中嵌套您的组件来实现居中嵌套的外观。您可以使用 EmptyBorder 实现某些框架 JPanel 的宽度。您应该永远不要从 paintComponent 方法中添加组件。

例如:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;

@SuppressWarnings("serial")
public class TestTextArea2 extends JPanel {

private static final Color BG = Color.LIGHT_GRAY;
private static final int ROWS = 14;
private static final int COLS = 34;
private JTextArea textArea = new JTextArea(ROWS, COLS);

public TestTextArea2(int heightGap, int sideGap) {
setBorder(BorderFactory.createEmptyBorder(heightGap, sideGap, heightGap, sideGap));

textArea.setBackground(Color.LIGHT_GRAY);
JScrollPane scrollPane = new JScrollPane(textArea);

JPanel txtAreaPanel = new JPanel();
int ebGap = 40;
txtAreaPanel.setBackground(Color.white);
txtAreaPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
txtAreaPanel.setLayout(new GridBagLayout());
txtAreaPanel.add(scrollPane);

JPanel myPanel2 = new JPanel();
Border outerBorder = BorderFactory.createEtchedBorder();
int heightGap2 = 5;
int sideGap2 = 5;
Border innerBorder = BorderFactory.createEmptyBorder(heightGap2, sideGap2, heightGap2, sideGap2);
myPanel2.setBorder(BorderFactory.createCompoundBorder(outerBorder, innerBorder));

myPanel2.setLayout(new GridBagLayout());
myPanel2.add(txtAreaPanel);

setBackground(BG);
setLayout(new GridBagLayout());
add(myPanel2);
}


private static void createAndShowGui() {
JFrame frame = new JFrame("TestTextArea2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TestTextArea2(100, 100));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

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

请注意,不是 setSize(...)setPreferredSize(...)

关于java - JTextArea位置,setBounds不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26360314/

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