动态地将JTextArea添加到Jpanel无法正常工作 [英] Dynamically Adding a JTextArea to Jpanel not working
问题描述
因此,在单击JMenu按钮时,我想将JTextArea动态添加到我的JPanel中.由于某种原因,它没有出现在预期的时间.我正在使用此代码添加它:
So, on the click of a JMenu button, I want to dynamically add a JTextArea to my JPanel. For some reason, it isn't showing up when it is expected to. I am using this code to add it:
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类时,此代码有效,就像这样:
And I know this code works when I do it when the JPanel class is initialized, like this:
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);
}
但是当我使用另一种方法动态添加它时,它没有添加.为什么会这样
But when I add it dynamically using another method, it isn't adding. Why is this
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);
}
}
}
更新
现在我已经解决了这个问题,我遇到了另一个问题.
Now that I have solved this problem, I have ran into another.
当我向面板添加新的JTextArea时,它将重置面板上所有其他JTextArea的位置.想法?
When I add a new JTextArea to the Panel, it is resetting all of the other JTextArea's positions on the panel. Ideas?
推荐答案
添加...
revalidate();
repaint();
addBox
方法的末尾,这将鼓励布局管理器更新容器,重新绘制管理器以重新绘制组件
to the end of your addBox
method, this will encourage the layout manager to update the container the repaint manager to repaint the component
另外,请不要这样使用JMenuBar
和JMenuItem
,这对用户而言是直观的,而是使用JToolBar
和JButton
Also, please don't use JMenuBar
and JMenuItem
s this way, it is counter intuitive to users, instead, use a JToolBar
and JButton
有关更多详细信息,请参见如何使用工具栏
See How to Use Tool Bars for more details
以这种方式拖动JTextArea
也是...烦人的.用户现在如何突出显示文本?
Dragging the JTextArea
in this manner is also...annoying. How does the user highlight the text now?
看看拖动拖放以在JPanel上移动JTextArea 以获得另一个想法
这篇关于动态地将JTextArea添加到Jpanel无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!