java - 为什么不调用绘制边框?

标签 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");
    }
}

最佳答案

JComponent中没有这个方法

public void paintBorder(Component t, Graphics g, int x, int y, int h, int w)

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

protected void paintBorder(Graphics g)

相反,在此处添加您的代码。

关于java - 为什么不调用绘制边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23496085/

相关文章:

java - 组件运行时绘图

java - 如何使用类路径通过 Liquibase 回滚数据库更改

java - Twitter4J 4.0.4 中无法识别 ListsResources.getUserListMemberships 方法

java - Matcher.appendReplacement 用文字文本

java - 如何移动相交的矩形?

java - 按下 JComponent 时触发 MouseListener,即使它是由 AffineTransform 转换的

java - 显示通用树的节点时,toString 将不会显示预期结果

Java 使用单独的类移动图像

java - 代码有时有效但有时无效(内存或线程问题)

java调整大小的方法