java - 简单的父子 Java 应用程序

标签 java swing

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

应用程序包含一个窗口 (JFrame),该窗口 (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);   
    }
}
}

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

谁能发现我在这段代码中遗漏了什么,或者我应该做些什么才能让这个应用程序正常工作?

谢谢

最佳答案

Swing 和 AWT 使用布局管理器来确定组件在容器中的放置位置(参见 http://download.oracle.com/javase/tutorial/uiswing/layout/using.html)。在您的情况下,您似乎试图通过调用 setBounds 手动放置组件。为此,您必须像这样替换面板的默认布局管理器:

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

完成后,您的方形组件就会出现在您想要的位置。你将无法看到你的蓝色方 block ,因为背景颜色从未被绘制过。这是因为 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。

关于java - 简单的父子 Java 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5420071/

相关文章:

java - 将 JPanel 导出到图像

来自 unix 机器的 Java Swing ComboBox 文件列表

Java 检查空对象意外行为

java - 通用堆栈实现

java - 字符串格式中的自动字段编号

java - 根据单元格内容为单个 JTable 单元格着色?

Java游戏。子弹不会直接从 Guzzle 飞出

来自数据库的 Java JTable 结果

java - 使用 jackson 转换 Java 对象时如何忽略可选属性

java - 添加有关新主机的信息