JTextArea的位置,setBounds不起作用? [英] JTextArea position, setBounds does not work?

查看:129
本文介绍了JTextArea的位置,setBounds不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want a JTextArea in a certain position. I tried several things like using different LayoutManagers, no LayoutManager at all, setLayout(null) etc. Whatever I do, it seems like setBounds(), setLocation() and setSize() aren't working here, but I read about it and it said it should work. So what am I doing wrong?

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

The JTextArea is always too high and the position doesn't change if I change the Parameters in 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 s中的 JTextField s,但是我想知道我是否可以只使用 JTextField JTextArea 以获得更多文本,而无需先将其添加到面板中!

I did a few tutorials where they used JTextFields that were added to JPanels, but I was wondering if I could just use a JTextField or JTextArea for more text WITHOUT adding it to a Panel first!

就像我说的那样,我正在查找如何设置 JTextArea 位置",并且说它使用了 setBounds().显然这是不正确的..因此,我想知道的是如何做得更好.另外:我确实阅读了很多有关 LayoutManager s的内容,但是对我来说,尝试使用它比仅仅阅读它更有帮助...

Like I said, I was looking up "how to set JTextArea position" and it said to use setBounds(). Apparently that's not correct.. So again, all I want to know is how to do it better. Also: I did read a lot about the LayoutManagers, but for me trying to use it is more helpful than just reading about it...

我尝试使用行和列,但是并没有改变 JTextArea 不在正确位置这一事实.

I tried that with rows and columns, but it didn't change the fact that the JTextArea was not in the right position.

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

What I did was (in the CreateTextArea method):

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方法中添加组件.

You could achieve your center-nested look by doing just that, using GridBagLayout to center nest your components. You could achieve the width of some of the framing JPanels by using an EmptyBorder. You should never add components from within a paintComponent method.

例如:

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(...).

这篇关于JTextArea的位置,setBounds不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆