2011-06-28 4 views
0

これのいくつかの例を見てきましたが、次のコードを試しました。私は、portraitBが選択されているときにコンテンツペインを変更してから、もう一方のクラスファイルを実行しようとしています。コンテンツの変更JFrameの

//imported java libraries 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.UIManager; 
import javax.swing.*; 
import javax.swing.border.*; 
import java.awt.Dimension; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 


public class birthdayCardGUI implements ActionListener 
{ 

//Welcome Screen 
JPanel welcomeP, welcomeImageP, portraitP, landscapeP, backP; 
JLabel welcomeImageL; 
JButton portraitB, landscapeB, backB; 

//Portrait Screen 
JTabbedPane tabbedPane; 
JPanel portraitOne; 
JLabel test; 

public JFrame frame; 

//Colours 
int colourOne = Integer.parseInt("c1c7f9", 16); 
Color Blue = new Color(colourOne); 


public birthdayCardGUI() throws Exception 
{ 
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 

    JFrame frame = new JFrame("birthday Card Maker!"); 
    frame.setExtendedState(frame.NORMAL); 

    frame.getContentPane().add(create_Content_Pane()); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(500, 700); //Size of main window 
    frame.setVisible(true); 

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 

    //sets frame location 
    int fw = frame.getSize().width; 
    int fh = frame.getSize().height; 
    int fx = (dim.width-fw)/2; 
    int fy = (dim.height-fh)/2; 

    //moves the frame 
    frame.setLocation(fx, fy); 
} 

public JPanel create_Content_Pane() throws Exception 
{ 
    JPanel TotalGUI = new JPanel(); 
    //TotalGUI.setBackground(Blue); 
    TotalGUI.setLayout(null); 

    //Welcome Panel 
    welcomeP = new JPanel(); 
    Border etched = BorderFactory.createBevelBorder(10); 
    Border titled = BorderFactory.createTitledBorder(etched, "Welcome"); 
    welcomeP.setBorder(titled); 
    welcomeP.setLayout(null); 
    welcomeP.setLocation(0,0); 
    welcomeP.setSize(485, 680); 
    welcomeP.setBackground(Blue); 
    TotalGUI.add(welcomeP); 

    welcomeImageP = new JPanel(); 
    welcomeImageP.setLayout(null); 
    welcomeImageP.setLocation(88,20); 
    welcomeImageP.setSize(324, 225); 
    welcomeP.add(welcomeImageP); 

    String welcomeG = "Welcome Image.png"; 
    ImageIcon WelcomeG = new ImageIcon(welcomeG); 
    welcomeImageL = new JLabel(WelcomeG, JLabel.CENTER); 
    welcomeImageL.setSize(324, 225); 
    welcomeImageL.setLocation(0,0); 
    welcomeImageP.add(welcomeImageL); 

    portraitB = new JButton("Portrait"); 
    portraitB.setSize(100, 30); 
    portraitB.setLocation(200, 295); 
    portraitB.addActionListener(this); 
    welcomeP.add(portraitB); 

    landscapeB = new JButton("Landscape"); 
    landscapeB.setSize(100, 30); 
    landscapeB.setLocation(200, 335); 
    landscapeB.addActionListener(this); 
    welcomeP.add(landscapeB); 

    TotalGUI.setOpaque(true); 

    return TotalGUI; 

} 


public void create_Portrait_Pane() 
{ 
    PortraitGUI portrait = new PortraitGUI(); 
    getContentPane().removeAll(); 
    getContentPane().add(portrait.PortraitGUI); 
    getContentPane().doLayout(); 
    update(getGraphics()); 
} 

@Override 
public void actionPerformed(ActionEvent e) 
    { 
     if(e.getSource() == portraitB) 
     {    
      create_Portrait_Pane(); 
     } 
    } 

//MAIN METHOD 
public static void main(String[] args) throws Exception 
{ 
    birthdayCardGUI CGUI = new birthdayCardGUI(); 
    } 
} 

これは、新しいコンテンツペインを作成するPortraitGUIファイルです。

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

public class PortraitGUI extends JPanel implements ActionListener 
{ 
JPanel frontPageP; 
JLabel frontPageL; 

//Color White; 
int intValue = Integer.parseInt("FFFFFF", 16); 
Color White = new Color(intValue); 

public JPanel PortraitGUI() throws Exception 
{ 
    JPanel PortraitGUI = new JPanel(); 
    PortraitGUI.setLayout(null); 

    frontPageP = new JPanel(); 
    frontPageP.setBackground(White); 
    frontPageP.setSize(350, 400); 
    frontPageP.setLocation(20, 70); 
    PortraitGUI.add(frontPageP); 

    frontPageL = new JLabel("Front Page"); 
    frontPageL.setLocation(10, 5); 
    frontPageL.setSize(70, 30); 
    frontPageL.setHorizontalAlignment(JTextField.CENTER); 
    PortraitGUI.add(frontPageL);   

    PortraitGUI.setOpaque(true); 

    return PortraitGUI; 
} 

public void actionPerformed(ActionEvent e) 
{ 
} 

}

+0

ContentPaneはJPanelと同じで、基本的にはBorderlayout.CENTERでレイアウトされています。別のJPanelsを切り替える場合は、revalidate()+ repaint()を呼び出すことを忘れないでください。 – mKorbel

答えて

5

あり、あなたのコード内のいくつかの問題がありますが、あなたの主な問題の一つは、ヌルと非使用可能なクラスのフィールドを残して、あなたのコンストラクタでのJFrameクラスフィールドのあなたのシャドーイングから来ています。これを修正するには、この変数を再宣言しないでください。これに

JFrame frame = new JFrame("birthday Card Maker!"); 

// this uses the JFrame variable declared in the class. 
frame = new JFrame("birthday Card Maker!"); 

次に、あなたがのcon​​tentPaneの内容を入れ替える方法では、後に、この変数を使用することができます。

public void create_Portrait_Pane() throws Exception { 
     PortraitGUI portrait = new PortraitGUI(); 
     frame.getContentPane().removeAll(); // now you can use the frame variable 
     frame.getContentPane().add(portrait); 
     //!! getContentPane().doLayout(); 
     //!! update(getGraphics()); // WTF? 
     ((JPanel)frame.getContentPane()).revalidate(); 
     frame.repaint(); 
    } 

がこれを言ってもこのようにこれを変更します私自身、コンテナとしてCardLayoutを使用してビュー(他のJPanel)をスワップするJPanelを使用しています。

はまた、あなたはここで、「擬似」コンストラクタを持っているように見えます:

public JPanel PortraitGUI() throws Exception { 

理由だけではなく、本当のコンストラクタを使用していない?:

public PortraitGUI() throws Exception { 
     setLayout(null); 

     frontPageP = new JPanel(); 
     frontPageP.setBackground(White); 
     frontPageP.setSize(350, 400); 
     frontPageP.setLocation(20, 70); 
     add(frontPageP); 

     frontPageL = new JLabel("Front Page"); 
     frontPageL.setLocation(10, 5); 
     frontPageL.setSize(70, 30); 
     frontPageL.setHorizontalAlignment(JTextField.CENTER); 
     add(frontPageL); 

     setOpaque(true); 
    } 

も良いプログラミングの練習のためにあなたがしたいと思いますプレーンバニラのExceptionクラスを使用せず、代わりに特定の例外をスローまたはキャッチします。

次に、絶対的なサイズと位置を使用する習慣から抜け出し、その代わりにレイアウトマネージャを使って自分が最もよくやっていることをすることになります。

編集:あなたの最近のコメントへの

回答

私はそれがエラーを投げていたか、戻り値の型が必要で、

ので、公共 "のJPanel" PortraitGUIが使用理由より良い解決策は、それを真のコンストラクタにすることであり、戻り型にすることではありませんでした。

と私はcreate_Content_Pane()と同じクラスをコーディングしました。パネルを返す。また、戻り値のタイプに必要なエラーが数回発生しました。

また、間違ったことを修正するのではなく、エラーが発生している理由を知ることが重要です。

アップデート(getGraphics());私が同じ問題で見つけたコード例から試した方法でした。

確かに、それはSwingの例ではなく、より古いAWTの例に由来します。あなたはSwingでこの種のコーディングをしません。

+0

悪い習慣やアドバイス私の質問に答える。私がpublic "JPanel" PortraitGUIを使用した理由は、エラーまたは返される型が必要なためです。クラスをcreate_Content_Pane()と同じようにコーディングしました。パネルを返す。また、戻り値のタイプに必要なエラーが数回発生しました。アップデート(getGraphics());私が同じ問題で見つけたコード例から試した方法でした。 – Elizabeth

+0

@Elizabeth:答えを編集するをご覧ください。 –

関連する問題