Можно ли настроить внешний вид один раз и сделать его "каскадным" для всех вложенных классов?
В приведенном ниже примере я задаю внешний вид и класс внутри класса Test
, однако JFileChooser
, который я добавляю в мой MainPanel
класс (вложенный в мой Test
класс) не изменяет его внешний вид, если я не установлю его снова в этом классе.
Это то, что мне нужно будет сделать для каждого класса, который я создаю? Или есть ли способ для меня, чтобы один и тот же внешний вид применялся во всех классах?
import java.awt.Dimension;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
* Class to demonstrate UIManager.setLookAndFeel issue.
*/
public class Test {
/**
* Main program.
* @param args
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test();
}
});
}
/**
* Constructor.
*/
public Test() {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
createGUI();
}
/**
* Set up the JFrame and add the main JPanel.
*/
public void createGUI() {
JFrame frame = new JFrame("Test");
frame.add(new MainPanel(800, 600));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
frame.setLocationRelativeTo(null);
}
/**
* Class for the main panel that will hold all other components.
*/
class MainPanel extends JPanel {
private final int width;
private final int height;
/**
* Serialize/save.
*/
private static final long serialVersionUID = -3727866499459986351L;
/**
* Constructor.
*/
public MainPanel(int w, int h) {
this.width = w;
this.height = h;
// ISSUE the chooser does not have the look and feel of my OS
// unless I set the look and feel in this constructor
JFileChooser chooser = new JFileChooser();
this.add(chooser);
}
/**
*
*/
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
}
}