I'm asking you a question because I got an execution error when I pressed the button.

Asked 1 years ago, Updated 1 years ago, 82 views

package studyfree;

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

    class Gugudan extends Frame implements WindowListener, ActionListener{

        @Override
        public void actionPerformed(ActionEvent arg0) {
            Object c = arg0.getSource();
            for (int a=0; a<9; a++) {
                if (c == btns[a]) {
                    for(int b=1;b<10;b++) {
                        lb1.setText(btns[a]+"*"+b); //Error appears here.
                    }
                }
            }
        }
        @Override
        public void windowActivated(WindowEvent e) {
            // // TODO Auto-generated method stub

        }

        @Override
        public void windowClosed(WindowEvent e) {
            // // TODO Auto-generated method stub

        }

        @Override
        public void windowClosing(WindowEvent e) {
            // // TODO Auto-generated method stub
            System.exit(0);
        }

        @Override
        public void windowDeactivated(WindowEvent e) {
            // // TODO Auto-generated method stub

        }

        @Override
        public void windowDeiconified(WindowEvent e) {
            // // TODO Auto-generated method stub

        }

        @Override
        public void windowIconified(WindowEvent e) {
            // // TODO Auto-generated method stub

        }

        @Override
        public void windowOpened(WindowEvent e) {
            // // TODO Auto-generated method stub

        }
        Button [] btns;
        Panel pa1,pa2;
        Label lb1;
        Gugudan(String str){
            super(str);


            pa1 = new Panel();
            pa1.setLayout(new GridLayout(3,3));
            pa1.setSize(500, 250);

            pa2 = new Panel();
            pa2.setSize(500, 250);

            Label b1 = new Label ("Select the desired end") ");
            btns = new Button[11];

            for (int a=0;a<9;a++) {
                btns[a] = new Button (a+1+"but"); // Put button name as number in button
                btns[a].addActionListener(this); // to act on each number button
                pa1.add(btns[a]); // Add buttons created in panel 1
            }

            add("South",pa1); // Create panel 1 below 
            add("North",pa2); // Create panel 2 on top
            pa2.add(lb1);

            This.addWindowListener(this); //close window
            this.setSize(500,500); // window size
            This.setVisable(true); // show window
        }
    }

public class Test1 {

    public static void main(String[] args) {
        Gugudan gu = new Gugudan ("Old Club Output").");

    }
}

java awt

2022-09-22 18:05

1 Answers

If an error occurs, please register the error message and provide the program description. That way, we can quickly grasp the content from the perspective of viewing it and provide something more helpful.

Labelb1 = new Label("Choose the desired tier) ");

Modify the above line as follows.

lb1 = new Label ("Select the desired end") ");

And you seem to be misunderstanding the Label widget. The error will be solved, but I don't think the multiplication table will be displayed as you want.


2022-09-22 18:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.