我有一个带有两个文本区域和一个按钮的Java程序。我希望允许用户使用触摸笔或鼠标编写一个文本区域,当他/她单击该按钮时,绘制的内容应该发送到文本区域2号。
因此,在用户写作的textarea中,我给出了一个带有paintComponent方法的毛斯情感监听器。在运行应用程序时,我已经意识到文本区域方法getText()和setText()无法设置或获取绘制的内容。
我有办法完成上面的任务吗?我也尝试过JPanel,但是它没有setContent()方法。
我很感谢你的帮助。
这是我的第一个textarea,用户在这里用触笔写字。
public class Area1 extends JTextArea {
int pointcount = 0;
Point[] points = new Point[10000];
public Area1() {
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent event) {
if (pointcount < points.length) {
points[pointcount] = event.getPoint();
++pointcount;
repaint();
}
}
});
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < pointcount; i++) {
g.fillOval(points[i].x, points[i].y, 4, 4);
}
}
}这是我使用textarea2和按钮的整体应用程序。
public class Handwriting extends JFrame {
private JTextArea area2;
private JButton b1;
Area1 area1;
public Handwriting() {
Box box = Box.createHorizontalBox();
area1 = new Area1();
area1.setRows(30);
area1.setColumns(30);
area2 = new JTextArea(30, 30);
b1 = new JButton("Copy");
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == b1) {
area2.setText(area1.getText());
}
}
});
box.add(area1);
box.add(b1);
box.add(area2);
add(box);
}
public static void main(String[] args) {
Handwriting hand = new Handwriting();
hand.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hand.setSize(500, 500);
hand.setVisible(true);
}
}发布于 2014-10-26 15:33:48
一种可能的解决方案是将一个类作为另一个类的内部类,这样甚至可以访问私有实例字段。
https://stackoverflow.com/questions/26574414
复制相似问题