I'm sorry for the amount of code (and I also presume that the formatting is pretty bas as well); I've only been coding java for 2 days atm, and are having a blast with Swing. I am trying to make a desktop that will act as an application/class launcher, with my own desktop with internal frames.
What Im trying to do below is to add the jinternalframe to the desktop when TestApp (menuItem) is selected from the menu. However, nothing appears on my screen. Ive tested that the action is actually happening by putting in a println command, and it seems that everything is being executed. Still, I can't see the internal frame.
Any help/tips would be greatly appreciated, along with tips on how to improve how I build up my code.
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Supporter
{
public static void main(String[] args)
{
SupporterDesktop start_application = new SupporterDesktop();
}
}
class SupporterDesktop extends JFrame implements ActionListener
{
JMenuItem menuItem;
JMenuItem menuItem2;
EMDesktop desktop;
MyInternalFrame demo = new MyInternalFrame();
public SupporterDesktop()
{
// Make the window and its container
JFrame window = new JFrame();
window.setSize(1024,768);
Container windowContent = window.getContentPane();
window.setLayout(new BorderLayout());
window.setDefaultLookAndFeelDecorated(true);
// Make the menu bar
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Program");
menuItem = new JMenuItem("TestApp");
menuItem2 = new JMenuItem("Quit");
menuBar.add(menu);
menu.add(menuItem);
menu.add(menuItem2);
// Make the desktop
desktop = new EMDesktop();
desktop.add(demo);
// Make listeners and assign components to them
menuItem.addActionListener(this);
menuItem2.addActionListener(this);
// Add all the components
window.add(menuBar, BorderLayout.NORTH);
window.add(desktop, BorderLayout.CENTER);
// Set startup visible values
window.setVisible(true);
}
void quit()
{
System.exit(0);
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if (source == menuItem)
{
JInternalFrame frame = new JInternalFrame(("Internal Frame"), true, true, true, true);
frame.setBackground(Color.white);
frame.moveToFront();
frame.setVisible(true);
desktop.add(frame);
}
else if (source == menuItem2)
{
quit();
}
else
{
System.out.println("Unspecified or unknown input.");
}
}
}
class EMDesktop extends JDesktopPane
{
Canvas mycanvas;
String filename = new String("images/wallpaper.jpg");
public EMDesktop()
{
}
public EMDesktop(String input)
{
filename = input;
}
public void paint(Graphics g)
{
ImageIcon myimage = new ImageIcon(filename);
Image image = myimage.getImage();
g.drawImage(image,0,0,this.getSize().width,this.getSize().height,myimage.getImageObserver());
}
public void setImage(String input)
{
filename = input;
}
}