【问题标题】:TextArea with Canvas Binding to RectTextArea 与 Canvas 绑定到 Rect
【发布时间】:2015-02-28 17:16:15
【问题描述】:

我有这个文本区域在我运行时没有显示的问题。有没有办法让它显示出来。顺便说一句,它是通过扩展画布的类上的游戏循环调用的。

public void render(Graphics g){
    Graphics2D g2d = (Graphics2D) g;
    if(!initialized)
        init();
    try {
        test.requestFocus();
        test.paintAll(g);
        test.setText("hi");
        test.setBounds(getBounds());
        test.printAll(g);
    } catch (Exception e) {
        e.printStackTrace();
    }
    g2d.draw(getBounds());
    g.drawRect(0, 0, 100, 100);


}
private void init(){
    frame.setVisible(false);
    initialized = true;
    test = new TextArea();
    test.setEditable(true);
    test.setBounds(getBounds());
    test.setBackground(test.getBackground());
    test.setForeground(test.getForeground());

    frame.add(test);
    frame.repaint();
    frame.setVisible(true);
    System.out.println(test.isVisible());
}
private Rectangle getBounds(){
    return new Rectangle(100, 100, 100, 100);
}

我尝试过使用 JTextArea,但它占据了全屏并且不会绑定到矩形。提前感谢您的帮助!

【问题讨论】:

  • 你想达到什么目的?要在另一个组件上显示 TextArea,您不需要手动绘制它;只需将其添加到画布旁边的父组件中即可。

标签: java canvas textarea jtextarea game-loop


【解决方案1】:

您无需手动绘制添加到程序中的现有Components。下面是一个简单示例,如何在您自己绘制的框架/画布上显示 TextArea:查看 cmets 了解更多详细信息。

package main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.Timer;

public class Test extends JFrame {

    private static Test frame;
    private static double t;
    private static int x;
    private static int y;
    private static TextArea test;

    public static void main(String[] args) {
        frame = new Test();
        frame.setVisible(true);
        // set layout to null so that you can freely position your components
        // without them "filling up the whole screen"
        frame.setLayout(null);
        frame.setSize(500, 500);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
        // game loop
        new Timer(10, (e) -> {
            t += 10.0 / 1000;
            x = (int) (100 + 50 * Math.sin(t));
            y = (int) (100 + 50 * Math.cos(t));
            // calling repaint will cause Test.paint() to be called first,
            // then Test's children will be painted (= the TextArea)
            frame.repaint();
        }).start();

        // initialize the textarea only once
        test = new TextArea();
        test.setEditable(true);
        test.setBounds(new Rectangle(100, 100, 100, 100));
        test.setText("hi");
        frame.add(test);
    }

    @Override
    public void paint(Graphics g) {
        // put only painting logic in your paint/render.
        // don't set the bounds of components here, 
        // as this will trigger a repaint.
        g.setColor(Color.black);
        g.fillRect(0, 0, 400, 400);
        g.setColor(Color.yellow);
        g.fillOval(x, y, 20, 20);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    相关资源
    最近更新 更多