我想要一个JTextArea
在某个位置上。我尝试了几件事,比如使用不同的LayoutManager
s, no LayoutManager
无论如何,setLayout(null)
等等。无论我做什么,似乎setBounds()
, setLocation()
and setSize()
不在这里工作,但我读到它并且它说它应该工作。那么我做错了什么?
The 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;
}
}
Here is what I want it to look like:
And here is what it currently looks like:
我做了一些他们使用的教程JTextField
被添加到JPanel
s,但我想知道我是否可以使用JTextField
or JTextArea
无需先将其添加到面板即可获取更多文本!
就像我说的,我正在查找“如何设置JTextArea
位置”,它说使用setBounds()
。显然这是不正确的..所以我再次想知道的是如何做得更好。另外:我确实读了很多关于LayoutManager
s,但对我来说尝试使用它比仅仅阅读它更有帮助......
我尝试了行和列,但它并没有改变这样的事实:JTextArea
位置不正确。
我所做的是(在 Create TextArea 方法中):
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;
}