gpt4 book ai didi

java - 框架上的 java 绘制问题

转载 作者:行者123 更新时间:2023-12-01 15:15:59 25 4
gpt4 key购买 nike

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

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

最佳答案

您已在 JSplash 构造函数中创建了一个新的 DFrame,然后将组件添加到 IT 中。这根本不是必需的。删除对splash 的引用,只需使用您扩展的DFrame 即可。当我查看时,splash 没有布局管理器,这没有帮助。 Paint 方法永远不会被调用,因为屏幕上显示的窗口永远不是 JSplash,而是您创建的 DFrame(称为splash)

关于java - 框架上的 java 绘制问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11607246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com