为什么油漆边框不被称为?

时间:2014-05-06 13:28:17

标签: java swing jcomponent

我正在学习如何创建自定义组件。我希望能够包含它调用的所有方法,并且能够更改边框方法。我的代码下面没有重绘paintBorder(...)方法

    public void paintBorder(Component t, Graphics g, int x, int y, int h, int w) {
         super.paintBorder(g);
         g.setColor(Color.YELLOW);
         g.fillOval(100, 100, 50, 50);
         System.out.println("PaintBorder");
    }

为什么不画画。 paintComponent(...)中的代码确实可以工作并绘制圆形,但是如果我想将边框设置为不同的东西,或者即使我只是想看到一条消息,请使用println(...)来到控制台

Paint Call:

    There are three methods called

          paintComponent()
          paintBorder()
          paintChildren()

如何调用paintBorder()?我想,如果我在另一个类中创建一个这样的实例,那么我应该能够调用repaint()调用update实际调用一个调用paint的调用,调用上面列出的三个方法(paintComponent,paintBorder,paintChildren)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

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

    public testBall() {
           JPanel testPane = new JPanel();
           testPane.setBackground(Color.green);
           testPane.setLayout(new BorderLayout());
           testPane.add(new Ball(30,30,10));
           JFrame frame = new JFrame("Testing");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setLayout(new BorderLayout());
           frame.add(testPane);
           frame.pack();
           frame.setSize(300, 200); 
           frame.setLocationRelativeTo(null);
           frame.setVisible(true);
    }
}

class MyBall extends JComponent{
    private static final long serialVersionUID = 1L;
    public static Color colorBall = Color.red;

    public MyBall() { 
        super();
        System.out.println("MyBall (0)");
    }

    public MyBall(int x, int y, int diameter){
        super();
        this.setLocation(x, y);
        this.setSize(diameter, diameter);
        System.out.println("MyBall (1)");
    }

    public void paintBorder(Graphics g) {
         super.paintBorder(g);
         g.setColor(Color.YELLOW);
         g.fillOval(100, 100, 50, 50);
         System.out.println("PaintBorder");
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(colorBall);
        g.fillOval(0, 0, 50, 50);
        System.out.println("paintComponent");
    }

    public void paint(Graphics g) {
        super.paint(g);
        paintComponent(g);
        paintBorder(this, g,10,10,10,10);
        paintChildren(g);
        System.out.println("Paint");
    }
}

工作代码:我正在创建一个不同的球类实例,这是我没有看到的。如果一个人要在类中编写paintBorder(...)方法,那么扩展JComponent(...)应该如何绘制边框?有没有人为这样的任务有任何良好的联系?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

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

    public testBall() 
    {
           JPanel testPane = new JPanel();
           testPane.setBackground(Color.green);
           testPane.setLayout(new BorderLayout());
           testPane.add(new MyBall(30,30,10));



           JFrame frame = new JFrame("Testing");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setLayout(new BorderLayout());
           frame.add(testPane);
           frame.pack();
           frame.setSize(300, 200); 
           frame.setLocationRelativeTo(null);
           frame.setVisible(true);
    }
}

class MyBall extends JComponent
{
    private static final long serialVersionUID = 1L;
    private Color colorBall = Color.red;

    public void setColorBall(Color c)
    {
        this.colorBall = c;
    }

    public MyBall() 
    { 
        super();
        System.out.println("MyBall (0)");
    }

    public MyBall(int x, int y, int diameter)
    {
        super();
        this.setLocation(x, y);
        this.setSize(diameter, diameter);
        System.out.println("MyBall (1)");
    }

    public void paintBorder(Graphics g) 
    {
         super.paintBorder(g);
         g.setColor(Color.YELLOW);
         g.fillOval(100, 100, 50, 50);
         System.out.println("PaintBorder");
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(colorBall);
        g.fillOval(0, 0, 50, 50);
        System.out.println("paintComponent");
    }

    public void paint(Graphics g) 
    {
        super.paint(g);
        paintComponent(g);
        paintBorder(g);
        paintChildren(g);
        System.out.println("Paint");
    }
}

1 个答案:

答案 0 :(得分:2)

JComponent

中没有这样的方法
public void paintBorder(Component t, Graphics g, int x, int y, int h, int w)

它是border的方法,因此它永远不会在JComponent中调用。覆盖

protected void paintBorder(Graphics g)

而是在那里添加你的代码。