复选框不显示 [英] Checkboxes not showing up

查看:244
本文介绍了复选框不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望另一套眼睛可以帮助我找到我的代码错误。我可以编译和运行程序,但我得到的是一个白色的屏幕。它应该显示复选框,并更改背景,无论选择哪种颜色。欢迎任何帮助!

I am hoping another set of eyes can help me find where my code is wrong. I can compile and run the program, but all I get is a white screen. It's supposed to show the checkboxes and change the background to whichever color is chosen. Any help is appreciated!

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

public class Chapter5Debug extends Frame implements ItemListener
{
static Chapter5Debug f = new Chapter5Debug();

CheckboxGroup options = new CheckboxGroup();
    Checkbox blue = new Checkbox("Blue",false,options);
    Checkbox red = new Checkbox("Red",false,options);
    Checkbox yellow = new Checkbox("Yellow",false,options);
    Checkbox pink = new Checkbox("Pink",false,options);
    Checkbox gray = new Checkbox("Gray",false,options);


public void Chapter5Debug()
{
    this.setLayout(new FlowLayout());
    add(blue);
    add(red);
    add(yellow);
    add(pink);
    add(gray);
    blue.addItemListener(this);
    red.addItemListener(this);
    yellow.addItemListener(this);
    pink.addItemListener(this);
    gray.addItemListener(this);

    //overriding windowClosing() allows user to click Close button
    addWindowListener(
        new WindowAdapter()
        {
           public void windowClosing(WindowEvent e)
           {
                System.exit(0);
           }
        }
    );

} //end of constructor method

public static void main(String[] args)
{
    //ColorButtons f = new ColorButtons();
    f.setBounds(200,200,500,100);
    f.setTitle("What's My Color?");
    f.setVisible(true);
} //end of main

public void actionPerformed(ActionEvent e)
{
    if (blue.getState()) f.setBackground(Color.blue);
    else if (red.getState()) f.setBackground(Color.red);
    else if (yellow.getState()) f.setBackground(Color.yellow);
    else if (pink.getState()) f.setBackground(Color.pink);
    else if (gray.getState()) f.setBackground(Color.gray);
} //end of actionPerformed method

public void itemStateChanged(ItemEvent e)
{
}

} //end of class


推荐答案

这不是一个构造函数:

public void Chapter5Debug()
{

这是一个 void 方法。你想要一个构造函数?删除 void 关键字。

it's a void method. You want a constructor? Remove the void keyword.

>

In this line

static Chapter5Debug f = new Chapter5Debug();

您正在调用默认构造函数, void 方法你从来没有调用过。

you are calling the default constructor, the void method you have is never called.

这篇关于复选框不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆