AWT Label/Button/Textfield + 画图?

AWT Label/Button/Textfield + paint?

如何在标签/按钮下方绘制? -JAVA

我想写一个ax^2 + bx + c的坐标计算程序,但这不是重点...

我只想通过 AWT 和 paint 方法绘制图形。 (尚未实施),但我无法在前台设置 paint 方法。你能帮我解决这个问题,让油漆至少可见吗?

我附上了我的想法大纲。 抱歉我的英语不好

idea

import java.awt.Button;
import java.awt.Canvas;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;

public class MyWindow extends Frame {
    Frame frame;
    Label l1;
    Label l2;
    Label l3;
    Button b1;
    TextField t1;
    TextField t2;
    TextField t3;
    Panel panel;
    Canvas canvas;

    MyWindow() {
        frame = new Frame("GraphPlotter");
        frame.setSize(800, 800);
        frame.setLayout(new FlowLayout());

        l1 = new Label("f(x)= ");
        l2 = new Label(" x^2 + ");
        l3 = new Label(" x + ");

        t1 = new TextField("1");
        t2 = new TextField("2");
        t3 = new TextField("3");

        b1 = new Button("Plot");


        frame.add(l1);
        frame.add(t1);
        frame.add(l2);
        frame.add(t2);
        frame.add(l3);
        frame.add(t3);
        frame.add(b1);
        frame.setVisible(true);


    }

    public void paint(Graphics g) {
        g.fillRect(10, 10, 300, 300); // ist im "Hintergrund"??
    }
}

`   public class Start {

    public static void main(String[] args) {
        MyWindow window = new MyWindow();   
    }

}

`

您之前没有设置过颜色

g.fillRect(10, 10, 300, 300);

我解决了这个问题大约两个月,但我现在有点动力,所以我要自己回答我的问题。信息:repaint()调用paint,所以你不必自己实现这个方法。

在任务中我忘了:

  • -生成一个canvas
  • -使用布局管理器
  • -使用 repaint() 方法调用绘画;或更新();

问题只是怎么画,看完下面的代码应该就清楚了:

package delete;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;

public class AwtFrame extends Frame {
    AwtCanvas c;
    Panel p1;
    Label la_ax2, la_bx, la_c;
    TextField tf_Ax2, tf_bx, tf_c;

    public AwtFrame() {
        super("paint example.");
        this.setSize(800, 800);

        initComp();
        addComp();

        // TODO: write a graph drawing method
        c.repaint(); // paints a line of your graph

        this.setVisible(true);
    }

    public void initComp() {
        p1 = new Panel();
        la_ax2 = new Label("x^2 ");
        la_bx = new Label("+ x ");
        la_c = new Label("+ c ");

        tf_Ax2 = new TextField(0);
        tf_bx = new TextField(0);
        tf_c = new TextField(0);
        c = new AwtCanvas();
    }

    public void addComp() {
        this.setLayout(new BorderLayout());
        p1.setLayout(new FlowLayout());

        this.add(p1, BorderLayout.NORTH);
        this.add(c, BorderLayout.CENTER);

        p1.add(tf_Ax2);
        p1.add(la_ax2);
        p1.add(tf_bx);
        p1.add(la_bx);
        p1.add(tf_c);
        p1.add(la_c);
    }

    public static void main(String[] args) {
        new AwtFrame();
    }
}



package delete;

import java.awt.Canvas;
import java.awt.Graphics;
import java.util.LinkedList;

public class AwtCanvas extends Canvas {
//  LinkedList<ToImplement> coords = new LinkedList<ToImplement>();


    // TODO: implement plotter
    public void paint(Graphics g) {
        g.drawLine(40, 40, 100, 100);
        g.drawLine(54, 22, 300, 200);
    }

}