繁体   English   中英

没有调用绘制方法

[英]Paint method not being called

我试图简化一些基本代码,我遇到了一个小问题。 我的印象是paint方法是自动调用的,而且我基于我编写的所有其他基本程序。 我没有得到任何错误,只是代码不起作用,我也不能调用repaint()。 码:

public class Dynamic_Bg_Color{

JFrame frame;

public Dynamic_Bg_Color(){
    frame = new JFrame("BG Color Changer");

}

public void paint(Graphics g1){
    Graphics g = (Graphics)g1;

    g.setColor(Color.pink);
    g.fillRect(20,20,frame.getWidth()-20,100);
}

public static void main(String[] args){
    Dynamic_Bg_Color d = new Dynamic_Bg_Color();
    Dimension size = new Dimension(500,400);
    d.frame.setPreferredSize(new Dimension(size));
    d.frame.setMinimumSize(new Dimension(size));
    d.frame.setMaximumSize(new Dimension(size));
    d.frame.setLocationRelativeTo(null);

    d.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    d.frame.setLayout(new FlowLayout());
    d.frame.setVisible(true);
    d.frame.getContentPane().setBackground(Color.cyan);
}

}

如果paint方法是覆盖,则仅调用paint方法,并且该类扩展了paint方法具有意义的另一个类。 你的班级没有这样做,因此你的绘画方法毫无意义。

话虽如此,我建议你不要重载paint,而是在扩展JComponent或其中一个子节点的类中使用paintComponent(...) 最重要的是,阅读Swing教程的绘画。 请从这里开始。

另外,将Graphics对象转换为Graphics对象无需任何操作。 也许您错误地复制了代码并且意味着将其转换为Graphics2D类型?

我使类扩展了JComponent,并添加了覆盖,但没有任何改变。

  1. 我没有看到您将组件添加到框架的位置。
  2. 即使您确实将组件添加到框架中也不会绘制,因为组件的默认大小为(0,0),因此无需绘制任何内容。

您还需要重新构建代码。 JFrame变量不属于您进行自定义绘制的组件。 我建议你阅读自定义绘画的Swing教程中的部分。 它会告诉你:

  1. 如何进行自定义绘制,包括如何为组件指定首选大小
  2. 如何更好地构建程序,包括在Event Dispatch Thread上执行代码

通过最小的更改使工作代码:

import javax.swing.*;
import java.awt.*;

public class Dynamic_Bg_Color extends JPanel{

JFrame frame;

public Dynamic_Bg_Color(){
frame = new JFrame("BG Color Changer");
}

public void paint(Graphics g1){
Graphics g = (Graphics2D)g1;

g.setColor(Color.pink);
g.fillRect(20,20,frame.getWidth()-20,100);
}

public static void main(String[] args){
Dynamic_Bg_Color d = new Dynamic_Bg_Color();
Dimension size = new Dimension(500,400);
d.setPreferredSize(new Dimension(size));
d.setMinimumSize(new Dimension(size));
d.setMaximumSize(new Dimension(size));

d.frame.add(d);
d.frame.setLocationRelativeTo(null);
d.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d.frame.setLayout(new FlowLayout());
d.frame.pack();
d.frame.setVisible(true);
d.frame.getContentPane().setBackground(Color.cyan);
}

}

但是,正如其他人所说,我不建议你使用这个原因,这是非常非常糟糕的程序结构。 并阅读有关绘画的教程,即使我认为Oracles教程对此不好(frame.pack()只是将JFrame窗口的大小设置为保持它所包含的组件的大小)。

下面是最后的工作代码。 一些更改是扩展JPanel,设置JPanel的大小,以及将面板添加到JFrame。 是的,我知道每个人都说,这不是一种最佳方式,但是现在,它确实有效。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Dynamic_Bg_Color extends JPanel{

private static final long serialVersionUID = 1L;

static Dimension size = new Dimension(500,400);

static JFrame frame;

public Dynamic_Bg_Color(){

    setPreferredSize(size);
    setBackground(Color.cyan);
    addMouseListener(new Handler());

}

@Override
public void paintComponent(Graphics g){

    System.out.println("Click");

    super.paintComponent(g);

    g.setColor(Color.blue);
    g.fillRect(20,20,getWidth()-40,100);

    g.setColor(Color.green);
    g.fillRect(20,140,getWidth()-40,100);

    g.setColor(Color.orange);
    g.fillRect(20,260,getWidth()-40,100);
}

public static void main(String[] args){     


    Dynamic_Bg_Color d = new Dynamic_Bg_Color();

    frame = new JFrame("BG Color Changer");

    frame.setPreferredSize(new Dimension(size));
    frame.setMinimumSize(new Dimension(size));
    frame.setMaximumSize(new Dimension(size));
    frame.setLayout(new FlowLayout());
    frame.setLocationRelativeTo(null);
    frame.add(d);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setBackground(Color.cyan);
    frame.setVisible(true);

    }

public class Handler extends MouseAdapter{

    public void mousePressed(MouseEvent e) {

        int x = e.getX();
        int y = e.getY();

        if(x>= 20 && x<=getWidth()-40 && y>=20 && y<= 120){
            frame.getContentPane().setBackground(Color.blue);
            setBackground(Color.blue);
            frame.setTitle("Blue");
        }
        if(x>= 20 && x<=getWidth()-40 && y>=140 && y<= 240){
            frame.getContentPane().setBackground(Color.green);
            setBackground(Color.green);
            frame.setTitle("Green");    
            }
        if(x>= 20 && x<=getWidth()-40 && y>=260 && y<= 360){
            frame.getContentPane().setBackground(Color.orange);
            setBackground(Color.orange);
            frame.setTitle("Orange");
            }
    }



}

}

暂无
暂无

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

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