2

可能重复:
paintComponent () 永远不会在 JFrame 上执行

我正在使用以下代码来显示两个字符串,我直接在 jfame 上绘制它们,而不是将它们作为组件添加或添加到 jpanel。但是为什么我得到一个空白窗口而不是得到字符串。我错在哪里?

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;


public class SimpleAttributes extends JFrame{

SimpleAttributes()
{

    super("Simple Attributes");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 200);
    //setUndecorated(true);
    Container cp=this.getContentPane();
    cp.setBackground(new Color(0,200,0,0));

    setVisible(true);
}

public void paintComponent(Graphics g)
{
    Graphics2D g2=(Graphics2D)g.create();
    g2.setColor(Color.RED);
    g2.drawString("One", 10, 10);

    g.drawString("Two", 10,40);

}
public static void main(String[] args) {
    // TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable(){public void run(){new SimpleAttributes();}});
}

}
4

2 回答 2

4

JFrame is not a component, therefor there's no paintComponent() function for it. See the API documentation.

于 2012-08-08T16:56:51.053 回答
2

如上所述是不正确的,没有这样的方法,(我打字速度很快)并考虑了JPanels。

您可以做的是创建自己的方法Container并覆盖该paint()方法,然后将其用作您ContentPane的方法frame.setContentPane(Container con)

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SimpleAttributes extends JFrame {

    SimpleAttributes() {

        super("Simple Attributes");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 200);
        //setUndecorated(true);
        setContentPane(new MyContainer());
        getContentPane().setBackground(new Color(0, 200, 0, 0));

        setVisible(true);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new SimpleAttributes();
            }
        });
    }
}

class MyContainer extends Container {

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setColor(Color.RED);
        g2.drawString("One", 10, 10);
        g.drawString("Two", 10, 40);
    }
}

如对一个答案的评论中所述,您可以使用 的paint()JFrame补偿对话框标题的偏移量:

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;


public class SimpleAttributes extends JFrame {

    SimpleAttributes() {

        super("Simple Attributes");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 200);
        //setUndecorated(true);
        getContentPane().setBackground(new Color(0, 200, 0, 0));

        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setColor(Color.RED);
    g2.drawString("One", 10, 10);//wont show
        g2.drawString("One", 50, 50);//will show
        g.drawString("Two", 40, 40);//will show
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new SimpleAttributes();
            }
        });
    }
}

but all of thats just going to give you more headaches why not just do it the preferred way? A JPanel and override paintComponent(Graphics g);

于 2012-08-08T16:53:36.610 回答