2011-07-20 87 views
0

儘管有一些提示,我仍然得到這一個錯誤。我最終得到一個基本窗口和另一個具有額外功能的基本窗口,但沒有前一窗口中的基本窗口。相反,我想要一個結合了基本功能和新功能的新窗口。這裏是我有代碼:(你也奉勸哪些方法?)
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(); 

} 

} 

現在被「擴展」是指一個:

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

我不太清楚你的問題是什麼,但你WindowTemplate基本上一個JFrame,讓你不想在你的構造函數來創建一個新的JFrame而是「適用」的方法,而不是thismyFrame。 嘗試是這樣的:

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,它的構造函數會調用超級構造,這是WindowTemplate這將反過來調用JFrame構造

0

如果我正確地讀出你的代碼,WindowTemplate是一個JFrame。在WindowTemplate的構造函數中實例化另一個JFrame?這沒有多大意義。相反,創建和配置myFrame的,你應該配置this

public WindowTemplate() { 

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

因爲你是在子類中做這件事,你在哪裏調用this.add(area)

2

首先,雖然你延長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秒。相反,您創建兩個單獨的JFrame,每個構造函數中有一個。

我會建議避免過深層次的Swing組件的情況下,因爲你會遇到許多意想不到的佈局問題,對於一個什麼作品,不爲別的,當添加更多的組件工作。

+0

我修改了你的代碼,如果不正確則恢復更改+1 – mKorbel

+0

關於你的建議 - 我將有幾個類似的窗口,我打算使用CardLayout。這些組件將是文本字段和區域,按鈕,單選按鈕等等(非常基本的東西)。在這種情況下會推薦什麼設計? – Hurdler

+0

對於卡布局,您可以擴展JPanel,而不是JFrame,除了真正的基本配置外,我不會創建層次結構,除非每次創建完整面板都是一個大問題。如果可能的話,我會在主JFrame中添加一些常用組件,並將所有面板都放在其他面板中,但這取決於您的需求。 – MByD

1

我想在你的第二個例子,在WindowTemplate,你創建另一個JFrame的,當你認爲你初始化this的JFrame。取而代之的

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

你可能想

super("My first window"); 

,然後用this