【发布时间】:2017-03-03 19:24:22
【问题描述】:
我有一个创建带有图像的 JFrame 的类,但每次我创建该类并运行实例化它的方法时,它都不会出现。但是,我注意到如果我要创建完全相同的类并在 main 中运行相同的方法,则会出现框架。
这是我尝试创建的带有 JFrame 的类中的大部分代码:
JFrame myFrame= new JFrame();
public void CreateFrame()
{
JLabel background=new JLabel(new ImageIcon("image.jpg"));
myFrame.add(background);
background.setLayout(new FlowLayout());
myFrame.setLayout(new GridLayout(1,1));
myFrame.setSize(360,250);
myFrame.setUndecorated(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
myFrame.setLocation((dim.width/2-170), dim.height/2-125);
myFrame.pack();
myFrame.setVisible(true);
}
如果我运行代码
MyClass mc = new MyClass();
mc.CreateFrame();
在另一个类的方法中它不会出现。但是,如果我在 main 方法中运行完全相同的代码,它就可以工作。
例如,这不起作用:
示例 1
public class otherClass extends JFrame
{
public void MethodA()
{
MyClass mc = new MyClass();
mc.CreateFrame();
}
public static void main(String[] args)
{
otherClass oc = new otherClass();
oc.MethodA();
}
}
但这确实有效
示例 2
public class otherClass extends JFrame
{
public void MethodA()
{
//CODE
}
public static void main(String[] args)
{
otherClass oc = new otherClass();
oc.MethodA();
MyClass mc = new MyClass();
mc.CreateFrame();
}
}
谁能看出我哪里出错了?抱歉,如果是一个愚蠢的错误,我仍在掌握 Java。 谢谢
编辑
import javax.swing.ImageIcon;
import javax.swing.JWindow;
import javax.swing.JLabel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.util.Timer;
import java.util.Date;
import javax.swing.JFrame;
public class MyClass
{
JFrame homeFrame = new JFrame();
public void createFrame()
{
JLabel background=new JLabel(new ImageIcon("images.jpg"));
myFrame.add(background);
background.setLayout(new FlowLayout());
myFrame.setLayout(new GridLayout(1,1));
myFrame.setSize(360,250);
myFrame.setUndecorated(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
myFrame.setLocation((dim.width/2-170), dim.height/2-125);
myFrame.setAlwaysOnTop(true);
myFrame.pack();
myFrame.setVisible(true);
durationOfTime();
}
public void durationOfTime()
{
MainProgram mp = new MainProgram();
long startTime = System.currentTimeMillis();
long elapsedTime = 0L;
int count =0;
while (elapsedTime < 2*1000)
{
if(count==0)
{
mp.launchInitiation();
}
count+=1;
elapsedTime = (new Date()).getTime() - startTime;
}
myFrame.setVisible(false);
mp.homeFrame.setVisible(true);
}
public static void main(String[] args)
{
MyClass mc = new MyClass();
mc.createFrame();
}
}
JFrame 试图制作的类的完整代码。我正在尝试将此 JFrame 用作启动屏幕,但无论我调用什么类
MyClass mc = new MyClass();
mc.createFrame();
从,它只是没有出现。在我的主 GUI 出现之前确实经过了两秒钟,但这个方法应该在登录类型框架中调用。但是,我已经用一个空白的 JFrame / GUI 对其进行了测试,它也会在按钮单击时出现,但它仍然没有出现。
编辑2
我之前也尝试过 @http://examples.oreilly.com/jswing2/code/ch08/SplashScreen.java 的这个 SplashScreen 示例,但我无法让它工作(同样的问题,从 main 调用时出现,但从动作侦听器调用时不出现)
import java.awt.*;
import javax.swing.*;
public class SplashScreen extends JWindow {
private int duration;
public SplashScreen(int d) {
duration = d;
}
// A simple little method to show a title screen in the center
// of the screen for the amount of time given in the constructor
public void showSplash() {
JPanel content = (JPanel)getContentPane();
content.setBackground(Color.white);
// Set the window's bounds, centering the window
int width = 450;
int height =115;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width-width)/2;
int y = (screen.height-height)/2;
setBounds(x,y,width,height);
// Build the splash screen
JLabel label = new JLabel(new ImageIcon("oreilly.gif"));
JLabel copyrt = new JLabel
("Copyright 2002, O'Reilly & Associates", JLabel.CENTER);
copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
content.add(label, BorderLayout.CENTER);
content.add(copyrt, BorderLayout.SOUTH);
Color oraRed = new Color(156, 20, 20, 255);
content.setBorder(BorderFactory.createLineBorder(oraRed, 10));
// Display it
setVisible(true);
// Wait a little while, maybe while loading resources
ClassToLoad ctl = new ClassToLoad();
try {
Thread.sleep(duration);
ctl.initiate();
} catch (Exception e) {}
setVisible(false);
}
public void showSplashAndExit() {
showSplash();
System.exit(0);
}
public static void main(String[] args) {
// Throw a nice little title page up on the screen first
SplashScreen splash = new SplashScreen(10000);
// Normally, we'd call splash.showSplash() and get on with the program.
// But, since this is only a test...
splash.showSplashAndExit();
}
}
我在 ClassToLoad 行中添加了代码,并在动作侦听器上调用了这个 SplashScreen,发生的情况是程序等待我告诉它的 2 秒,没有出现任何框架,然后是我想要的主类在启动画面可见时加载。我首先尝试了这种方法,但这没有奏效,这导致我使用此编辑上面列出的代码
编辑 3
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestFrame extends JFrame implements ActionListener
{
JPanel thePanel = new JPanel(null); //layout
JButton button = new JButton();
public void startGUI()
{
this.setLayout(new GridLayout(1,1));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CREATEMYPANEL();
this.add(thePanel);
this.setTitle("NO_TITLE_SET");
this.setSize(400,400);
this.setVisible(true);
this.setResizable(true);
}
public void CREATEMYPANEL()
{
button.setLocation(242,151);
button.setSize(100,50);
button.addActionListener(this);
button.setText("button");
thePanel.add(button);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
System.out.println("button has been pressed ");
SplashScreen splash = new SplashScreen(1000);
splash.showSplash();
}
}
public static void main(String[] args )
{
TestFrame tf = new TestFrame();
tf.startGUI();
}
}
我从哪里调用启动画面的示例。还是不行。另外,请注意我正在加载的图像是本地图像
对错误的问题格式表示歉意
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。
-
@khelwood 我还有其他类,我可以按照我描述的方式从方法中创建其他类/框架;为什么这个例子有什么不同。
-
我刚刚运行了这段代码,两个例子都有效。无论您遇到什么问题,都不会在您显示的代码中表达出来。
-
为什么
otherClass扩展JFrame而你却从不使用它?也请遵循naming conventions 并始终如一地使用它们:firstWordLowerCaseMember、firstWordLowerCaseMethod()、FirstWordUpperCaseClass和ALL_UPPER_CASE_WORDS_CONSTANTS,以便您和我们更轻松地阅读您的代码 -
我知道您尝试加载的图像存储在本地,但您没有尝试从 URL 加载它吗?这会告诉我们是否是您的图像未加载,因此您的框架的大小为 0, 0
标签: java swing class user-interface jframe