Rahul
Posts: 52
Nickname: wildhorse
Registered: Oct, 2002
|
|
Re: Help with code modification
|
Posted: Apr 11, 2003 12:35 AM
|
|
I've modified your program slightly
Declare: JTextField txtField instantiate: txtField = new JTextField(""); add to container: c.add(txtField, BorderLayout.NORTH); // commented out the button code (Look and Feel=) fillin the text field: txtField.setText(strPLAF);
Check the relevant portions of the program:
import java.awt.*; import javax.swing.*; import java.awt.event.*;
public class TestChangePLAF extends JFrame { private JLabel label;
JButton btnMetal, btnWindows, btnMotif; JTextField txtField; TestChangePLAF app;
public static void main( String args[]) { TestChangePLAF app = new TestChangePLAF(); app.app = app; app.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); app.pack(); app.setVisible(true); }
public TestChangePLAF() { Container c = getContentPane(); c.setLayout(new BorderLayout());
ActionListener pl = new ActionListener() { public void actionPerformed(ActionEvent e) { String strPLAF = new String(); try { String s = e.getActionCommand(); System.out.println(s);
if (s.equals("Metal")) { strPLAF = "javax.swing.plaf.metal.MetalLookAndFeel"; } else if (s.equals("Windows")) { strPLAF = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; } else if (s.equals("Motif")) { strPLAF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; }
txtField.setText(strPLAF);
UIManager.setLookAndFeel(strPLAF); SwingUtilities.updateComponentTreeUI(app); app.pack(); } catch (Exception e2) { e2.printStackTrace(System.err); } } };
btnMetal = new JButton("Metal"); btnMetal.addActionListener(pl);
btnWindows = new JButton("Windows"); btnWindows.addActionListener(pl);
btnMotif = new JButton("Motif"); btnMotif.addActionListener(pl);
txtField = new JTextField("");
//c.add(new JButton("Look and Feel ="), BorderLayout.NORTH); c.add(txtField, BorderLayout.NORTH); // c.add(new JButton("South"), BorderLayout.SOUTH); c.add(btnMetal, BorderLayout.EAST); c.add(btnWindows, BorderLayout.CENTER); c.add(btnMotif, BorderLayout.WEST); } }
|
|