Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JFrame中的绘制不工作(Java)_Java_Swing_Graphics_Jframe_Paint - Fatal编程技术网

JFrame中的绘制不工作(Java)

JFrame中的绘制不工作(Java),java,swing,graphics,jframe,paint,Java,Swing,Graphics,Jframe,Paint,所以在课堂上我们正在制作一个Java程序。我们正在尝试在JFrame中使用paint(Graphics g)函数。我们在过去(几周前)已经尝试过了,它过去也很管用。但现在它没有(或者我们在某处犯了一个错误)。我们也尝试过使用paintComponent(图形g),但似乎没有任何效果。 这是我们的代码: public class MainAc { public static void main(String[] args) { JFrame frame = new JFra

所以在课堂上我们正在制作一个Java程序。我们正在尝试在JFrame中使用paint(Graphics g)函数。我们在过去(几周前)已经尝试过了,它过去也很管用。但现在它没有(或者我们在某处犯了一个错误)。我们也尝试过使用paintComponent(图形g),但似乎没有任何效果。 这是我们的代码:

public class MainAc {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Class Paint");       
        JButton button = new JButton("Click for more");             
        frame.setSize(800, 600);    
        frame.add(button);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.setLayout(null);
        button.setLocation(100,100);
        button.setSize(200,100);
        frame.setVisible(true);     
    }
    public void paint(Graphics g){
        g.drawString("Hello", 200, 50);
    }
}

因此,您要做的是在自己的
MainAc
类中实现
paint
方法,而不是
JFrame

您的
MainAc
类本身应该派生自
JFrame

下面的代码应该可以工作。如果您不理解继承,您应该查看您的课堂笔记,它会在那里

public class MainAc extends JFrame {

    public static void main(String[] args) {
        new MainAc();
    }
    
    public MainAc() {
        super("Class Paint");       
        JButton button = new JButton("Click for more");             
        setSize(800, 600);    
        add(button);
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.setLayout(null);
        button.setLocation(100,100);
        button.setSize(200,100);
        setVisible(true);
    }
    
    public void paint(Graphics g) {
        super.paint(g);
        g.drawString("Hello", 200, 50);
    }
    
}

正如代码所示,您只是在类中定义了一个
paint
方法。该类扩展了
对象
,因此您不会在
JComponent
上覆盖绘制


类必须扩展
Component
Component
本身的任何子类,以便实际重写
paint
paintComponent
。在您的情况下,
MainAc
将扩展
JFrame

您的类不会从任何能够绘制的
组件扩展

通读更多的想法

你可以这样做:

public class SimplePaint {

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

    public SimplePaint() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new PaintPane());
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected class PaintPane extends JPanel {

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();
            String text = "Look ma, no hands";
            FontMetrics fm = g2d.getFontMetrics();
            int x = (getWidth() - fm.stringWidth(text)) / 2;
            int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
            g2d.drawString(text, x, y);
            g2d.dispose();


        }

    }

}

如果可能,您应该避免重写顶级容器的
paint
方法,如果没有其他原因,它们不是双缓冲的


出于同样的原因,您还应该尝试扩展基于Swing的组件,因为混合使用重组件和轻组件可能会导致绘制问题。

下面是Sticky代码的简化版本,似乎可以工作。我将其更改为extend JApplet,并使用
init()
方法代替
main()

public class JavaApplication51 extends JApplet {
    public  void something() {
       JButton button = new JButton("Click for more");             
        add(button);
        setLayout(null);
        button.setLayout(null);
        button.setLocation(100,100);
        button.setSize(101,20);
        setVisible(true); 
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.drawString("Hello", 200, 50);
    }

    public void init() {
        something();
    }
}

建议OP从顶级容器扩展,但未能调用super.paint,这并不是对OP有利,是一种非常糟糕的方法-ihmofix。显然有更好的方法来做到这一点。但OP似乎并不特别先进,这一事实使得“正确”的方式变得不那么重要。这似乎不起作用。尝试移除按钮以查看按钮是否重叠,但这只会导致出现一个奇怪的窗口。“但OP似乎并不特别先进”我本以为这是最好的理由,尽可能以最好的方式(IMHO)尝试并完成OP,因为这是公平的观点,当然,如果您愿意,您可以自由添加更深入的答案。“必须扩展JComponet”。。。一定要吗?JPanel有什么问题?请阅读整个句子“或它的一个子类”。重点是,沟通的意义是由接收者定义的。如果这就是我从中挑选出来的,想象一下OP会从中挑选出什么。这有点挑剔(对不起),但我鼓励你稍微重复一下。从技术上讲,您也可以从组件扩展;)干杯,我知道我很痛苦,但是如果我们不能把彼此保持在一个高标准,海报还有什么希望呢你已经从我这里获得了+1