简体   繁体   English

框架上的Java Paint问题

[英]Issue with java paint on frame

I am creating an object of another class from my main class using the following call: 我正在使用以下调用从主类中创建另一个类的对象:

JSplash splash = new JSplash();

However, when I create this object it performs the constructor of the JSplash class and gives my window and my button. 但是,当我创建此对象时,它将执行JSplash类的构造函数,并提供我的窗口和按钮。 But it does not paint on the frame. 但是它不会在框架上绘画。 Can you please assist me with this? 你能帮我吗?

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 *
 * @author Curtis
 */
public class JSplash extends DFrame implements ActionListener {
//declaration of variable objects
    Font myFont = new Font("Arial", Font.BOLD, 20);
    JButton myButton = new JButton("Click Me!");
    Color bgColor = new Color(0, 0, 255);
    Color firstColor = new Color(255, 255, 255);
    String first = "Welcome to DaemoDynamics!";
    String last = "Click the Button";
    String middle = "";
    String middle2 = "";
    private static int count = 1;
    DFrame splash = new DFrame();
//Constructor
    public JSplash() {
        setDefaultLookAndFeelDecorated(true);
        System.out.println("Hello");
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        splash.add(myButton);
        getContentPane().setBackground(bgColor);
        //adds action listener
        myButton.addActionListener(this);
        splash.setVisible(true);
    }
//Paint method
    @Override
    public void paint(Graphics e) {
        System.out.println("paint is being reached");
        super.paint(e);
        e.setFont(myFont);
        e.setColor(firstColor);
        e.drawString(first, 14, 80);
        e.drawString(last, 70, 240);
        e.drawString(middle, 75, 150);
        e.drawString(middle2, 60, 175);
    }

//Listener Method
    @Override
    public void actionPerformed(ActionEvent e) {
        //First Time button hit
        if (count == 1) {
            middle = "Brighter Business";
            middle2 = "for A Brighter Future";
            last = "Click Again to Begin";
            repaint();
            //increases button count
            count++;
        } else//if button count is not 1
        {
            splash.setVisible(false);
            FinalProject app = new FinalProject();
        }
    }
}

You've create a NEW DFrame inside you JSplash constructor and then ADDED your components to IT. 您已经在JSplash构造函数中创建了一个NEW DFrame ,然后将组件添加到IT中。 This is simply not required. 根本不需要这样做。 Remove the reference's to splash and simply use the DFrame you've extended. 删除引用即可,只需使用扩展的DFrame And while i'm looking at, splash has no layout manager, which isn't going to help. 在我查看的同时,splash没有布局管理器,这对您没有帮助。 The paint method is never going to be called, because the window that's displayed on the screen is never the JSplash , but the DFrame you created (called splash) 永远不会调用paint方法,因为屏幕上显示的窗口永远不会是JSplash ,而是您创建的DFrame (称为splash)

Create a new class that extends JPanel. 创建一个扩展JPanel的新类。 Ensure your button is added to that panel and override the paintComponent() method of that panel with your logic. 确保将按钮添加到该面板,并使用您的逻辑覆盖该面板的paintComponent()方法。 This should work. 这应该工作。

Refer here. 请参考这里。

Custom painting should be done in paintComponent method. 自定义绘画应在paintComponent方法中完成。 Ideally you should not override the paint method of a JFrame. 理想情况下,您不应覆盖JFrame的paint方法。

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

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