简体   繁体   English

小程序和数组(使按钮可见)

[英]Applets and Arrays (Making buttons visible)

Working on this project, and first off, I need to implement a 600x600 pixel JApplet that is a 10x10 grid (with horizontal and vertical gap of 4 pixels) of 100 JButtons. 在这个项目上,首先,我需要实现一个600x600像素的JApplet,它是一个100x JButtons的10x10网格(水平和垂直间隙为4像素)。 The program starts out with all buttons hidden except for one, and certain buttons are uncovered with every button click. 该程序以隐藏的所有按钮(一个按钮除外)开始,并且每次单击按钮都会发现某些按钮。 Every click also causes the background color to change and the label of the button to change. 每次单击也会导致背景颜色更改和按钮标签更改。 When all buttons are uncovered the background color changes to black and remains black until the user closes the applet. 揭开所有按钮后,背景颜色变为黑色,并保持黑色,直到用户关闭小程序为止。

The buttons are to be saved in a 10x10 array of type JButton. 这些按钮将保存在JButton类型的10x10数组中。 Buttons have a label of a 2-digit number that contains the row and column number, ie the button in the top left corner at row zero column zero is labeled 00. 按钮具有2位数字的标签,其中包含行号和列号,即位于行零,列零的左上角的按钮标记为00。

Pick 5 colors (other than black) you wish to use for your background. 选择您想用于背景的5种颜色(黑色除外)。

When the applet is started, the background is a neutral color and only the 00 button is visible. 小程序启动时,背景为中性色,只有00按钮可见。

I am having trouble making my 100 buttons from 00-99 in the 10x10 grid visible. 我在10x10网格中无法看到00-99的100个按钮。 I feel like my for-loops may be incorrect, and that is why the buttons aren't being created properly.: 我觉得我的for循环可能不正确,这就是为什么不能正确创建按钮的原因:

public class Project5 extends JApplet implements ActionListener {

    JButton button;
    Container contentPane;

    public void init() {

        setSize(600, 600);
        contentPane = getContentPane();
        contentPane.setBackground(Color.WHITE);

        GridLayout grid = new GridLayout(10,10);
        grid.setVgap(4);

        JButton[][] btns = new JButton[10][10];

        for (int i = 0; i < 10; i++) {

            for (int j = 0; j < 10; j++) {

                button = new JButton();
                button.addActionListener(this);
                contentPane.add(button);
                button.setVisible(true);

            }
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Container contentPane = getContentPane();
        button.setVisible(false);

        int clicks = 0;
        clicks++;

        if (clicks % 5 == 0) {
            contentPane.setBackground(Color.PINK);
        } else if (clicks % 5 == 1) {
            contentPane.setBackground(Color.GREEN);
        } else if (clicks % 5 == 2) {
            contentPane.setBackground(Color.BLUE);
        } else if (clicks % 5 == 3) {
            contentPane.setBackground(Color.YELLOW);
        } else if (clicks % 5 == 4) {
            contentPane.setBackground(Color.RED);
        }

    }

    public void checkDone() {
        //if all buttons visible, change contentPane color to black.

    }

}

You create a layout manager, a GridLayout, but never use it to set any of the layouts of your components. 您创建了一个布局管理器GridLayout,但是从不使用它来设置组件的任何布局。 You should do this, call setLayout(...) and pass in your manager. 您应该执行此操作,调用setLayout(...)并传入您的管理器。 Otherwise your contentPane will use the default BorderLayout, and when a component is added to a BorderLayout using container in a default way, it covers up any other components added in the same way. 否则,您的contentPane将使用默认的BorderLayout,并且当以默认方式使用容器将组件添加到BorderLayout时,它将覆盖以相同方式添加的所有其他组件。 Eventually only the last added component shows. 最终仅显示最后添加的组件。


Edit : Other problems 编辑 :其他问题

Here: 这里:

Container contentPane = getContentPane();
button.setVisible(false);

You're using a button variable and setting it invisible, but what button do you mean? 您正在使用按钮变量并将其设置为不可见,但是什么意思呢? The one pressed? 一个按下? This certainly isn't it. 当然不是。 If that's the one you want, then you should call getSource() on your ActionEvent parameter, e. 如果那是您想要的,则应在ActionEvent参数上调用getSource()

And then the next line down: 然后下一行:

int clicks = 0;
clicks++;

This will guarantee that clicks will always equal 1 and nothing but 1. Not too useful that. 这样可以确保点击次数始终等于1,除了等于1之外别无其他。


Also, you create an array of JButtons, btns, but do nothing with it. 另外,您将创建一个JButtons,btns数组,但不对其进行任何处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM