简体   繁体   中英

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. 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. 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.

Pick 5 colors (other than black) you wish to use for your background.

When the applet is started, the background is a neutral color and only the 00 button is visible.

I am having trouble making my 100 buttons from 00-99 in the 10x10 grid visible. I feel like my for-loops may be incorrect, and that is why the buttons aren't being created properly.:

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. You should do this, call setLayout(...) and pass in your manager. 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. 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.

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.


Also, you create an array of JButtons, btns, but do nothing with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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