简单的父子Java应用程序

时间:2011-03-24 13:38:29

标签: java swing

我希望创建一个简单的Java应用程序,它应该在黄色框架上绘制2个正方形 - 红色和蓝色。

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);   
    }
}
}

代码生成带黄色框的窗口。但是,方块不可见。

任何人都可以发现我在此代码中缺少的内容,或者我应该采取哪些不同的方式让此appln正常工作?

由于

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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。