Hallo,
ich habe ein kleines Problem, und zwar möchte ich, das ich auch über das Menü das Look&Feel ändern kann, leider funktioniert das nur, wenn ich Windows anwähle..
hätte da jemand rat? vielleicht auch, wie ich das mit dem menü über ein array lösen könnte?
danke
ich habe ein kleines Problem, und zwar möchte ich, das ich auch über das Menü das Look&Feel ändern kann, leider funktioniert das nur, wenn ich Windows anwähle..
hätte da jemand rat? vielleicht auch, wie ich das mit dem menü über ein array lösen könnte?
danke
Code:
/**
* Autor:
* Version: j2sdk1.4.2
* Beschreibung: Demoprogramm für Swing-Komponenten
* look & feel wird über Button eingestellt.
* Datum: 30.3.2005
* Änderungen:
*/
import java.awt.*; // wg Container, GridBagLayout, etc.
import java.awt.event.*; // wg ActionListener etc.
import javax.swing.*;
import javax.swing.event.*;
class SwingDemo1{
public static void main(String [] arg){
// hier soll ein Fenster-Objekt konstruiert werden
SwingFenster meinFensterObjekt = new SwingFenster();
meinFensterObjekt.pack();
meinFensterObjekt.show();
}
}// end of class SwingDemo0
class SwingFenster extends JFrame{
// Membervariablen
JPanel
pnlFlaeche,
pnlEingabe,
pnlAuswahl,
pnlButtons,
pnlRadio;
JButton
butOK,
butReturn;
JLabel
labName,
labListBox,
labComboBox,
labEMail,
labFrei2,
labFrei1,
labFrei3,
labFrei4,
labFrei6;
JTextField
txfName,
txfEMail;
String
lafName1 = "javax.swing.plaf.metal.MetalLookAndFeel",
lafName2 = "com.sun.java.swing.plaf.motif.MotifLookAndFeel",
lafName3 = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
JButton
butMotif,
butMetal,
butWindows;
JList
lstAuswahl;
JComboBox
cmbAuswahl;
JRadioButton
radiobuttons1,
radiobuttons2,
radiobuttons3;
JMenuBar
menubar;
JMenu
mnuFileExit,
mnuFile;
JFrame fenster = null;
String lafName = null;
final int numButtons = 3;
JRadioButton[] radioButtons
= new JRadioButton[numButtons];
final ButtonGroup group = new ButtonGroup();
final String[] lufComboChoose = {"Metal", "Motif", "Windows"};
// Konstruktoren -----------------------------------------
public SwingFenster(){
this("JKomponenten");
JMenuBar menubar = new JMenuBar();
menubar.add(createFileMenu());
setJMenuBar(menubar);
}// end of constructor
public SwingFenster(String titelleiste){
super(titelleiste);
fenster = this; // Referenz auf das Fensterobjekt selbst
//Look & Feel festlegen
try{
UIManager.setLookAndFeel( lafName1 );
}
catch( Exception exp){
System.out.println("Fehler bei l&f " + exp.getMessage() );
}
// nun die ContentPane holen
Container cPane = this.getContentPane();
cPane.setLayout(new BorderLayout());
// this.getContentPane().setLayout(new BorderLayout());
// Komponenten konstruieren und auf das Panel legen
pnlEingabe = new JPanel();
labName = new JLabel("Name");
labEMail = new JLabel("E-Mail");
labFrei2 = new JLabel(" ");
labFrei1 = new JLabel(" ");
txfName = new JTextField(30);
txfEMail = new JTextField(30);
// nun Layout für das Panel einstellen
GridBagLayout grid = new GridBagLayout();
GridBagConstraints constr = new GridBagConstraints();
pnlEingabe.setLayout( grid );
// nun Komponenten auf das Panel legen
constr.gridx = 1; // Spalte
constr.gridy = 2; // Zeile
pnlEingabe.add(labName, constr);
constr.gridx = 2; // Spalte
constr.gridy = 2; // Zeile
pnlEingabe.add(txfName, constr);
constr.gridx = 1; // Spalte
constr.gridy = 3; // Zeile
pnlEingabe.add(labEMail, constr);
constr.gridx = 2; // Spalte
constr.gridy = 3; // Zeile
pnlEingabe.add(txfEMail, constr);
constr.gridx = 2;
constr.gridy = 4;
pnlEingabe.add(labFrei2, constr);
constr.gridx = 2;
constr.gridy = 1;
pnlEingabe.add(labFrei1, constr);
// nun das Panel auf das Fenster legen
cPane.add("North", pnlEingabe);
// nun Layout für das Panel einstellen
pnlAuswahl = new JPanel();
labListBox = new JLabel("ListBox");
labComboBox = new JLabel("ComboBox");
labFrei3 = new JLabel(" ");
labFrei4 = new JLabel(" ");
labFrei6 = new JLabel(" ");
lstAuswahl = new JList();
cmbAuswahl = new JComboBox(lufComboChoose);
cmbAuswahl.setSelectedIndex(0);
cmbAuswahl.addActionListener(new ActionAdapter3());
GridBagConstraints constr2 = new GridBagConstraints();
pnlAuswahl.setLayout( grid );
// nun Komponenten auf das Panel legen
constr2.gridx = 2;
constr2.gridy = 1;
pnlAuswahl.add(labListBox, constr2);
constr2.gridx = 5;
constr2.gridy = 1;
pnlAuswahl.add(labComboBox, constr2);
constr2.gridx = 3;
constr2.gridy = 1;
constr2.ipadx = 51;
constr2.ipady = 69;
pnlAuswahl.add(lstAuswahl, constr2);
constr2.gridx = 7;
constr2.gridy = 1;
constr2.ipady = 1;
constr2.ipadx = 40;
pnlAuswahl.add(cmbAuswahl, constr2);
constr2.gridx = 1;
constr2.gridy = 1;
pnlAuswahl.add(labFrei3, constr2);
constr2.gridx = 4;
constr2.gridy = 1;
pnlAuswahl.add(labFrei4, constr2);
constr2.gridx = 8;
constr2.gridy = 1;
pnlAuswahl.add(labFrei6, constr2);
// nun das Panel auf das Fenster legen
cPane.add("Center", pnlAuswahl);
// drittes Panel für Radio-Button anlegen -----------
pnlRadio = new JPanel();
// nun Layout für das Panel einstellen
GridLayout gridLayout = new GridLayout(3,2);
pnlRadio.setLayout( gridLayout );
radioButtons[0] = new JRadioButton("Metal");
radioButtons[0].setForeground(new Color(255, 51, 51));
radioButtons[0].setActionCommand("Metal");
radioButtons[1] = new JRadioButton("Motif");
radioButtons[1].setForeground(new Color(0, 0, 153));
radioButtons[1].setActionCommand("Motif");
radioButtons[2] = new JRadioButton("Windows");
radioButtons[2].setForeground(new Color(0, 102, 51));
radioButtons[2].setActionCommand("Windows");
for (int i = 0; i < numButtons; i++) {
pnlRadio.add(radioButtons[i],gridLayout);
group.add(radioButtons[i]);
}
//Select the first button by default.
radioButtons[0].setSelected(true);
// nun das Panel auf das Fenster legen
cPane.add("East", pnlRadio);
//Listener anbinden
for (int i = 0; i < numButtons; i++) {
radioButtons[i].addActionListener(new ActionAdapter2());
}
// Komponenten konstruieren und auf das Panel legen
pnlButtons = new JPanel();
butMotif = new JButton("Metal" );
butWindows = new JButton("Windows" );
butMetal = new JButton("Motif" );
GridBagConstraints constr4 = new GridBagConstraints();
pnlButtons.setLayout( grid );
butMotif.setForeground(new Color(0, 0, 153));
constr4.gridx = 0;
constr4.gridy = 1;
constr4.fill = GridBagConstraints.BOTH;
pnlButtons.add(butMotif, constr4);
butMetal.setForeground(new Color(255, 51, 51));
constr4.gridx = 0;
constr4.gridy = 2;
constr4.fill = GridBagConstraints.BOTH;
pnlButtons.add(butMetal, constr4);
butWindows.setForeground(new Color(0, 102, 51));
constr4.gridx = 0;
constr4.gridy = 3;
constr4.fill = GridBagConstraints.BOTH;
constr4.anchor = GridBagConstraints.NORTH;
pnlButtons.add(butWindows, constr4);
// nun das Panel auf das Fenster legen
cPane.add("West", pnlButtons);
pack();
// Listener anbinden
butMotif.addActionListener(new ActionAdapter());
butMetal.addActionListener(new ActionAdapter());
butWindows.addActionListener(new ActionAdapter());
}// end of constructor
// Methoden -----------------------------------------------
public JMenu createFileMenu()
{
JMenu ret = new JMenu("Look & Feel");
JMenuItem mi;
mi = new JMenuItem("Metal");
ret.add(mi);
mi = new JMenuItem("Motif");
ret.add(mi);
mi = new JMenuItem("Windows");
mi.addActionListener(new ActionAdapter4());
ret.add(mi);
fenster.pack();
return ret;
}
// Implementierungen der ListenerInterfaces ---------------
class ActionAdapter4 implements ActionListener
{
public void actionPerformed(ActionEvent e) {
JMenuItem mi = (JMenuItem)e.getSource();
String newSelection = (String)mi.getText();
if (newSelection == "Metal") {
lafName = lafName1;
} else if (newSelection == "Motif") {
lafName = lafName2;
} else if (newSelection == "Windows") {
lafName = lafName3;
}
try{
UIManager.setLookAndFeel( lafName );
SwingUtilities.updateComponentTreeUI( fenster );
}
catch( Exception exp){
System.out.println("Fehler bei l&f " + exp.getMessage() );
}
fenster.pack();
}
}// end of class ActionAdapter4
class ActionAdapter3 implements ActionListener
{
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String newSelection = (String)cb.getSelectedItem();
if (newSelection == "Metal") {
lafName = lafName1;
} else if (newSelection == "Motif") {
lafName = lafName2;
} else if (newSelection == "Windows") {
lafName = lafName3;
}
try{
UIManager.setLookAndFeel( lafName );
SwingUtilities.updateComponentTreeUI( fenster );
}
catch( Exception exp){
System.out.println("Fehler bei l&f " + exp.getMessage() );
}
fenster.pack();
}
}// end of class ActionAdapter3
class ActionAdapter2 implements ActionListener
{
public void actionPerformed(ActionEvent e) {
String command
= group.getSelection().getActionCommand();
if (command == "Metal") {
JOptionPane.showMessageDialog( fenster,
"Gewählt wurde l&f Metal");
lafName = lafName1;
} else if (command == "Motif") {
JOptionPane.showMessageDialog( fenster,
"Gewählt wurde l&f Motif");
lafName = lafName2;
} else if (command == "Windows") {
JOptionPane.showMessageDialog( fenster,
"Gewählt wurde l&f Windows");
lafName = lafName3;
}
try{
UIManager.setLookAndFeel( lafName );
SwingUtilities.updateComponentTreeUI( fenster );
}
catch( Exception exp){
System.out.println("Fehler bei l&f " + exp.getMessage() );
}
fenster.pack();
}
}// end of class ActionAdapter2
class ActionAdapter implements ActionListener
{
public void actionPerformed(ActionEvent ev ){
Object quelle = ev.getSource();
if( quelle == butMotif){
lafName = lafName1;
}
else if(quelle == butMetal){
lafName = lafName2;
}
else if(quelle == butWindows){
lafName = lafName3;
}
try{
UIManager.setLookAndFeel( lafName );
SwingUtilities.updateComponentTreeUI( fenster );
}
catch( Exception exp){
System.out.println("Fehler bei l&f " + exp.getMessage() );
}
fenster.pack();
}
}// end of inner class ActionAdapter
}// end of class SwingFenster