Java PaintComponent绘制2次

时间:2016-11-21 14:58:17

标签: java swing jpanel paintcomponent oval

我创建了

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

  public class Painter extends JPanel {

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.setColor(new Color(100,100,100));
        g.drawLine(0, 0, 200, 200);
        System.out.println("heir3");
        System.out.println(Main.Array[0]);
        if (Main.Array[0] == 1){
            g.drawOval(100, 100, Main.Radint, Main.Radint);
            System.out.println("heir8");

      } else {
          g.drawString("2", 150, 100);
      }
    }
  } 

这个类应该能够在Label上绘制一些东西。一开始,它只是绘制一条线和2.但不幸的是,我无法第二次打电话给这个类画椭圆形?

那我怎么能再次打电话给这个班级并更新或更新JPanel呢?

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Buttons implements ActionListener {

    public JButton Kreis = new JButton();
    public JTextField Text = new JTextField();
    Frame Fr = new Frame();

    public JButton Kreis(){
        Kreis.setText("Kreis");
        Kreis.addActionListener(this);
        return Kreis;
    }
    public JTextField TextField(){
        Text.setPreferredSize(new Dimension(100,20));
        return Text;
    }
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (Kreis == source){
            Main.Radius = Text.getText();
            Main.Radint = Integer.parseInt(Main.Radius);
            Main.Array[0] = 1;
            Main.Array[1] = Main.Radint;
            System.out.println("hier");
            JPanel paint = new Painter();
            Fr.changeFrame(paint);
            System.out.println("hier");

        }
}
}

这是@GojiraDeMonstah

0 个答案:

没有答案