Hello,
I am having a problem with the look and feel methods. I have a
applet that can run either as an applet or an application. If it is run as a application, The code puts the applet into a JFrame.
This works, but the look and feel always seems to be metal. I want the look and feel to be windows. How can I accomplish this ?
This is code that was built with Jbuilder 5 with some additions
from me trying to accomplish what I want. when I switch to Jbuilders UI designer the screen pick up the windows look and feel.
I am using JDK 1.3.
Here is the code...
package untitled29;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class Applet2 extends JApplet {
boolean isStandalone = false;
JButton jButton1 = new JButton();
JComboBox jComboBox1 = new JComboBox();
/**Get a parameter value*/
public
String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**Construct the applet*/
public Applet2() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
jButton1.setText("jButton1");
jButton1.setBounds(new Rectangle(110, 39, 103, 27));
this.getContentPane().setLayout(null);
jComboBox1.setBounds(new Rectangle(87, 103, 156, 21));
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jComboBox1, null);
}
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
}
/**Main method*/
public static void main(String[] args) {
Applet2 applet = new Applet2();
applet.isStandalone = true;
JFrame frame;
frame = new JFrame() {
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
String laf = UIManager.getSystemLookAndFeelClassName();
try
{
UIManager.setLookAndFeel(laf);
SwingUtilities.updateComponentTreeUI(frame);
} catch (UnsupportedLookAndFeelException exc) {
System.err.println("Warning: UnsupportedLookAndFeel: " + laf);
} catch (Exception exc) {
System.err.println("Error loading " + laf + ": " + exc);
}
Frame[] frames = frame.getFrames();
for(int f = 0; f < frames.length; f++) {
SwingUtilities.updateComponentTreeUI(frames[f]);
frames[f].validate();
Window[] windows = frames[f].getOwnedWindows();
for(int w = 0; w < windows.length; w++) {
SwingUtilities.updateComponentTreeUI(windows[w]);
windows[w].validate();
}
}
frame.setTitle("Applet Frame");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
}