我正在使用以下调用从我的主类创建另一个类的对象:
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();
}
}
}