gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:54:49 25 4
gpt4 key购买 nike

我正在学习如何创建自定义组件。我希望能够包含它首先调用的所有方法,甚至能够更改边框方法。下面我的代码没有重新绘制 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com