JButtons无法正确显示borderlayout,也会将actionListener添加到按钮

时间:2014-06-20 11:02:40

标签: java swing events jbutton actionlistener

我在这里遇到了2个相同代码的问题,首先,由于某种原因,JButton没有出现在具有当前配置的GUI上,但是代码运行了(调试器出现在那行代码中) )。我似乎无法弄清楚问题是什么。

我遇到的第二个问题是JButton.addActionListener(this)不喜欢工作,我需要actionListener来激活不同类中的事件,这样我就无法执行匿名actionListener。

以下是代码:

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

public class PPP extends JFrame{  
    static digit atp_hundred = new digit();  //Amount to pay
    static digit atp_ten = new digit();
    static digit atp_unit = new digit();
    static digit atp_tenth = new digit();
    static digit atp_hundreth = new digit(); 
    static digit ltr_ten = new digit();     //Litres
    static digit ltr_unit = new digit();
    static digit ltr_tenth = new digit();
    static digit ppl_hundred = new digit(); //Pence per litre
    static digit ppl_ten = new digit();
    static digit ppl_unit = new digit();
    static digit ppl_tenth = new digit();
    static PPP frame = new PPP();   

    public static void init() {           
        frame.setSize(1280,800);
        frame.setLayout(new BorderLayout());
        JPanel mainPanel = new mainPanel();
        mainPanel.setLayout(new GridLayout(2,2));
        frame.getContentPane().add(mainPanel);
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                    System.exit(0);
                }
            });
        frame.setVisible(true);
    }

    public static void main(String arg[]){
        init();
        while (true) {
            for (int i = 0; i < 10; i++) {
                atp_hundred.value = i;
                atp_hundred.update();
                frame.getContentPane().repaint();
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                }
                frame.getContentPane().repaint();
                try {
                    Thread.sleep(10);
                } catch (Exception e) {
                }
            }
        }
    }

    public void refresh() {
        repaint();
    }

    public PPP(){
        super();
    }

    /*public void update(Graphics g) {
    paint(g);
    }*/

    public static void atpCreate(Graphics g, int x, int y) {
        atp_hundred.create(x, y);
        drawPolygons(g, atp_hundred);
        atp_ten.create(x+50,y);
        drawPolygons(g, atp_ten);
        atp_unit.create(x+100, y);
        drawPolygons(g, atp_unit);
        g.drawOval(x+145, y+60, 10,10);
        atp_tenth.create(x+160, y);
        drawPolygons(g, atp_tenth);
    }

    public static void ltrCreate(Graphics g, int x, int y) {
        ltr_ten.create(x+50, y);
        drawPolygons(g, ltr_ten);
        ltr_unit.create(x+100, y);
        drawPolygons(g, ltr_unit);
        g.drawOval(x+145, y+60, 10, 10);
        ltr_tenth.create(x+160, y);
        drawPolygons(g, ltr_tenth);
    }

    public static void pplCreate(Graphics g, int x, int y) {
        ppl_hundred.create(x, y);
        drawPolygons(g, ppl_hundred);
        ppl_ten.create(x+50,y);
        drawPolygons(g, ppl_ten);
        ppl_unit.create(x+100, y);
        drawPolygons(g, ppl_unit);
        g.drawOval(x+145, y+60, 10,10);
        ppl_tenth.create(x+160, y);
        drawPolygons(g, ppl_tenth);
    }

    public static void drawPolygons(Graphics g, digit d) {
        if (d.top) {
            g.setColor(Color.green);
            g.fillPolygon(d.ptop);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.ptop);
        }
        if (d.topleft) {
            g.setColor(Color.green);
            g.fillPolygon(d.ptopleft);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.ptopleft);
        }
        if (d.topright) {
            g.setColor(Color.green);
            g.fillPolygon(d.ptopright);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.ptopright);
        }
        if (d.mid) {
            g.setColor(Color.green);
            g.fillPolygon(d.pmid);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.pmid);
        }
        if (d.botleft) {
            g.setColor(Color.green);
            g.fillPolygon(d.pbotleft);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.pbotleft);
        }
        if (d.botright) {
            g.setColor(Color.green);
            g.fillPolygon(d.pbotright);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.pbotright);
        }
        if (d.bot) {
            g.setColor(Color.green);
            g.fillPolygon(d.pbot);
        } else {
            g.setColor(Color.black);
            g.drawPolygon(d.pbot);
        }  
        g.setColor(Color.black);
    }
}
class mainPanel extends JPanel {
    public mainPanel() {
        setPreferredSize(new Dimension(1280,800));
        JPanel customerPanel = new customerPanel();
        customerPanel.setLayout(new BorderLayout());
        add(customerPanel);
        //add(new customerButtons());
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setFont(new Font("Copperplate Gothic Light", Font.PLAIN, 30));
        String title = "Pete's Petrol Pump Simulation vAlpha0.1";
        g.setColor(Color.white);
        //g.clearRect(0,0,640,500);
        g.setColor(Color.black);
        g.drawString(title, (int)(640 - (g.getFontMetrics().getStringBounds(title, g).getWidth() / 2)), 75);
    }
}
class customerPanel extends JPanel {
    public customerPanel() {
        //setPreferredSize(new Dimension(640,500));
        //setPreferredSize(new Dimension(500,800));
        customerButtons buttons = new customerButtons();
        add(buttons, BorderLayout.SOUTH);
    }

    @Override
    public void paintComponent(Graphics g){
        //super.paintComponent(g);
        /*g.setFont(new Font("Copperplate Gothic Light", Font.PLAIN, 30));
        String title = "Pete's Petrol Pump Simulation vAlpha0.1";
        g.setColor(Color.white);
        g.clearRect(0,0,640,500);
        g.setColor(Color.black);
        g.drawString(title, (int)(640 - (g.getFontMetrics().getStringBounds(title, g).getWidth() / 2)), 75);*/
        g.drawRoundRect(20,100,600,250,5,5);
        g.setFont(new Font("Gill Sans MT", Font.PLAIN, 20));
        g.drawString("Amount to Pay: ", 40, 160);
        PPP.atpCreate(g, 200, 110);
        g.drawString("Litres Dispensed: ", 40, 240);
        PPP.ltrCreate(g, 200, 190);
        g.drawString("Pence per Litre: ", 40, 320);
        PPP.pplCreate(g, 200, 270);
        //System.out.println((g.getFontMetrics().getStringBounds("Amount to Pay", g).getWidth()));
    }
}
class customerButtons extends JPanel {
    public customerButtons() {
        JButton remove = new JButton("Remove nozzle");
        remove.setActionCommand("remove");
        //System.out.println("Hello");
        remove.addActionListener(this);
        remove.setToolTipText("Replace the fucking nozzle");
        JButton squeeze = new JButton("Squeeze nozzle");
        squeeze.setActionCommand("squeeze");
        squeeze.addActionListener(this);
        squeeze.setToolTipText("Squeeze the fucking nozzle");
        JButton stop = new JButton("Stop Squeeze");
        stop.setActionCommand("stop");
        stop.addActionListener(this);
        stop.setToolTipText("Stop squeezing the fucking nozzle");
        JButton replace = new JButton("Replace nozzle");
        replace.setActionCommand("replace");
        replace.addActionListener(this);
        replace.setToolTipText("Replace the fucking nozzle");
        add(remove);
        add(squeeze);
        add(stop);
        add(replace);

    }
}

class digit {
    int value = 0;
    boolean top = false;
    boolean topleft = false;
    boolean topright = false;
    boolean mid = false;
    boolean botleft = false;
    boolean botright = false;
    boolean bot = false;
    public void update() {
        switch(value) {
            case 0: top = true;
            topleft = true;
            topright = true;
            mid = false;
            botleft = true;
            botright = true;
            bot = true;
            break;
            case 1: top = false;
            topleft = false;
            topright = true;
            mid = false;
            botleft = false;
            botright = true;
            bot = false;
            break;
            case 2: top = true;
            topleft = false;
            topright = true;
            mid = true;
            botleft = true;
            botright = false;
            bot = true;
            break;
            case 3: top = true;
            topleft = false;
            topright = true;
            mid = true;
            botleft = false;
            botright = true;
            bot = true;
            break;
            case 4: top = false;
            topleft = true;
            topright = true;
            mid = true;
            botleft = false;
            botright = true;
            bot = false;
            break;
            case 5: top = true;
            topleft = true;
            topright = false;
            mid = true;
            botleft = false;
            botright = true;
            bot = true;
            break;
            case 6: top = true;
            topleft = true;
            topright = false;
            mid = true;
            botleft = true;
            botright = true;
            bot = true;
            break;
            case 7: top = true;
            topleft = false;
            topright = true;
            mid = false;
            botleft = false;
            botright = true;
            bot = false;
            break;
            case 8: top = true;
            topleft = true;
            topright = true;
            mid = true;
            botleft = true;
            botright = true;
            bot = true;
            break;
            case 9: top = true;
            topleft = true;
            topright = true;
            mid = true;
            botleft = false;
            botright = true;
            bot = true;
            break;                    
        }
    }
    Polygon ptop = new Polygon();
    Polygon ptopleft = new Polygon();
    Polygon ptopright = new Polygon();
    Polygon pmid = new Polygon();
    Polygon pbotleft = new Polygon();
    Polygon pbotright = new Polygon();
    Polygon pbot = new Polygon();
    public void create(int x, int y) {
        ptop.addPoint(x+10, y);
        ptop.addPoint(x+30, y);
        ptop.addPoint(x+35, y+5);
        ptop.addPoint(x+30, y+10);
        ptop.addPoint(x+10, y+10);
        ptop.addPoint(x+5, y+5);
        ptopleft.addPoint(x+5, y+5);
        ptopleft.addPoint(x+10, y+10);
        ptopleft.addPoint(x+10, y+30);
        ptopleft.addPoint(x+5, y+35);
        ptopleft.addPoint(x, y+30);
        ptopleft.addPoint(x, y+10);   
        ptopright.addPoint(x+35, y+5);
        ptopright.addPoint(x+40, y+10);
        ptopright.addPoint(x+40, y+30);
        ptopright.addPoint(x+35, y+35);
        ptopright.addPoint(x+30, y+30);
        ptopright.addPoint(x+30, y+10);     
        pmid.addPoint(x+10, y+30);
        pmid.addPoint(x+30, y+30);
        pmid.addPoint(x+35, y+35);
        pmid.addPoint(x+30, y+40);
        pmid.addPoint(x+10, y+40);
        pmid.addPoint(x+5, y+35);  
        pbotleft.addPoint(x+5, y+35);
        pbotleft.addPoint(x+10, y+40);
        pbotleft.addPoint(x+10, y+60);
        pbotleft.addPoint(x+5, y+65);
        pbotleft.addPoint(x, y+60);
        pbotleft.addPoint(x, y+40);
        pbotright.addPoint(x+35, y+35);
        pbotright.addPoint(x+40, y+40);
        pbotright.addPoint(x+40, y+60);
        pbotright.addPoint(x+35, y+65);
        pbotright.addPoint(x+30, y+60);
        pbotright.addPoint(x+30, y+40);            
        pbot.addPoint(x+10, y+60);
        pbot.addPoint(x+30, y+60);
        pbot.addPoint(x+35, y+65);
        pbot.addPoint(x+30, y+70);
        pbot.addPoint(x+10, y+70);
        pbot.addPoint(x+5, y+65);          
    }
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

  1. 您错误地使用了布局。您在向components设置layout之前添加panel 对于您的第一个问题,我的建议是阅读LayoutManagers

  2. 现在提出第二个问题: 你的ActionListener在哪里?请参阅修改后的代码。我添加了它。

  3. enter image description here

    修改后的代码

    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class PPP extends JFrame{  
        static digit atp_hundred = new digit();  //Amount to pay
        static digit atp_ten = new digit();
        static digit atp_unit = new digit();
        static digit atp_tenth = new digit();
        static digit atp_hundreth = new digit(); 
        static digit ltr_ten = new digit();     //Litres
        static digit ltr_unit = new digit();
        static digit ltr_tenth = new digit();
        static digit ppl_hundred = new digit(); //Pence per litre
        static digit ppl_ten = new digit();
        static digit ppl_unit = new digit();
        static digit ppl_tenth = new digit();
        static PPP frame = new PPP();   
    
        public static void init() {           
            frame.setSize(1280,800);
            frame.setLayout(new BorderLayout());
            JPanel mainPanel = new mainPanel();
            mainPanel.setLayout(new GridLayout(2,2));
            frame.getContentPane().add(mainPanel);
            frame.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                        System.exit(0);
                    }
                });
            frame.setVisible(true);
        }
    
        public static void main(String arg[]){
            init();
            while (true) {
                for (int i = 0; i < 10; i++) {
                    atp_hundred.value = i;
                    atp_hundred.update();
                    frame.getContentPane().repaint();
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                    }
                    frame.getContentPane().repaint();
                    try {
                        Thread.sleep(10);
                    } catch (Exception e) {
                    }
                }
            }
        }
    
        public void refresh() {
            repaint();
        }
    
        public PPP(){
            super();
        }
    
        /*public void update(Graphics g) {
        paint(g);
        }*/
    
        public static void atpCreate(Graphics g, int x, int y) {
            atp_hundred.create(x, y);
            drawPolygons(g, atp_hundred);
            atp_ten.create(x+50,y);
            drawPolygons(g, atp_ten);
            atp_unit.create(x+100, y);
            drawPolygons(g, atp_unit);
            g.drawOval(x+145, y+60, 10,10);
            atp_tenth.create(x+160, y);
            drawPolygons(g, atp_tenth);
        }
    
        public static void ltrCreate(Graphics g, int x, int y) {
            ltr_ten.create(x+50, y);
            drawPolygons(g, ltr_ten);
            ltr_unit.create(x+100, y);
            drawPolygons(g, ltr_unit);
            g.drawOval(x+145, y+60, 10, 10);
            ltr_tenth.create(x+160, y);
            drawPolygons(g, ltr_tenth);
        }
    
        public static void pplCreate(Graphics g, int x, int y) {
            ppl_hundred.create(x, y);
            drawPolygons(g, ppl_hundred);
            ppl_ten.create(x+50,y);
            drawPolygons(g, ppl_ten);
            ppl_unit.create(x+100, y);
            drawPolygons(g, ppl_unit);
            g.drawOval(x+145, y+60, 10,10);
            ppl_tenth.create(x+160, y);
            drawPolygons(g, ppl_tenth);
        }
    
        public static void drawPolygons(Graphics g, digit d) {
            if (d.top) {
                g.setColor(Color.green);
                g.fillPolygon(d.ptop);
            } else {
                g.setColor(Color.black);
                g.drawPolygon(d.ptop);
            }
            if (d.topleft) {
                g.setColor(Color.green);
                g.fillPolygon(d.ptopleft);
            } else {
                g.setColor(Color.black);
                g.drawPolygon(d.ptopleft);
            }
            if (d.topright) {
                g.setColor(Color.green);
                g.fillPolygon(d.ptopright);
            } else {
                g.setColor(Color.black);
                g.drawPolygon(d.ptopright);
            }
            if (d.mid) {
                g.setColor(Color.green);
                g.fillPolygon(d.pmid);
            } else {
                g.setColor(Color.black);
                g.drawPolygon(d.pmid);
            }
            if (d.botleft) {
                g.setColor(Color.green);
                g.fillPolygon(d.pbotleft);
            } else {
                g.setColor(Color.black);
                g.drawPolygon(d.pbotleft);
            }
            if (d.botright) {
                g.setColor(Color.green);
                g.fillPolygon(d.pbotright);
            } else {
                g.setColor(Color.black);
                g.drawPolygon(d.pbotright);
            }
            if (d.bot) {
                g.setColor(Color.green);
                g.fillPolygon(d.pbot);
            } else {
                g.setColor(Color.black);
                g.drawPolygon(d.pbot);
            }  
            g.setColor(Color.black);
        }
    }
    class mainPanel extends JPanel {
        public mainPanel() {
            setPreferredSize(new Dimension(1280,800));
            JPanel customerPanel = new customerPanel();
    
            add(customerPanel);
            //add(new customerButtons());
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setFont(new Font("Copperplate Gothic Light", Font.PLAIN, 30));
            String title = "Pete's Petrol Pump Simulation vAlpha0.1";
            g.setColor(Color.white);
            //g.clearRect(0,0,640,500);
            g.setColor(Color.black);
            g.drawString(title, (int)(640 - (g.getFontMetrics().getStringBounds(title, g).getWidth() / 2)), 75);
        }
    }
    class customerPanel extends JPanel {
        public customerPanel() {
            //setPreferredSize(new Dimension(640,500));
            //setPreferredSize(new Dimension(500,800));
            setLayout(new BorderLayout());
            customerButtons buttons = new customerButtons();
            add(buttons, BorderLayout.SOUTH);
        }
    
        @Override
        public void paintComponent(Graphics g){
            //super.paintComponent(g);
            /*g.setFont(new Font("Copperplate Gothic Light", Font.PLAIN, 30));
            String title = "Pete's Petrol Pump Simulation vAlpha0.1";
            g.setColor(Color.white);
            g.clearRect(0,0,640,500);
            g.setColor(Color.black);
            g.drawString(title, (int)(640 - (g.getFontMetrics().getStringBounds(title, g).getWidth() / 2)), 75);*/
            g.drawRoundRect(20,100,600,250,5,5);
            g.setFont(new Font("Gill Sans MT", Font.PLAIN, 20));
            g.drawString("Amount to Pay: ", 40, 160);
            PPP.atpCreate(g, 200, 110);
            g.drawString("Litres Dispensed: ", 40, 240);
            PPP.ltrCreate(g, 200, 190);
            g.drawString("Pence per Litre: ", 40, 320);
            PPP.pplCreate(g, 200, 270);
            //System.out.println((g.getFontMetrics().getStringBounds("Amount to Pay", g).getWidth()));
        }
    }
    class customerButtons extends JPanel implements ActionListener {
        public customerButtons() {
            JButton remove = new JButton("Remove nozzle");
            remove.setActionCommand("remove");
            //System.out.println("Hello");
            remove.addActionListener(this);
            remove.setToolTipText("Replace the fucking nozzle");
            JButton squeeze = new JButton("Squeeze nozzle");
            squeeze.setActionCommand("squeeze");
            squeeze.addActionListener(this);
            squeeze.setToolTipText("Squeeze the fucking nozzle");
            JButton stop = new JButton("Stop Squeeze");
            stop.setActionCommand("stop");
            stop.addActionListener(this);
            stop.setToolTipText("Stop squeezing the fucking nozzle");
            JButton replace = new JButton("Replace nozzle");
            replace.setActionCommand("replace");
            replace.addActionListener(this);
            replace.setToolTipText("Replace the fucking nozzle");
            add(remove);
            add(squeeze);
            add(stop);
            add(replace);
    
        }
    
        public void actionPerformed(ActionEvent e) {
            // do your stuff here
    
        }
    }
    
    class digit {
        int value = 0;
        boolean top = false;
        boolean topleft = false;
        boolean topright = false;
        boolean mid = false;
        boolean botleft = false;
        boolean botright = false;
        boolean bot = false;
        public void update() {
            switch(value) {
                case 0: top = true;
                topleft = true;
                topright = true;
                mid = false;
                botleft = true;
                botright = true;
                bot = true;
                break;
                case 1: top = false;
                topleft = false;
                topright = true;
                mid = false;
                botleft = false;
                botright = true;
                bot = false;
                break;
                case 2: top = true;
                topleft = false;
                topright = true;
                mid = true;
                botleft = true;
                botright = false;
                bot = true;
                break;
                case 3: top = true;
                topleft = false;
                topright = true;
                mid = true;
                botleft = false;
                botright = true;
                bot = true;
                break;
                case 4: top = false;
                topleft = true;
                topright = true;
                mid = true;
                botleft = false;
                botright = true;
                bot = false;
                break;
                case 5: top = true;
                topleft = true;
                topright = false;
                mid = true;
                botleft = false;
                botright = true;
                bot = true;
                break;
                case 6: top = true;
                topleft = true;
                topright = false;
                mid = true;
                botleft = true;
                botright = true;
                bot = true;
                break;
                case 7: top = true;
                topleft = false;
                topright = true;
                mid = false;
                botleft = false;
                botright = true;
                bot = false;
                break;
                case 8: top = true;
                topleft = true;
                topright = true;
                mid = true;
                botleft = true;
                botright = true;
                bot = true;
                break;
                case 9: top = true;
                topleft = true;
                topright = true;
                mid = true;
                botleft = false;
                botright = true;
                bot = true;
                break;                    
            }
        }
        Polygon ptop = new Polygon();
        Polygon ptopleft = new Polygon();
        Polygon ptopright = new Polygon();
        Polygon pmid = new Polygon();
        Polygon pbotleft = new Polygon();
        Polygon pbotright = new Polygon();
        Polygon pbot = new Polygon();
        public void create(int x, int y) {
            ptop.addPoint(x+10, y);
            ptop.addPoint(x+30, y);
            ptop.addPoint(x+35, y+5);
            ptop.addPoint(x+30, y+10);
            ptop.addPoint(x+10, y+10);
            ptop.addPoint(x+5, y+5);
            ptopleft.addPoint(x+5, y+5);
            ptopleft.addPoint(x+10, y+10);
            ptopleft.addPoint(x+10, y+30);
            ptopleft.addPoint(x+5, y+35);
            ptopleft.addPoint(x, y+30);
            ptopleft.addPoint(x, y+10);   
            ptopright.addPoint(x+35, y+5);
            ptopright.addPoint(x+40, y+10);
            ptopright.addPoint(x+40, y+30);
            ptopright.addPoint(x+35, y+35);
            ptopright.addPoint(x+30, y+30);
            ptopright.addPoint(x+30, y+10);     
            pmid.addPoint(x+10, y+30);
            pmid.addPoint(x+30, y+30);
            pmid.addPoint(x+35, y+35);
            pmid.addPoint(x+30, y+40);
            pmid.addPoint(x+10, y+40);
            pmid.addPoint(x+5, y+35);  
            pbotleft.addPoint(x+5, y+35);
            pbotleft.addPoint(x+10, y+40);
            pbotleft.addPoint(x+10, y+60);
            pbotleft.addPoint(x+5, y+65);
            pbotleft.addPoint(x, y+60);
            pbotleft.addPoint(x, y+40);
            pbotright.addPoint(x+35, y+35);
            pbotright.addPoint(x+40, y+40);
            pbotright.addPoint(x+40, y+60);
            pbotright.addPoint(x+35, y+65);
            pbotright.addPoint(x+30, y+60);
            pbotright.addPoint(x+30, y+40);            
            pbot.addPoint(x+10, y+60);
            pbot.addPoint(x+30, y+60);
            pbot.addPoint(x+35, y+65);
            pbot.addPoint(x+30, y+70);
            pbot.addPoint(x+10, y+70);
            pbot.addPoint(x+5, y+65);          
        }
    }
    

    但在这种情况下,请尝试创建如下布局:

    enter image description here

答案 1 :(得分:1)

1

关于按钮没有出现问题,您在为组件添加组件后设置每个面板(CustomerPanel和MainPanel)的布局。请在添加前尝试设置布局 组件。例子

setLayout(new BorderLayout()); customerButtons buttons = new customerButtons(); add(buttons, BorderLayout.SOUTH);

2

关于ActionListener问题,请您检查一下代码中是否实现了ActionListener。我找不到Actionlistener