Nach Look-And-Feel Umschaltung ist die Titelleiste weg !

Student_FH

Grünschnabel
Hallo Leute,

ich benötige Hilfe !! Ich habe ein Problem, das ich so auf die schnelle nicht lösen kann. Vielleicht kann mir ja einer helfen

:confused:

Wenn ich bei meiner einfachen Swing Anwendung das PLAF umschalte, dann verschwindet bei den 2 anderen Motiven die Titelleiste.
Klicke ich wieder auf das erste Motiv, so ist sie wieder da

:suspekt:.

Gruß Holger


Hier der Code:

Code:
 import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.Rectangle;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JCheckBox;
import javax.swing.JScrollPane;
import java.awt.GridBagLayout;
import javax.swing.ImageIcon;



	



public class AdornSample extends JFrame
{
     
	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JPanel jPanel = null;

	private JButton jButton = null;

	private JPanel jPanel1 = null;

	private JButton jButton1 = null;

	private JButton jButton2 = null;

	private JLabel jLabel = null;

	private JLabel jLabel1 = null;

	private JTextArea jTextArea = null;

	private JButton jButton3 = null;

	private JTextField jTextField = null;

	private JLabel jLabel3 = null;

	private JCheckBox jCheckBox = null;

	private JScrollPane jScrollPane = null;

	private JPanel jPanel2 = null;

	private JLabel jLabel2 = null;
	
	public AdornSample()
	  {
			/**
			 * This is the default constructor
			 */

	    super();
	    initialize();
	  }


		/**
		 * This method initializes this
		 * 
		 * @return void
		 */
		private void initialize() {
			this.setSize(300, 200);
			this.setContentPane(getJContentPane());
			this.setPreferredSize(new Dimension(500, 400));
			this.setTitle("Swing Oberfläche");
			this.setBounds(new Rectangle(500, 400, 497, 400));
			this.setContentPane(getJContentPane());
		}

		/**
		 * This method initializes jContentPane
		 * 
		 * @return javax.swing.JPanel
		 */
		private JPanel getJContentPane() {
			if (jContentPane == null) {
				jLabel = new JLabel();
				jLabel.setBounds(new Rectangle(24, 1, 228, 30));
				jLabel.setFont(new Font("Arial", Font.BOLD, 24));
				jLabel.setText("Swing Oberfläche");
				jContentPane = new JPanel();
				jContentPane.setLayout(null);
				jContentPane.setBackground(Color.lightGray);
				jContentPane.setToolTipText("JFrame");
				jContentPane.add(getJPanel(), null);
				jContentPane.add(getJPanel1(), null);
				jContentPane.add(jLabel, null);
				JFrame.setDefaultLookAndFeelDecorated(true);
			}
			return jContentPane;
		}

		private JPanel getJPanel() {
			if (jPanel == null) {
				jLabel3 = new JLabel();
				jLabel3.setBounds(new Rectangle(364, 41, 45, 23));
				jLabel3.setText("JPanel");
				jLabel1 = new JLabel();
				jLabel1.setBounds(new Rectangle(17, 9, 41, 23));
				jLabel1.setToolTipText("JLabel");
				jLabel1.setText("JLabel:");
				jPanel = new JPanel();
				jPanel.setLayout(null);
				jPanel.setBounds(new Rectangle(20, 31, 456, 268));
				jPanel.setToolTipText("JPanel");
				jPanel.setBackground(Color.gray);
				jPanel.add(jLabel1, null);
				jPanel.add(getJTextArea(), null);
				jPanel.add(getJButton3(), null);
				jPanel.add(getJTextField(), null);
				jPanel.add(jLabel3, null);
				jPanel.add(getJCheckBox(), null);
				jPanel.add(getJScrollPane(), null);
			}
			return jPanel;
		}

		/**
		 * This method initializes jButton	
		 * 	
		 * @return javax.swing.JButton	
		 */
		private JButton getJButton() {
			if (jButton == null) {
				jButton = new JButton();
				jButton.setText("Windows");
				jButton.setToolTipText("Windows Look & Feel");
				jButton.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						action(e);
						System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					}
				});
			}
			return jButton;
		}

		/**
		 * This method initializes jPanel1	
		 * 	
		 * @return javax.swing.JPanel	
		 */
		private JPanel getJPanel1() {
			if (jPanel1 == null) {
				GridLayout gridLayout = new GridLayout();
				gridLayout.setRows(1);
				jPanel1 = new JPanel();
				jPanel1.setLayout(gridLayout);
				jPanel1.setBounds(new Rectangle(16, 308, 460, 49));
				jPanel1.add(getJButton(), null);
				jPanel1.add(getJButton1(), null);
				jPanel1.add(getJButton2(), null);
			}
			return jPanel1;
		}

		/**
		 * This method initializes jButton1	
		 * 	
		 * @return javax.swing.JButton	
		 */
		private JButton getJButton1() {
			if (jButton1 == null) {
				jButton1 = new JButton();
				jButton1.setText("Motif");
				jButton1.setToolTipText("Motif Look & Feel");
				jButton1.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						action(e);
						System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					}
				});
			}
			return jButton1;
		}

		/**
		 * This method initializes jButton2	
		 * 	
		 * @return javax.swing.JButton	
		 */
		private JButton getJButton2() {
			if (jButton2 == null) {
				jButton2 = new JButton();
				jButton2.setText("Metal");
				jButton2.setToolTipText("Metal Look & Feel");
				jButton2.setMnemonic(KeyEvent.VK_UNDEFINED);
				jButton2.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						action(e);
						System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					}
				});

			}
			return jButton2;
		}

		/**
		 * This method initializes jTextArea	
		 * 	
		 * @return javax.swing.JTextArea	
		 */
		private JTextArea getJTextArea() {
			if (jTextArea == null) {
				jTextArea = new JTextArea();
				jTextArea.setBounds(new Rectangle(252, 67, 185, 190));
				jTextArea.setText("Beispiel Text für das jTextArea\n");
				jTextArea.setDragEnabled(true);
				jTextArea.setColumns(0);
				jTextArea.setRows(20);
				jTextArea.setToolTipText("JTextArea");
				//jTextArea.setText();
			}
			return jTextArea;
		}

		/**
		 * This method initializes jButton3	
		 * 	
		 * @return javax.swing.JButton	
		 */
		private JButton getJButton3() {
			if (jButton3 == null) {
				jButton3 = new JButton();
				jButton3.setBounds(new Rectangle(340, 10, 107, 32));
				jButton3.setToolTipText("JButton");
				jButton3.setText("Button");
				jButton3.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						jTextArea.setText(jTextArea.getText()+ jTextField.getText()+"\n");
						System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					}
				});
			}
			return jButton3;
		}

		/**
		 * This method initializes jTextField	
		 * 	
		 * @return javax.swing.JTextField	
		 */
		private JTextField getJTextField() {
			if (jTextField == null) {
				jTextField = new JTextField();
				jTextField.setBounds(new Rectangle(70, 10, 257, 23));
				jTextField.setToolTipText("JTextField");
				jTextField.setText("TextField");
				jTextField.setDragEnabled(true);
			}
			return jTextField;
		}
		   /**
		 * This method initializes jCheckBox	
		 * 	
		 * @return javax.swing.JCheckBox	
		 */
		private JCheckBox getJCheckBox() {
			if (jCheckBox == null) {
				jCheckBox = new JCheckBox();
				jCheckBox.setBounds(new Rectangle(73, 39, 138, 23));
				jCheckBox.setBackground(Color.gray);
				jCheckBox.setText("Checkbox");
				jCheckBox.addItemListener(new java.awt.event.ItemListener() {
					public void itemStateChanged(java.awt.event.ItemEvent e) {
						change(e);
						System.out.println("itemStateChanged()"); // TODO Auto-generated Event stub itemStateChanged()
					}
				});
			}
			return jCheckBox;
		}
		
		public void action(ActionEvent event)
		   {
		     String cmd = event.getActionCommand();
		     try {
		       //PLAF-Klasse auswählen 
		       String plaf = "unknown";
		       
		      if (cmd.equals("Metal")) 
		      		{
		    	  		plaf = "javax.swing.plaf.metal.MetalLookAndFeel";
		      		} 
		      else if (cmd.equals("Motif")) 
		      		{
		    	  		plaf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
		      		} 
		      else if (cmd.equals("Windows")) 
		      		{
		    	  		plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
		      		}
		       //LAF umschalten
		       UIManager.setLookAndFeel(plaf);

		       
		       SwingUtilities.updateComponentTreeUI(this);

	
		     } catch (UnsupportedLookAndFeelException e) {
		       System.err.println(e.toString());
		     } catch (ClassNotFoundException e) {
		       System.err.println(e.toString());
		     } catch (InstantiationException e) {
		      System.err.println(e.toString());
		     } catch (IllegalAccessException e) {
		       System.err.println(e.toString());
		     }
		   }
		/**
		 * This method initializes jScrollPane	
		 * 	
		 * @return javax.swing.JScrollPane	
		 */
		

		
		private JScrollPane getJScrollPane() {
			if (jScrollPane == null) {

			      
				jScrollPane = new JScrollPane();
				jScrollPane.setBounds(new Rectangle(15, 66, 213, 198));
				jScrollPane.setToolTipText("JScrollPane");
				
				jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
				jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				jScrollPane.setViewportView(getJPanel2());
				jScrollPane.setBackground(Color.white);
			}
			return jScrollPane;
		}
		public void change(java.awt.event.ItemEvent e)
		{
			
			if(e.getStateChange() == 1)
				{
					jLabel2.setVisible(false);

				}
			else
				{
				jLabel2.setVisible(true);
					
				}
			
		}
		/**
		 * This method initializes jPanel2	
		 * 	
		 * @return javax.swing.JPanel	
		 */
		private JPanel getJPanel2() {
			if (jPanel2 == null) {
				jLabel2 = new JLabel();
				jLabel2.setText("");
				jLabel2.setIcon(new ImageIcon(getClass().getResource("/logo.png")));
				jPanel2 = new JPanel();
				jPanel2.setLayout(new GridBagLayout());
				jPanel2.setBackground(Color.white);
				jPanel2.add(jLabel2, null);
			}
			return jPanel2;
		}
	 
	  


     public static void main(final String args[]) 
     {
    	   
           JFrame.setDefaultLookAndFeelDecorated(true);
           AdornSample test = new AdornSample();
           test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           test.setSize(300, 100);
           test.setVisible(true);
           test.getRootPane().setWindowDecorationStyle(JFrame.EXIT_ON_CLOSE);
           
     }
}
 
Hallo Holger,

du musst bedenken das nicht jedes Feature von jedem Look and Feel und auch nicht von jedem BS unterstützt werden.

Wenn du folgende Zeilen entfernst/auskommentierst besteht das Problem nicht mehr. Es scheint das manche L&Fs diese Titelleisten Decoration nicht unterstützten womit auch die Titelleiste gänzlich nicht erscheint.

Code:
//JFrame.setDefaultLookAndFeelDecorated(true);
//test.getRootPane().setWindowDecorationStyle(JRootPane.QUESTION_DIALOG);


Vg Erdal
 
Hallo Erdal,

die erste Zeile hab ich drin, damit nicht die Titelleiste von Windows verwendet wird.
Code:
JFrame.setDefaultLookAndFeelDecorated(true);
Die 2te war nur zum Testen drin.
Vielleicht zur Erklärung:
Ich möchte ein Fenster haben das keine Komponenten von " Betriebssystem" verwendet.
Swing soll also die Titelleiste selber zeichen und ich möchte mit dem Look-And-Feel dann umschalten und auch die geänderte Titelleiste angezeigt bekommen. Habe die auch mal in einer MDI Version probiert. Das hat wunderbar funktioniert, aber ich möchte halt nur ein einziges Fenster haben...


Trotzdem Danke !

Gruß Holger
 
Zurück