首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >3 JButtons无法触发3 drawImages

3 JButtons无法触发3 drawImages
EN

Stack Overflow用户
提问于 2018-02-27 03:58:53
回答 1查看 21关注 0票数 0

我正在尝试用一个边框布局,3个jbutton和画图的drawImage组件来创建一个侦听按钮按下和改变图像的红绿灯。我知道条件语句中的drawImages可以工作,因为如果我去掉if语句,图像看起来很好,而且我知道操作监听器可以工作,因为我用joptionpane对话框测试了每个监听器。然而,在当前的表单中,按钮按下时没有任何反应,我不确定为什么。任何帮助都将不胜感激,我正在学习!

代码语言:javascript
复制
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TrafficLight extends JFrame implements ActionListener
{
//establishes panel, button, and boolean variables
 private JPanel pN, pS, pE, pW, pC;

 private JButton btnWait, btnGo, btnStop;

 private boolean redIlluminated = false , greenIlluminated = false , yellowIlluminated = false;

public static void main(String[] args)
{
    TrafficLight frame = new TrafficLight();
    frame.setSize(750, 850);
    frame.createGUI();
    frame.setVisible(true);

}

private void createGUI()
{
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container window = getContentPane();

    window.setLayout(new BorderLayout());

    //custom colors
    Color slate=new Color(49, 58, 58);
    Color eggshell=new Color(230, 226, 222);
    Color easterPink=new Color(249, 170, 170);
    Color salmon=new Color(201, 80, 65);
    Color dusk=new Color(187, 185, 184);
    Color billiards=new Color(71, 88, 68);

    //custom fonts
    Font buttonFont = new Font("SansSerif", Font.BOLD, 20);

    //sets up north jpanel 
    pN = new JPanel();
    pN.setPreferredSize(new Dimension(680,45));
    pN.setBackground(eggshell);
    window.add (pN, BorderLayout.NORTH);

    //button formatting
    //establishes go button, font, color, and click event, then adds it to pN panel
    btnGo = new JButton ("GO");
    btnGo.setFont(buttonFont);
    btnGo.setBackground(dusk);
     btnGo.addActionListener(
        new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                //trigger condition change here to cause the paint event below
              greenIlluminated = true;
            }
        }
    );
    pN.add(btnGo);

    //establishes wait button, font, color, and click event, then adds it to pN panel
    btnWait = new JButton("WAIT"); 
    btnWait.setFont(buttonFont);
    btnWait.setBackground(dusk);
    btnWait.addActionListener(
        new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
               //trigger condition change here to cause the paint event below 
              yellowIlluminated = true;
            }
        }
    );
    pN.add(btnWait);

    //establishes stop button, font, color, and click event, then adds it to pN panel
    btnStop = new JButton("STOP");
    btnStop.setFont(buttonFont);
    btnStop.setBackground(dusk);
    btnStop.addActionListener(
        new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                //trigger condition change here to cause the paint event below
              redIlluminated = true;
            }
        }
    );
    pN.add(btnStop);

    //east jpanel for stoplight
    pE = new JPanel();
    pE.setPreferredSize(new Dimension(272,318));
    pE.setBackground(billiards);
    window.add (pE, BorderLayout.EAST);

    //west jpanel
    pW = new JPanel();
    pW.setPreferredSize(new Dimension(136,318));
    pW.setBackground(billiards);
    window.add (pW, BorderLayout.WEST);

    //center jpanel for car
    pC = new JPanel();
    pC.setPreferredSize(new Dimension(272,318));
    pC.setBackground(slate);
    window.add (pC, BorderLayout.CENTER);

    //south jpanel
    pS = new JPanel();
    pS.setPreferredSize(new Dimension(680, 15));
    pS.setBackground(eggshell);
    window.add (pS, BorderLayout.SOUTH);
}

public void actionPerformed(ActionEvent click) {


    }

public void paint(Graphics g) {
    super.paint(g);
    //draws stoplight
    g.drawImage(new ImageIcon(getClass().getResource("red1.png")).getImage(), 460, 150, this);
    g.drawImage(new ImageIcon(getClass().getResource("green1.png")).getImage(), 460, 272, this);
    g.drawImage(new ImageIcon(getClass().getResource("yellow1.png")).getImage(), 460, 373, this);

    //draws car in center
    g.drawImage(new ImageIcon(getClass().getResource("car.png")).getImage(), 150, 600, this);

    //sets conditions to show images on button click based on boolean logic changed by buttons
    if (redIlluminated)
    {
        g.drawImage(new ImageIcon(getClass().getResource("red2.png")).getImage(), 460, 150, this);
    }

    else if (greenIlluminated)
    {
        g.drawImage(new ImageIcon(getClass().getResource("green2.png")).getImage(), 460, 272, this);
    }
    else if (yellowIlluminated)
    {
        g.drawImage(new ImageIcon(getClass().getResource("yellow2.png")).getImage(), 460, 373, this);
    }
     }

}
EN

回答 1

Stack Overflow用户

发布于 2018-04-20 12:31:11

不确定是否仍然与您相关,但只是为了防止其他人偶然发现这个问题...

Swing容器不会不断侦听对其图形的更改,因此当您进行更改时,需要确保调用repaint(),以便重新呈现整个容器。

在按钮的ActionListener中添加一个方法调用如下所示:

代码语言:javascript
复制
btnGo.addActionListener( new ActionListener() {

        public void actionPerformed(ActionEvent ae) {

          //trigger condition change here to cause the paint event below, I turn off the other flags so that the if clause in the paint method knows exactly what to paint 
          greenIlluminated = true;
          redIlluminated = false;
          yellowIlluminated = false;

          //This is the one method that triggers a call to the paint method you overrode
          repaint();
       }
});

并且需要对另一个ActionListeners进行类似的更改。

顺便说一句,如果要为按钮使用匿名ActionListener类的对象,则不需要实现ActionListener接口。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48996161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档