2013-03-27 39 views
1

嘿傢伙,所以我可以設置我的彈出窗口,從一個單獨的類中調用的默認框架。正如你可能會告訴我,我是一個極端的noob Java。任何幫助,將不勝感激。JFrame Swing如何從彈出窗口改變框架看起來像Windows主題

這是調用第二個類運行的代碼。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.text.NumberFormat; 

class Login extends JFrame implements ActionListener 
{ 
JButton SUBMIT; 
JPanel panel; 
JLabel label1,label2; 
final JTextField text1,text2; 
    Login() 
    { 
    label1 = new JLabel(); 
    label1.setText("    Enter Username:"); 
    label1.setForeground(Color.green); 

    text1 = new JTextField(10); 

    label2 = new JLabel(); 
    label2.setText("    Enter Password:"); 
    label2.setForeground(Color.green); 
    text2 = new JPasswordField(10); 

    SUBMIT=new JButton("SUBMIT"); 
    SUBMIT.setOpaque(true); 
    SUBMIT.setBackground(Color.BLACK); 
    SUBMIT.setForeground(Color.green); 

    panel=new JPanel(new GridLayout(4,1)); 
    panel.add(label1); 
    panel.add(text1); 
    panel.add(label2); 
    panel.add(text2); 
    panel.add(SUBMIT); 
    add(panel,BorderLayout.CENTER); 
    SUBMIT.addActionListener(this); 
    setTitle("LOGIN or DIE!!!!!"); 
    panel.setBackground(Color.black); 
    setDefaultLookAndFeelDecorated(true); 
    setLocationRelativeTo(null); 
    } 
public void actionPerformed(ActionEvent ae) 
    { 
    String value1=text1.getText(); 
    String value2=text2.getText(); 
    if (value1.equals("McDinger") && value2.equals("welcome1")) { 
    ExerciseSevenPt2 page=new ExerciseSevenPt2(); 
    page.setVisible(true); 
    JLabel label = new JLabel(" Welcome to The Java Cave, "+value1 + ". " + "Are you Worthy of the Cave?"); 
    label.setOpaque(true); 
    label.setForeground(Color.green); 
    label.setBackground(Color.black); 
    page.getContentPane().add(label); 
    } 
    else{ 
    System.out.println("enter the valid username and password OR ELSE!!!!!"); 

    UIManager UI=new UIManager(); 
    UI.put("OptionPane.messageForeground", Color.red); 
    UI.put("OptionPane.background", Color.black); 
    UI.put("Panel.background", Color.black); 

    JOptionPane.showMessageDialog(this,"Incorrect login or password Genius", "Error",JOptionPane.ERROR_MESSAGE); 
    setDefaultLookAndFeelDecorated(true); 

    } 
} 
} 
class ExerciseSeven 
{ 
    public static void main(String arg[]) 
    { 
    try 
    { 
    Login frame=new Login(); 
    frame.setSize(300,100); 
    frame.setVisible(true); 
    } 
    catch(Exception e) 
    {JOptionPane.showMessageDialog(null, e.getMessage());} 
    } 
} 

這裏是第二類

import javax.swing.*; 
import java.awt.*; 

class ExerciseSevenPt2 extends JFrame 
{ 
    ExerciseSevenPt2() 
{  
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 
setTitle("The Java Cave "); 
setSize(400,70); 
setLocationRelativeTo(null); 
} 
} 

這裏是我的問題,執行代碼後視覺。 底部窗口是我想要的頂部的樣子。

enter image description here

+1

1)請參閱[多JFrames,好/壞習慣的用?(http://stackoverflow.com/a/9554657/418556)的登錄應顯示在一個模式'JDialog'或者「JOptionPane」。 2)AFAIU彈出窗口應該使用當前的PLAF。 PLAF如何設置?爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-03-27 23:13:32

+0

如何連接SSCCE? – 2013-03-27 23:19:49

+0

與上面的代碼片段和類相同,除了包含自包含的源文件。詳情請閱讀[鏈接文件](http://sscce.org/)。 – 2013-03-27 23:22:07

回答

6

您必須使用您的Windows UIManager to "set the look and feel"

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
+0

..有時會附帶['updateComponentTreeUI(Component)'](http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#updateComponentTreeUI%28java.awt.Component %29)。沒有SSCCE,很難說這種或那種。請參閱[本示例](http://stackoverflow.com/a/5630271/418556),它可以在運行時更改PLAF(並且所有對話框也會更改)。 – 2013-03-27 23:27:55

+0

在我的代碼中,我需要在第二課中使用它嗎? – 2013-03-27 23:28:44

+0

在main()方法中,使用適當的try/catch(請參閱http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html) – evuez 2013-03-27 23:38:43

2

正如evuez已經說明(+1),你只需要安裝系統的外觀和感覺。

查看更多詳情How to set the look and feel

下面的示例基本上安裝了系統外觀,然後再執行其他操作。這是非常重要的,否則你可能會遇到非常討厭的油漆效果和不匹配的渲染更新。

enter image description here

很顯然,我使用的Mac OS的外觀和感覺,但它會使用什麼都爲系統的外觀和感覺你運行它什麼都平臺。

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test02 { 

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

    public Test02() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 
       JFrame frame = new JFrame("The Java Cave "); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setBackground(Color.BLACK); 
      JLabel label = new JLabel("Welcome to the Jave Cave, be afraid, be very afraid"); 
      label.setForeground(Color.GREEN); 
      add(label); 
     } 

    } 

} 
+0

這使我更接近,但我仍然無法弄清楚如何讓它從我的if else語句中拉出正確的窗口。 – 2013-03-28 02:19:49

+0

嘗試使用'equalsIgnoreCase'而不是'equals' ...假設案件無關緊要;) – MadProgrammer 2013-03-28 06:37:15