gpt4 book ai didi

java - 通过按 jbutton java 更改图像

转载 作者:行者123 更新时间:2023-12-02 11:57:59 25 4
gpt4 key购买 nike

您好,我的代码遇到了问题,几天来我一直在试图找出问题所在,并查看了一些相关的程序来寻求帮助,但无法弄清楚。该程序应该根据您按下的按钮来更改交通灯的图像:红色、黄色或绿色。有3个类(class)。我正在 eclipse 中运行该程序。包含 main 方法的 1 类 TrafficLight:

import javax.swing.*;
import java.awt.*;
public class trafficLight
{
//-----------------------------------------------------------------
// Creates and displays the main program frame.
//-----------------------------------------------------------------
public static void main(String[] args)
{
JFrame frame = new JFrame("CHANGE TRAFFIC LIGHT");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

trafficLightPanel lights = new trafficLightPanel();
trafficLightControls controls = new trafficLightControls(lights);

JPanel panel = new JPanel();
panel.setBackground(Color.blue);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));



panel.add(Box.createRigidArea (new Dimension (0, 20)));
panel.add(lights);
panel.add(Box.createRigidArea (new Dimension (0, 10)));
panel.add(controls);
panel.add(Box.createRigidArea (new Dimension (0, 10)));

frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}

包含图像图标部分的第二类trafficLightPanel:

import java.awt.*;
import javax.swing.*;
public class trafficLightPanel extends JPanel
{
public int count, redCount, yellowCount, greenCount;
private ImageIcon none, red, yellow, green;
private JLabel imageLabel;
//-----------------------------------------------------------------
// Constructor: Sets up the images and the initial state.
//-----------------------------------------------------------------
public trafficLightPanel()
{

none = new ImageIcon("nonePic.png");
red = new ImageIcon("redPic.png");
yellow = new ImageIcon("yellowPic.png");
green = new ImageIcon("greenPic.png");
setBackground(Color.black);
redCount = 1; yellowCount = 2; greenCount = 3;

imageLabel = new JLabel(none);

add(imageLabel);

}
//-----------------------------------------------------------------
// Paints the panel using the appropriate image.
//-----------------------------------------------------------------
public void paintComponent(Graphics page)
{
super.paintComponent(page);

if (count == redCount)
{
imageLabel.setIcon(red);
}
if (count == yellowCount)
{
imageLabel.setIcon(yellow);
}
if (count == greenCount)
{
imageLabel.setIcon(green);
}
}
//-----------------------------------------------------------------
// Sets the status of the traffic light.
//-----------------------------------------------------------------
public void setCount(int newCount)
{
count = newCount;
}
}

第三类trafficLightControls包含jbuttons:

//********************************************************************
// Represents the control panel for the traffic light program.
//********************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class trafficLightControls extends JPanel
{
private trafficLightPanel lights;
private JButton red, yellow, green;
//-----------------------------------------------------------------
// Sets up the traffic light control panel.
//-----------------------------------------------------------------
public trafficLightControls(trafficLightPanel lightPanel)
{
lights = lightPanel;
red = new JButton("RED");
red.addActionListener(new redListener());
yellow = new JButton("YELLOW");
yellow.addActionListener(new yellowListener());
green = new JButton("GREEN");
green.addActionListener(new greenListener());

setBackground(Color.black);
add(red);
add(yellow);
add(green);

}
//*****************************************************************
// Represents the listener for the red button.
//*****************************************************************
private class redListener implements ActionListener
{
//--------------------------------------------------------------
// sets count to redCount and repaints the lights panel.
//--------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
lights.setCount(lights.redCount);
lights.repaint();
}
}
//*****************************************************************
//Represents the listener for the yellow button.
//*****************************************************************
private class yellowListener implements ActionListener
{
//--------------------------------------------------------------
//sets count to yellowCount and repaints the lights panel.
//--------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
lights.setCount(lights.yellowCount);
lights.repaint();
}
}
//*****************************************************************
//Represents the listener for the green button.
//*****************************************************************
private class greenListener implements ActionListener
{
//--------------------------------------------------------------
//sets count to green count and repaints the lights panel.
//--------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
lights.setCount(lights.greenCount);
lights.repaint();
}
}
}

nonePic.png the non lit up traffic light image redPic.png of traffic light lit up with red yellowPic.png greenPic.png

每次我单击按钮时,都应该将 TrafficLightPanel 对象(灯光)中的计数设置为与颜色相对应的计数,然后根据计数将图像替换为相应的图像。所有组件均以盒式布局组合在一起。

出于某种原因,只有红色可以工作...它开始显示无图片,即没有任何灯光的图片,当我单击红色时,它会显示红色图片。如果我在单击红色按钮之前或之后单击任何一个,则其他按钮不会显示。如果我先单击其他按钮之一,然后单击红色按钮,则由于某种原因仍会显示红色。所有图像都位于根文件夹中(这是正确的名称吗?),其中包含 src 和 bin 文件夹。

我认为计数可能有问题,我尝试做一些事情,例如添加一个 jLabel,每次都会使用框布局将计数显示到程序中,但它不显示任何内容(此尝试不在代码中) )。我还尝试将 jLabel 放入 TrafficLightControls 类中,并将其与 add(red) add(yellow)...一起添加,但它不会显示任何内容。我尝试的另一件事是每次更改按钮的文本以显示带有计数的颜色,作为 jLabel 尝试的替代方案。我尝试在红色的监听器类中使用 .setText("") 方法,例如 red.setText("") 。如果有人能够解释如何添加 jLabel 并更改按钮的文本(正如我在这个特定的小段落中所描述的那样),我将不胜感激,因为我想知道如何做以供将来引用,尽管它是没有必要解决我的问题,所以不帮助解决这一小段也没关系。

非常感谢您提供的任何帮助!

编辑:(很抱歉,我在尝试制作 jLabels 来测试代码时留下了残余部分,但我删除了它们,尽管它们没有影响我相信的代码,但由于我的问题,我尝试使用它们。我是如果这让任何人感到困惑,非常抱歉)

最佳答案

如果您所做的只是交换 ImageIcons,则无需调用 repaint() 并且不应重写 paintComponent。只需在 JLabel 上调用 setIcon(...) 即可。模型将触发 View 本身的重新绘制。

摆脱你的paintComponent覆盖并将setCount更改为像这样简单的东西可以工作:

public void setCount(int newCount) {
count = newCount;
Icon icon = null;
switch (count) {
case 1:
icon = red;
break;
case 2:
icon = yellow;
break;
case 3:
icon = green;
break;
default:
icon = null;
break;
}
imageLabel.setIcon(icon);
}

例如,我的 MCVE,它使用枚举和 Map 来稍微简化代码。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

@SuppressWarnings("serial")
public class Traff2 extends JPanel {
public Traff2() {
Traff2LightPanel lightPanel = new Traff2LightPanel();
Traff2LightControlsPanel controlsPanel = new Traff2LightControlsPanel(lightPanel);

setLayout(new BorderLayout());
add(lightPanel, BorderLayout.CENTER);
add(controlsPanel, BorderLayout.PAGE_END);
}

private static void createAndShowGui() {
Traff2 mainPanel = new Traff2();

JFrame frame = new JFrame("Traffic");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

enum Light {
NONE(""), RED("Red"), YELLOW("Yellow"), GREEN("Green");

private String text;

private Light(String text) {
this.text = text;
}

public String getText() {
return text;
}
}

@SuppressWarnings("serial")
class Traff2LightPanel extends JPanel {
private Map<Light, Icon> lightColorMap = new EnumMap<>(Light.class);
private JLabel imageLabel = new JLabel();
private Light light = Light.NONE;

public Traff2LightPanel() {
// fill the map
lightColorMap.put(Light.NONE, new ImageIcon("nonePic.png"));
lightColorMap.put(Light.RED, new ImageIcon("redPic.png"));
lightColorMap.put(Light.YELLOW, new ImageIcon("yellowPic.png"));
lightColorMap.put(Light.GREEN, new ImageIcon("greenPic.png"));

imageLabel.setIcon(lightColorMap.get(Light.NONE));
add(imageLabel);
}

// when changing the light field,
// also set the ImageIcon
public void setLight(Light light) {
this.light = light;
imageLabel.setIcon(lightColorMap.get(light));
}

public Light getLight() {
return light;
}
}

@SuppressWarnings("serial")
class Traff2LightControlsPanel extends JPanel {
private Traff2LightPanel lightPanel;

public Traff2LightControlsPanel(Traff2LightPanel lightPanel) {
this.lightPanel = lightPanel;
for (Light light : Light.values()) {
if (light == Light.NONE) {
continue;
}
add(new JButton(new LightAction(light)));
}
}

// use an AbstractAction...
// like an ActionListener on "steroids"
private class LightAction extends AbstractAction {
private Light light;

public LightAction(Light light) {
super(light.getText());
this.light = light;
}

@Override
public void actionPerformed(ActionEvent e) {
lightPanel.setLight(light);
}
}
}

关于java - 通过按 jbutton java 更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47459632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com