我想要在某个位置有一个JTextArea
。我尝试了几种方法,例如使用不同的 LayoutManager、根本不使用 LayoutManager、setLayout(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;
}
}
这是我想要的样子:
这是目前的样子:
我做了一些教程,他们使用添加到 JPanel
中的 JTextField
,但我想知道是否可以只使用 JTextField
code> 或 JTextArea
获取更多文本,无需先将其添加到面板!
就像我说的,我正在查找“如何设置 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/