简体   繁体   中英

Change Swing L&F at command line not work well

I made an application with some widgets and at command line I want change their look and feel:

java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel LookAndFeelAppl

but after invoked that command only a widget into the class constructor change its L&F but other that I created into a separate methods don't!!! Also tha JFrame itself not change.

public class LookAndFeelAppl extends JFrame { private JLabel label; private JButton button;

public LookAndFeelAppl()
{
    super("Look And Feel Demo");

    setLayout(null);

    final UIManager.LookAndFeelInfo plaf[] = UIManager.getInstalledLookAndFeels();

    JLabel lable_laf = new JLabel("Choose L&F:");
    final JComboBox cb = new JComboBox();

    createOtherGUI();

    cb.addItemListener(new ItemListener()
    {
        public void itemStateChanged(ItemEvent e)
        {
            int ix = cb.getSelectedIndex();
            try
            {
                UIManager.setLookAndFeel(plaf[ix].getClassName());
                SwingUtilities.updateComponentTreeUI(LookAndFeelAppl.this);
            }
            catch (Exception ex)
            {
            }
        }
    });

    // HERE IS THE PROBLEM!!!! THIS LOOP GOES BEFORE
    // THE ITEMLISTENER BECAUSE WHEN I ADD ITEMS AN ITEMEVENT
    // IS RAISED THAT SET AGAIN THE L&F WITH setLookAndFeel!!!
    for (int i = 0, n = plaf.length; i < n; i++)
    {
      cb.addItem(plaf[i].getName());
    }
    //--------------------------------------------------------

    add(lable_laf);
    add(cb);
    lable_laf.setBounds(10, 10, 150, 25);
    cb.setBounds(10, 35, 150, 25);
}

public void createOtherGUI()
{
    button = new JButton("BUTTON!!!!");
    add(button);
    button.setBounds(300, 45, 150, 35);
}

public static void main(String args[])
{
    LookAndFeelAppl window = new LookAndFeelAppl();
    window.setSize(1000, 700);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
    window.setLocationRelativeTo(null);
}

}

I ran this in Eclipse (latest version) using JDK 1.6.0_13 on windows. In the run configuration (VM arguments) of Eclipse I used -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel

public class Test extends JFrame {

public Test() {

super("Test L&F");

JComboBox box = new JComboBox(new String[] {"One", "Two", "Three"});

getContentPane().add(box, BorderLayout.SOUTH);

createControls();

setSize(300, 300);
setVisible(true);

}

private void createControls() {

JComboBox box = new JComboBox(new String[] {"One", "Two", "Three"});

getContentPane().add(box, BorderLayout.NORTH);

}

public static void main(String[] args) { new Test(); } }

Sorry, formatting is weird but you get the idea...

Few things to try and questions:

  1. I see you are setting the L&F for windows, so I'm assuming you are running this on a windows box? The reason I ask is I'm not sure what the JDK is supporting as far as cross-platform L&Fs, just something to think about.
  2. Did you try other L&Fs with the same result? (eg Nimbus)
  3. This may or may not do anything but where do you have main(...) ? Try putting it in the JFrame and in a separate class that constructs the JFrame.
  4. Worst comes to worse you could always pass the L&F classname in as an arg to main(...) (or a props file) then use the UIManager to set it BEFORE any swing components are created.

Good luck, Dave

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM