2011-03-24 82 views
2

我希望創建一個簡單的Java應用程序,該應用程序應該在黃色框架上繪製2個方塊 - 紅色和藍色。簡單的父子Java應用程序

appln包含一個窗口(JFrame),它具有作爲子項添加的contentPane(JPanel)。我已經添加了2個格作爲子到JFrame

/** 
Squares' container 
------------------ 
This file contiains 2 small squares - red and blue. The user can drag 
these squares in the panel. If the user drags these squares out of the 
panel, the square is lost forever. 
*/ 

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

public class sqsCont { 
public static void main(String[] args) { 
    JFrame window = new JFrame(); 
    window.setVisible(true); 
    sqsContPanel myPanel = new sqsContPanel(); 
    window.setBounds(0,0,500,250); 
    window.getContentPane().add(myPanel); 
    window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);  
} 
} 

/** 
Contains 2 squares red and blue side-by-side on 
*/ 
class sqsContPanel extends JPanel{ 
mySquare redSq; 
mySquare blueSq; 

public sqsContPanel() { 
    //setBounds(0,0,500,250); 
    redSq = new mySquare(Color.RED); 
    blueSq = new mySquare(Color.BLUE); 
    add(redSq); 
    add(blueSq); 
} 

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    setBackground(Color.YELLOW); 
} 
} 

/** 
Squares 
*/ 
class mySquare extends JComponent{ 
int myWidth = 50; 
int myHeight = 50; 
Color myColor; 

/** 
    myColor = color of the square 
*/ 
public mySquare(Color myColor) { 
    this.myColor = myColor;  
    if(myColor == Color.RED) { 
     setBounds(10,10,10+myWidth,10+myHeight); 
    } else { 
     setBounds(20+myWidth,10,10+myWidth,10+myHeight); 
    } 
    setVisible(true); 
} 

public void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    if (myColor == Color.RED){ 
     setBackground(Color.RED); 
     g.setColor(Color.RED); 
     g.fillRect(10,10,10+myWidth,10+myHeight); 
    } else { 
     setBackground(Color.BLUE); 
     g.setColor(Color.BLUE); 
     g.fillRect(20+myWidth,10,10+myWidth,10+myHeight); 
    } 
} 
} 

代碼生成與黃色幀中的窗口。但是,正方形不可見。

任何人都可以發現我在這段代碼中缺少的東西,或者我應該以不同的方式讓這個應用程序正常工作?

感謝

回答

1

Swing和AWT使用佈局管理器來確定組件放置在容器中的位置(請參閱http://download.oracle.com/javase/tutorial/uiswing/layout/using.html)。在你的情況下,你似乎試圖通過調用setBounds手動放置組件。對於工作,你必須更換默認佈局管理器爲您的面板是這樣的:

public sqsContPanel() { 
    setLayout(null); 
    .... 

一旦你已經做了那麼你的方形部件將是您想要的地方。您將無法看到您的藍色方塊,因爲背景顏色永遠不會被繪製。這是因爲g.fillRect(...)使用當前組件的本地座標。你是這樣叫它:

 g.fillRect(20 + myWidth, 10, 10 + myWidth, 10 + myHeight); 

20 + myWidth = 70組件的寬度是60。因此沒有被繪製。您可以在mySquare類這個簡單的版本替換的paintComponent來解決這個問題:

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    g.setColor(myColor); 
    g.fillRect(0, 0, 10 + myWidth, 10 + myHeight); 
} 

樣式注意:在Java的慣例是所有類的名稱以大寫字母開頭。所以mySquare應該是MySquare。

+0

感謝您耐心解決我的問題:) – Edwards 2011-03-24 14:36:56

1

你應該盡一切改變AWT事件調度線程(EDT),包括添加成分在裏面GUI操作。

在你的情況,這很簡單:

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { public void run() { 
     JFrame window = new JFrame(); 
     window.setVisible(true); 
     sqsContPanel myPanel = new sqsContPanel(); 
     window.setBounds(0,0,500,250); 
     window.getContentPane().add(myPanel); 
     window.setDefaultCloseOperation(window.EXIT_ON_CLOSE); 
    }}); 
} 

順便說一句,我會先添加組件和設置,並僅作爲最後的操作使用setVisible(true)

+0

感謝Russ和Paulo。順便說一句,我現在理解flowlayout。爲此,我在MySquares類中刪除了[code] setbounds()[\ code]並添加了[code] setPreferredSize(new Dimension(myWidth,myHeight))[\ code](供將來參考) – Edwards 2011-03-27 02:32:44

+0

@Edwards:對於內聯代碼,使用反引號(''),那麼它將被格式化爲源代碼(而不是解釋)。 – 2011-03-27 02:42:01