0

我正在使用以下调用从我的主类创建另一个类的对象:

JSplash splash = new JSplash();

但是,当我创建这个对象时,它会执行 JSplash 类的构造函数并给出我的窗口和按钮。但它不会在框架上绘制。你能帮我解决这个问题吗?

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();
        }
    }
}
4

2 回答 2

1

You've create a NEW DFrame inside you JSplash constructor and then ADDED your components to IT. This is simply not required. Remove the reference's to splash and simply use the DFrame you've extended. And while i'm looking at, splash has no layout manager, which isn't going to help. 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)

于 2012-07-23T07:37:03.933 回答
0

创建一个扩展 JPanel 的新类。确保您的按钮已添加到该面板,并使用您的逻辑覆盖该面板的 paintComponent() 方法。这应该有效。

参考这里。

自定义绘画应该在 paintComponent 方法中完成。理想情况下,您不应覆盖 JFrame 的绘制方法。

于 2012-07-23T06:30:42.340 回答