动态添加JTextArea到Jpanel无法正常工作

时间:2014-11-02 23:42:34

标签: java swing jtextarea

因此,点击JMenu按钮,我想动态地将JTextArea添加到我的JPanel。由于某种原因,它没有出现在预期的时候。我正在使用此代码添加它:

drag = new DragListener();
JTextArea textArea = new JTextArea("Some text\nSome other text");
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
add(textArea);
textArea.addMouseListener(drag);
textArea.addMouseMotionListener(drag);

我知道当我在初始化JPanel类时执行此代码时,此代码有效:

public MyPanel() {
    drag = new DragListener();
    JTextArea textArea = new JTextArea("Some text\nSome other text");
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    add(textArea);
    textArea.addMouseListener(drag);
    textArea.addMouseMotionListener(drag);
}

但是当我使用另一种方法动态添加它时,它不会添加。这是为什么

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.event.MouseInputAdapter;

public class Editor {

    public static void main(String[] args) {
        JFrame frame = new Window();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(30, 30, 1000, 700);
        frame.getContentPane().setBackground(Color.white);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class Window extends JFrame {
    MyPanel myPanel = new MyPanel();

    private static final long serialVersionUID = 1L;

    public Window() {
        addMenus();
    }

    public void addMenus() {

        getContentPane().add(myPanel);

        JMenuBar menubar = new JMenuBar();

        JMenuItem addBox = new JMenuItem("Add Menu");
        addBox.setMnemonic(KeyEvent.VK_E);
        addBox.setToolTipText("Exit application");
        addBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                myPanel.addBox();
            }
        });

        menubar.add(addBox);
        setJMenuBar(menubar);

        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

class MyPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    DragListener drag;

    public MyPanel() {
        drag = new DragListener();
        drag = new DragListener();
        JTextArea textArea = new JTextArea("Some text\nSome other text");
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        add(textArea);
        textArea.addMouseListener(drag);
        textArea.addMouseMotionListener(drag);
    }

    public void addBox() {
        drag = new DragListener();
        JTextArea textArea2 = new JTextArea("Some text\nSome other text");
        textArea2.setLineWrap(true);
        textArea2.setWrapStyleWord(true);
        add(textArea2);
        textArea2.addMouseListener(drag);
        textArea2.addMouseMotionListener(drag);
        repaint();
        revalidate();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    }

    public class DragListener extends MouseInputAdapter {
        Point location;
        MouseEvent pressed;

        public void mousePressed(MouseEvent me) {
            pressed = me;
        }

        public void mouseDragged(MouseEvent me) {
            Component component = me.getComponent();
            location = component.getLocation(location);
            int x = location.x - pressed.getX() + me.getX();
            int y = location.y - pressed.getY() + me.getY();
            component.setLocation(x, y);
        }
    }

}

更新

现在我已经解决了这个问题,我遇到了另一个问题。

当我向Panel添加一个新的JTextArea时,它将重置面板上所有其他JTextArea的位置。想法?

enter image description here

1 个答案:

答案 0 :(得分:3)

添加...

revalidate();
repaint();

addBox方法的末尾,这将鼓励布局管理器更新容器重绘管理器以重新绘制组件

此外,请不要以这种方式使用JMenuBarJMenuItem,这对用户来说非常直观,而是使用JToolBarJButton

有关详细信息,请参阅How to Use Tool Bars

以这种方式拖动JTextArea也很烦人。用户如何突出显示文本?

查看Dragging-and-dropping to move a JTextArea around on JPanel了解其他想法