Reputation: 153
EDITED 2/2/2013
With the help of some of your answers and a simple Swing application guide I found, I was able to make a little bit of progress and I put it all in one class for now. However, I still can't get it to close and I had to make a button to do it because it was getting really annoying having to pull up task manager. The tooltips aren't working either. I don't know if it matters, but I'm doing it in Eclipse.
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Thing extends JFrame
{
private static final long serialVersionUID = 1L;
public Thing()
{
setTitle("Thing");
setSize(1024, 768);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
panel.setToolTipText("A panel container");
JButton quit = new JButton("Quit");
quit.setBounds(50, 60, 80, 30);
quit.setToolTipText("A button component");
quit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
panel.add(quit);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Thing thing = new Thing();
thing.setVisible(true);
}
});
}
}
This is the error it gives when I try to close it with the X. (The closing works fine with the button)
java.lang.NoClassDefFoundError: sun/awt/TimedWindowEvent
at sun.awt.windows.WToolkit.eventLoop(Native Method)
at sun.awt.windows.WToolkit.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Upvotes: 1
Views: 374
Reputation: 851
I'm not sure if you fixed your problem, but I had a similar problem: For my application I extended JFrame, but was not able to close or hide it with any setDefaultCloseOperation(...). I got the same error you showed above when minimizing, restoring or maximizing the frame.
I fixed it updating both jdk and jre to latest Java 1.7 and using it. In eclipse, go to Window > Preferences > Java > Installed JREs
and select your latest Java if working on 64 bit System, best use also 64 bit Java. In the submenu > Execution Environment
select your Java 1.7 on the left hand and compatible Java 1.7 64 bit JRE on the right hand.
I'm not sure what exactly was the problem, but possibly some incompatibility between Java 64 bit Application and 32 bit runtime environment I guess.
Upvotes: 0
Reputation: 347184
Toolkit.getDefaultToolkit().getImage("image.jpg")
is likely using a background loader to load the image off the main paint thread (or Event Dispatching Thread, AKA EDT).
This basically means, the method will return immeditly, but the image won't actually have loaded.
This was done (way back in the olden days) for low capacity bandwidth systems, to prevent the systems from stalling while images where downloaded and loaded into memory. Now days, this is much less of a concern.
Instead, in the constructor of Title
you should pre-load the image...
private BufferedImage background;
public Title() throws IOException {
background = ImageIO.read(new File("image.jpg"));
}
And in your paintComponent
method...
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (background != null) {
g.drawImage(background, 0, 0, this);
}
}
The other problems with you current approach to loading the images deals with how it handle errors. Your current method will tend to silently consume the errors and it's difficult to determine what's actually gone wrong.
You could take a look at JButton only show up on mouseover for more of a complete example
Upvotes: 1
Reputation: 9579
For some reason, I can't close it and have to use task manager.
Use SwingUtilies.invokeLater()
to create GUI.
http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
Upvotes: 2