繁体   English   中英

JButton和ActionEvent的问题

[英]Issues with JButtons and ActionEvent

出于某种原因,当我单击按钮时,它会进入actionPerformed方法,但是程序无法将任何按钮与actionevent匹配,因此,当按下任何按钮时,任何操作均无效。 我从paintcomponent中取出了代码,并将其放入了构造函数中,但是现在按钮不显示了。 有人可以帮忙吗?

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

//

public class game extends JFrame//header
{
        private CardPanel mp;
        private Font big = new Font("Verdana", Font.BOLD, 100);
        private Font small = new Font("Verdana", Font.BOLD, 90);
        private Font norm = new Font("Verdana", Font.PLAIN, 23);
        private CardLayout cards;
        private JButton back;
        private boolean clairewang=false;
        private boolean wangfam=false;
        private boolean cl=false;
        private JButton next;
        private JButton instruction;
        private JButton startb;
         private JButton done;
        private JTextArea title;
        private Color c=new Color(186,225,200);
        private int a=0;;
        private JButton yes;
        private JButton no;
        private int x;
        private int y;
        private JButton claire= new JButton("PLAY!");
        private JButton fambam=new JButton("PLAY!");
        private JButton sickbob=new JButton("PLAY!");
        private String pc="Play as Claire (medium)- an able bodied 20 yearold";
        private String pfb="Play as a family (hard)- receive more waterper week.";
        private String psb="Play as Bob (easy)- he is connected to thecity's water" ;
       private String psb2="supply because he has brain cancer.";



        public static void main(String [] args)
        {
                game b = new game();//create constructor
        }
        public game()//Bio constructor
        {
           //set everything for JFrame
           setSize(900,600);
           setDefaultCloseOperation(DISPOSE_ON_CLOSE);
           setLocation(0,0);
           setResizable(false);//set to false
           mp = new CardPanel();
           cards = new CardLayout();
           back = new JButton("BACK");
           done= new JButton("DONE");

          next = new JButton("NEXT");
                                                next.setBounds(275,200, 315, 220);


           Start beg = new Start();

           instruct1 inst1 = new instruct1();
           instruct2 inst2 = new instruct2();
           choosepanel ch = new choosepanel();
           sb1 sb= new sb1();
           fb1 f1= new fb1();//FINISH THIS
           fb2 f2= new fb2();
           fb3 f3= new fb3();
           fb4 f4= new fb4();
           c1 s= new c1();

           mp.setLayout(cards);
           mp.add(beg,"start");

           mp.add(inst1,"i1");
           mp.add(inst2, "i2");
           mp.add(ch,"choose");
                        mp.add(s, "c1");
                        mp.add(sb, "sb");
                        mp.add(f1, "fb1");
                        mp.add(f2, "fb2");
                        mp.add(f3, "fb3");
                        mp.add(f4, "fb4");
           setContentPane(mp);
           setVisible(true);
        }

        class CardPanel extends JPanel
        {
                public CardPanel()
                {
                }
        }
        class Start extends JPanel implements ActionListener
        {

                public Start()
                {
                    setLayout(null);

                    startb = new JButton("START");
                    startb.setBounds(255,200, 315, 100);

                    instruction = new JButton("INSTRUCTIONS");
                    instruction.setBounds(255,320, 315, 100);


                                                //make text area uneditable
                     instruction.addActionListener(this);
                    startb.addActionListener(this);
                     add(startb);
                    add(instruction);
                }
                public void paintComponent(Graphics g)
                {
                       setBackground(c);
                                                super.paintComponent(g); //import image
                                                setFont(big);
                                                g.setColor(Color.WHITE);
                                                g.drawString("THIRST", 230,150);
                                        //      g.setColor(c);
                                        //      g.drawString("THIRST",235,153);
                                        //      g.setColor(Color.WHITE);//have loop that draws waves

                                for( y=0;y<600;y+=200){
                                        for(x=0;x<800;x+=100){
                                                g.drawArc(x,y, 100,50,0,-180);
                                                }
                                }
                                        for(y=100;y<600;y+=200){
                                        for(x=-50;x<800;x+=100){
                                                g.drawArc(x,y, 100,50,0,-180);
                                                }
                                        }


                                //for( y=0; y<600;x+=100){
                                //              for(x=0; x<800;x+=50){
                                        //                      g.drawArc(x, y, 50,50,0,-180);
                                        //      }
                                //}

                }

                public void actionPerformed(ActionEvent evt)
                {
                                         if (evt.getSource()==instruction) {
                                                         cards.show(mp,"i1"); }
                                        else if(evt.getSource()==startb){
                                                cards.show(mp, "choose");
                                        }

                }
        }
       /* class instruct extends JPanel implements ActionListener
        {//header

                public instruct()
                {
                                                setLayout(null);


                                                next = new JButton("NEXT");
                                                next.setBounds(600,500,100,50);
                                                //instruction.setBounds(225,500, 275, 575);
                        back = new JButton("BACK");
                                                back.setBounds(100,500,100,50);
                     //   instruction.setBounds(225,500, 375, 575);
                      next.addActionListener(this);
                      back.addActionListener(this);
                                                add(next);

                        add(back);


                }
                public void paintComponent(Graphics g){
                        setBackground(c);
                        super.paintComponent(g);
                        setFont(big);
                                                g.setColor(Color.WHITE);
                                                g.drawString("THING0", 230,150);

                        //draw the instructions in the center using blackor white

                }
                public void actionPerformed(ActionEvent evt){

                       //  if(((JButton)(evt.getSource()).getText()).equals("BACK"))

                                                //       cards.show(mp,"start");
                                                //       System.out.println("X");
                                                //       }
                                                //if ((((JButton)(evt.getSource()).getText()).equals("NEXT")) ){
                                                         //cards.show(mp,"i1");
                                                         //System.out.println("X");
                                                        //}

                                                        if (evt.getSource()==next ){
                                                         cards.show(mp,"i1");
                                                         System.out.println("X");
                                                        }
                                                        else if (evt.getSource()==back) {
                                                 cards.show(mp,"start");
                                                         System.out.println("X");
                                                         }



                }

                        } */

                        class instruct1 extends JPanel implements ActionListener{
                                public instruct1(){
                                        setLayout(null);
                                                next = new JButton("NEXT");
                                                next.setBounds(600,500,100,50);
                                                //instruction.setBounds(225,500, 275, 575);
                        back = new JButton("BACK");
                                                back.setBounds(100,500,100,50);
                                                next.addActionListener(this);
                                                back.addActionListener(this);
                                                add(next);
                                                add(back);
                                        }
                                public void paintComponent(Graphics g){
                                                setBackground(c);
                                                super.paintComponent(g); //import image
                                                setFont(big);
                                                g.setColor(Color.WHITE);
                                                g.drawString("THING1", 230,150);
                                        }
                                                public void actionPerformed(ActionEvent evt){
                         if (evt.getSource()==back) {
                                                         cards.show(mp,"start"); }
                                                else if(evt.getSource()==next){
                                                         cards.show(mp,"i2");
                                                        }

        }

                                }
                        class instruct2 extends JPanel implements ActionListener{
                                public instruct2(){
                                setLayout(null);


                                                done = new JButton("DONE");
                                                back = new JButton("BACK");

                                                back.addActionListener(this);
                                                done.addActionListener(this);

                                                done.setBounds(600,500,100,50);
                                                //instruction.setBounds(225,500, 275, 575);

                                                back.setBounds(100,500,100,50);
                                                add(done);
                                                add(back);

                                        }
                                public void paintComponent(Graphics g){

                                                setBackground(c);
                                                super.paintComponent(g); //import image
                                                setFont(big);
                                                g.setColor(Color.WHITE);
                                                g.drawString("THING2", 230,150);
                                        }
                                public void actionPerformed(ActionEvent evt){
                                             if (evt.getSource()==back) {
                                                cards.show(mp,"i1"); }
                                                else if(evt.getSource()==done){
                                                 cards.show(mp,"start");
                                                        }

        }
        }

       class choosepanel extends JPanel implements ActionListener{
                        public choosepanel(){
                                setLayout(null);
                                sickbob.addActionListener(this);
                                fambam.addActionListener(this);
                                claire.addActionListener(this);
                                sickbob.setBounds(600,200,100,50);
                                claire.setBounds(600,300,100,50);
                                fambam.setBounds(600,400,100,50);
                                add(sickbob);
                                add(claire);
                                add(fambam);

                                                //instruction.setBounds(225,500, 275, 575);

                        }
                        public void paintComponent(Graphics g){
                                setBackground(c);
                                super.paintComponent(g);
                        setFont(small);
                                                g.setColor(Color.WHITE);
                                                g.drawString("CHOOSE PLAYER", 0,150);
                                g.setFont(norm);
                                g.drawString(psb,50,225);
                                g.drawString(psb2,50,250);
                                g.drawString(pc,50,325);
                                g.drawString(pfb,50,425);

                        }
                        public void actionPerformed(ActionEvent evt){
                        if(evt.getSource()==claire){
                                cards.show(mp, "c1");
                                clairewang=true;
                            }

                        else if(evt.getSource()==sickbob){
                                cards.show(mp, "c1");
                                cl=true;}
                        else if(evt.getSource()==fambam){
                                cards.show(mp, "c1");
                                wangfam=true;
                        }

                        }
                      /*  public boolean stuff(){
                            if(wangfam) return wangfam;
                            else if(cl)return cl;
                            else if(clairewang)return clairewang;
                        } */


        }
        class c1 extends JPanel implements ActionListener{
                public void c1(){
                    /* setLayout(null);
                    next=new JButton("NEXT");
                    no=new JButton("NO");
                    yes=new JButton("YES");
                     yes.addActionListener(this);
                     no.addActionListener(this);
                    next.addActionListener(this);
                    yes.setBounds(100,200,100,50);
                    no.setBounds(300,300,100,50);
                    next.setBounds(700,400,100,50);
                    add(yes);
                    add(no);
                    add(next); */
                     next=new JButton("NEXT");
                    no=new JButton("NO");
                    yes=new JButton("YES");
                     yes.addActionListener(this);
                     no.addActionListener(this);
                    next.addActionListener(this);
                    yes.setBounds(100,200,100,50);
                    no.setBounds(300,200,100,50);
                    next.setBounds(700,500,100,50);
                    add(yes);
                    add(no);
                    add(next);


                }
                public void paintComponent(Graphics g){
                        setBackground(c);
                                super.paintComponent(g);
                                setLayout(null);

                        setFont(norm);
                                                g.setColor(Color.WHITE);
                                                g.drawString("Your flowers are wilting. Water your lawn?",20,100);
                                                //print situation yn
                                                //jbutton yes and j button no

                                        g.fillRect(700, 75, 100, 400);//draw the bar that shows how much wateris left
                                        g.fillArc(700,65,100,20,0,180);
                                        g.fillArc(700,465,100,20,0,-180);
                                        g.setColor(c);
                                        g.fillRect(705,82, 90,390);
                                        g.fillArc(705, 463, 90, 18, 0 , -180);
                                        g.setColor(Color.WHITE);
                                        g.fillRect(705,82, 90,a);
                                       // if(clairewang)System.out.println(a);
                                         if(wangfam)System.out.println("wangfam");
                                        else if(cl)System.out.println("cl");



                }
                public void actionPerformed(ActionEvent evt){
                    System.out.println("stuff");
                        if(evt.getSource()==yes){
                                if(clairewang){
                                    a=15;
                                    System.out.println(a);}
                                else if(wangfam)a+=20;
                                else if(cl)a+=10;
                                //clairewang=true;
                                repaint();

                            }

                        else if(evt.getSource()==next){
                                cards.show(mp, "sb");
                               // cl=true;}

                        //yes gives an int a value
                        //no stays 0
                }
            }
        }


}

将创建和初始化代码从paintComponent()中移出到构造函数中

编码

            next=new JButton("NEXT");
            no=new JButton("NO");
            yes=new JButton("YES");
             yes.addActionListener(this);
             no.addActionListener(this);
            next.addActionListener(this);
            yes.setBounds(100,200,100,50);
            no.setBounds(300,200,100,50);
            next.setBounds(700,500,100,50);
            add(yes);
            add(no);
            add(next);

实际上,在每次重画时,它都会重新创建按钮并读取它们。 因此,在actionPerformed()中,按钮状态被更改(以显示按下状态),并且按钮被重新创建。 当然,它!=单击了一个。

暂无
暂无

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

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