简体   繁体   English

AWT标签/按钮/文本框+油漆?

[英]AWT Label/Button/Textfield + paint?

how to draw below a label / button? 如何在标签/按钮下方绘制? -JAVA -Java

I wanted to write a coordinate calculation program for ax^2 + bx + c, but thats not the important point... 我想为ax ^ 2 + bx + c编写一个坐标计算程序,但这并不是重点。

I'd like to draw the graph only via AWT and the paint method. 我只想通过AWT和paint方法绘制图形。 (not implemented yet), but I'm not able to set the paint method in the foreground. (尚未实现),但是我无法在前台设置paint方法。 Can you please help me to fix the problem so that the paint is at least visible? 您能帮我解决问题,使油漆至少可见吗?

I have attached a outline of my thoughts. 我附上了我的想法纲要。 Sorry for my bad english 对不起,我的英语不好

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);

I solved that problem like two months or so, but I'm a bit motivated at the moment, so I am going to answer my question on my own. 我大约两个月左右就解决了这个问题,但此刻我有点动力,所以我将自己回答问题。 Info: repaint() calls paint, so you don't have to implement this method on your own. 信息:repaint()调用paint,因此您不必自己实现此方法。

in the task I forgot to: 在任务中,我忘记了:

  • -generate a canvas -生成画布
  • -use layout manager 使用布局管理器
  • -call paint with the methods repaint(); 用方法repaint()调用paint; or update(); 或update();

The question was only how to paint and this should be clear after reading following code: 问题仅在于如何绘制,在阅读以下代码后应该很清楚:

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);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM