2011-07-20 18 views
0

いくつかのヒントにもかかわらず、私はまだこの1つを間違っています。私は1つの基本的なウィンドウと別の機能を備えた別のもので終わりますが、前のウィンドウの基本的なものはありません。代わりに、基本と新機能を組み合わせた新しいウィンドウが1つ必要です。 (また、あなたがどのアプローチを助言する?)
Javaの継承またはGUIが間違っている

package windows; 

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

public abstract class WindowTemplate extends JFrame { 

/** 
* Create the GUI and show it. For thread safety, this method should be 
* invoked from the event-dispatching thread. 
*/ 
public WindowTemplate() { 

JFrame myFrame = new JFrame("My first window"); 
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
myFrame.setVisible(true); 
myFrame.setSize(550, 450); 
myFrame.setLocationRelativeTo(null); 

// JLabel emptyLabel = new JLabel(""); 
// emptyLabel.setPreferredSize(new Dimension(550, 450)); 

// myFrame.getContentPane().setLayout(new CardLayout()); 
// myFrame.getContentPane().add(emptyLabel, BorderLayout.CENTER); 

// myFrame.pack(); 

} 

} 

を今「拡張」することを意図している1:

package windows; 

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

public class a_Welcome extends WindowTemplate { 

public a_Welcome() { 

JPanel area = new JPanel(); 

JLabel text = new JLabel("One line another line and another line"); // , JLabel.CENTER); 

// text.setBounds(80, 400, 400, 50); 
add(area); 

// area.setLayout(null); 
area.add(text, new CardLayout()); 

// area.add(text); // , BorderLayout.CENTER); 

Font font = new Font("SansSerif", Font.BOLD, 30); 
text.setFont(font); 
text.setForeground(Color.green); 
area.setBackground(Color.darkGray); 
area.setSize(550, 450); 

} 

} 

// timer-after 5 seconds-go to the next window (countdown in the bottom right corner) 

とメインを:ここで私が持っているコードがあります

package windows; 

public class Launcher { 

public static void main(String[] args) { 

// Schedule a job for the event-dispatching thread: 
// creating and showing this application's GUI. 
javax.swing.SwingUtilities.invokeLater(new Runnable() { 
    public void run() { 

     // WindowTemplate.createWindow(); 
     // a_Welcome.createWindow(); 

     a_Welcome window = new a_Welcome(); 
     window.setVisible(true); 
    } 
}); 

} 

} 



- 代わり - コードとあなたの助けに感謝の多くのために申し訳ありません

public class WindowTemplate extends JFrame { 

// Constructor 
public WindowTemplate() { 
    init(); 
} 

public void init() { 
    // add basic components 
    JFrame myFrame = new JFrame("My first window"); 
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    myFrame.setVisible(true); 
    myFrame.setSize(550, 450); 
    myFrame.setLocationRelativeTo(null); 
} 
} 

public class a_Welcome extends WindowTemplate { 

public a_Welcome() { 
    super(); 
} 

@Override 
public void init() { 
    super.init(); // important so you get the base stuff 
    // add other components 
    JPanel area = new JPanel(); 
    JLabel text = new JLabel("One line another line and another line"); 
    add(area); 
    area.add(text, new CardLayout()); 

    Font font = new Font("SansSerif", Font.BOLD, 30); 
    text.setFont(font); 
    text.setForeground(Color.green); 
    area.setBackground(Color.darkGray); 
    area.setSize(550, 450); 

} 
} 

+2

'a_Welcome'は関係なく、言語の慣用的なJavaとひどいクラス名ではありませんし、その後、任意のMyFrameと参照を交換してください。 –

+0

はい、私はそれを知っています。しかし、それは最初のウィンドウであり、 "a_"ビットがそれがトップに留まることを確かめるので、私の人生は楽になります。 「ウィンドウ」を書く方法ではないことを確認してください。明らかに、私はそれを適切に変更します。 – Hurdler

+0

「何の上に」とどまる? –

答えて

3

私はあなたの問題が何であるかはかなりわからないんだけど、あなたはあなたのコンストラクタで新しいJFrameのを作成したいのではなくthisにそれらの方法を「適用」の代わりにしていないので、基本的に、あなたのWindowTemplateは、JFrameですmyFrame。このような何かを試してみてください は:

public WindowTemplate() 
{ 
    super("My first window"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // "this" is optional 
    setVisible(true); 
    setSize(550, 450); 
    setLocationRelativeTo(null); 
} 

public a_Welcome() 
{ 
    super(); // this is implicit, because a_Welcome extends WindowTemplate, which has got a constructor without parameters 
    //[add more stuff here] 

} 

新しいa_Welcomeを作成している、そのコンストラクタは、順番にJFrameコンストラクタを呼び出しますWindowTemplateあるスーパーコンストラクタを呼び出します

0

あなたのコードを正しく読んだら、WindowTemplateはJFrameです。また、WindowTemplateのコンストラクタ内で別のJFrameをインスタンス化しますか?これはあまり意味がありません。代わりにmyFrameを作成および構成のあなたがthis.add(area)

2

まずを呼び出しているサブクラスでそれをやっているとして、あなたはthis

public WindowTemplate() { 

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    ... 
} 

を設定する必要があり、あなたはJFrameのを拡張していますが、新しいJFrameのと使用を作成しますそれは各コンストラクタにあります。それはそのように見ている必要があります。

public WindowTemplate() { 

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setLocationRelativeTo(null); 
    this.setPreferredSize(new Dimension(550, 450)); 
    this.pack(); 
    this.setVisible(true); 

を今、あなたはそのようにそれを行う場合は、各コンストラクタ/ initメソッドでJComponent秒を追加することができます。代わりに、2つの別々のJFrameを各コンストラクタに1つずつ作成します。

多くのコンポーネントを追加すると、予期しないレイアウトの問題に直面するので、スイングコンポーネントの場合は階層が深すぎるようにすることをおすすめします。

+0

あなたのコードを修正し、正しくない場合は変更を元に戻してください。+1 – mKorbel

+0

あなたのお勧めについて - 私はいくつかの類似したタイプのウィンドウを持ち、CardLayoutを使用する予定です。コンポーネントは、テキストフィールドとエリア、ボタン、ラジオボタンなど(かなり基本的なもの)です。この場合、どのようなデザインが推奨されますか? – Hurdler

+0

カードレイアウトの場合、JFrameではなくJPanelを拡張することができます。本当に基本的な設定を除いて、毎回フルパネルを作成するのが本当に大きな問題でない限り、階層を作成しません。可能であれば、いくつかの共通コンポーネントをメインのJFrameに追加し、他のパネルの中にすべてのパネルを配置しますが、それは実際のニーズにかかっています。 – MByD

1

WindowTemplateの2番目の例では、this JFrameを初期化していると思うと、別のJFrameを作成していると思います。

代わりの

JFrame myFrame = new JFrame("My first window"); 

あなたはおそらく

super("My first window"); 

をしたいし、その後this

関連する問題