gpt4 book ai didi

java - 为什么 ActionListener 不起作用?

转载 作者:行者123 更新时间:2023-12-01 22:29:18 26 4
gpt4 key购买 nike

我一直在尝试做这个,它确实有效,除了当我单击按钮时没有任何反应,这是一个大学项目。

我需要知道的是如何使按钮按预期工作,使我编码的功能能够执行。

 /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package proyecto;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;



/**
*
* @author Cristian
*/
public class Diseños extends JComponent implements ActionListener {

JButton Boton1, Boton2, Boton3, Boton4, Boton5, Boton6, Boton7;





@Override
public void paint(Graphics g) {


this.Boton1=new JButton("-");
this.Boton1.setBounds(90, 410, 30, 30);
this.Boton1.setBackground(Color.black);
Component agregar1 = add(Boton1);



this.Boton2=new JButton("-");
this.Boton2.setBounds(150, 410, 30, 30);
this.Boton2.setBackground(Color.BLACK);
Component agregar2 = add(Boton2);

this.Boton3 =new JButton("-");
this.Boton3.setBounds(210, 410, 30, 30);
this.Boton3.setBackground(Color.BLACK);
Component agregar3 = add(Boton3);

this.Boton4=new JButton("-");
this.Boton4.setBounds(270, 410, 30, 30);
this.Boton4.setBackground(Color.BLACK);
Component agregar4 = add(Boton4);

this.Boton5=new JButton("-");
this.Boton5.setBounds(330, 410, 30, 30);
this.Boton5.setBackground(Color.BLACK);
Component agregar5 = add(Boton5);

this.Boton6=new JButton("-");
this.Boton6.setBounds(390, 410, 30, 30);
this.Boton6.setBackground(Color.BLACK);
Component agregar6 = add(Boton6);

this.Boton7=new JButton("-");
this.Boton7.setBounds(450, 410, 30, 30);
this.Boton7.setBackground(Color.BLACK);
Component agregar7 = add(Boton7);

g.drawRect (570, 10, 300, 200);
g.setColor(Color.BLACK);
g.fillRect(570, 10, 300, 200);
g.drawRect (570, 250, 300, 200);
g.setColor(Color.BLACK);
g.fillRect(570, 250, 300, 200);
g.setColor(Color.WHITE);
Font oldFont=getFont();
Font fuente=new Font("Primer Jugador", Font.BOLD, 14);
g.setFont(fuente);
g.drawString("Primer Jugador", 580, 30);
Font titulo=new Font("Segundo Jugador", Font.BOLD, 14);
g.setFont(titulo);
g.drawString("Segundo Jugador", 580, 270);
g.drawString("[ " + Proyecto.primernombre + " ]", 580, 50);
g.drawString("[ " + Proyecto.segundonombre + " ]", 580, 290);
g.setColor(Color.red);
g.fillRoundRect(10, 5, 550, 450, 50, 50);
g.setColor(Color.WHITE);
g.fillOval(80, 50, 50, 50);
g.fillOval(140, 50, 50, 50);
g.fillOval(200, 50, 50, 50);
g.fillOval(260, 50, 50, 50);
g.fillOval(320, 50, 50, 50);
g.fillOval(380, 50, 50, 50);
g.fillOval(440, 50, 50, 50);
g.fillOval(80, 110, 50, 50);
g.fillOval(140, 110, 50, 50);
g.fillOval(200, 110, 50, 50);
g.fillOval(260, 110, 50, 50);
g.fillOval(320, 110, 50, 50);
g.fillOval(380, 110, 50, 50);
g.fillOval(440, 110, 50, 50);
g.fillOval(80, 170, 50, 50);
g.fillOval(140, 170, 50, 50);
g.fillOval(200, 170, 50, 50);
g.fillOval(260, 170, 50, 50);
g.fillOval(320, 170, 50, 50);
g.fillOval(380, 170, 50, 50);
g.fillOval(440, 170, 50, 50);
g.fillOval(80, 230, 50, 50);
g.fillOval(140, 230, 50, 50);
g.fillOval(200, 230, 50, 50);
g.fillOval(260, 230, 50, 50);
g.fillOval(320, 230, 50, 50);
g.fillOval(380, 230, 50, 50);
g.fillOval(440, 230, 50, 50);
g.fillOval(80, 290, 50, 50);
g.fillOval(140, 290, 50, 50);
g.fillOval(200, 290, 50, 50);
g.fillOval(260, 290, 50, 50);
g.fillOval(320, 290, 50, 50);
g.fillOval(380, 290, 50, 50);
g.fillOval(440, 290, 50, 50);
g.fillOval(80, 350, 50, 50);
g.fillOval(140, 350, 50, 50);
g.fillOval(200, 350, 50, 50);
g.fillOval(260, 350, 50, 50);
g.fillOval(320, 350, 50, 50);
g.fillOval(380, 350, 50, 50);
g.fillOval(440, 350, 50, 50);
}

@Override
public void actionPerformed (ActionEvent ae){
if(ae.getSource().equals(Boton1) && turno.Tjugador == true && colores.colorjugador[0] == 1 && matrices.matrizraya[0] == 6) {
System.out.println("I doubt this is even working");
turno.Tjugador = false;
matrices.matrizraya[0] --;
getGraphics().setColor(Color.RED);
getGraphics().fillOval(80, 350, 50, 50);
repaint();
}

}
}

编辑:我一直在重新测试并且代码可以工作,但只有当我不使用 if 时,这是为什么?

编辑:好的,我解决了按钮问题,但我得到了一个新按钮,当我尝试更改 Boton61 的背景颜色时,出现此错误:

线程“AWT-EventQueue-0”中出现异常 java.lang.NullPointerException 在 proyecto.Diseños.actionPerformed(Diseños.java:364)

package proyecto;


import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;



/**
*
* @author Cristian
*
* */

public class Diseños extends JComponent implements ActionListener {

JButton Boton1, Boton2, Boton3, Boton4, Boton5, Boton6, Boton7;

JButton Boton11,Boton21,Boton31,Boton41,Boton51,Boton61;
JButton Boton12,Boton22,Boton32,Boton42,Boton52,Boton62;
JButton Boton13,Boton23,Boton33,Boton43,Boton53,Boton63;
JButton Boton14,Boton24,Boton34,Boton44,Boton54,Boton64;
JButton Boton15,Boton25,Boton35,Boton45,Boton55,Boton65;
JButton Boton16,Boton26,Boton36,Boton46,Boton56,Boton66;
JButton Boton17,Boton27,Boton37,Boton47,Boton57,Boton67;




@Override
public void paintComponent(Graphics g) {
Boton1=new JButton("lol");
Boton1.setBounds(90, 410, 30, 30);
Boton1.setBackground(Color.black);
Component agregar1 = add(Boton1);
Boton1.addActionListener(new Diseños());


Boton2=new JButton("lol");
Boton2.setBounds(150, 410, 30, 30);
Boton2.setBackground(Color.black);
Component agregar2 = add(Boton2);
Boton2.addActionListener(new Diseños());

Boton3=new JButton("lol");
Boton3.setBounds(210, 410, 30, 30);
Boton3.setBackground(Color.black);
Component agregar3 = add(Boton3);
Boton3.addActionListener(new Diseños());

Boton4=new JButton("lol");
Boton4.setBounds(270, 410, 30, 30);
Boton4.setBackground(Color.black);
Component agregar4 = add(Boton4);
Boton4.addActionListener(new Diseños());

Boton5=new JButton("lol");
Boton5.setBounds(330, 410, 30, 30);
Boton5.setBackground(Color.black);
Component agregar5 = add(Boton5);
Boton5.addActionListener(new Diseños());

Boton6=new JButton("lol");
Boton6.setBounds(390, 410, 30, 30);
Boton6.setBackground(Color.black);
Component agregar6 = add(Boton6);
Boton6.addActionListener(new Diseños());

Boton7=new JButton("lol");
Boton7.setBounds(450, 410, 30, 30);
Boton7.setBackground(Color.black);
Component agregar7 = add(Boton7);
Boton7.addActionListener(new Diseños());


Boton11=new JButton("");
Boton11.setBounds(85, 10, 40, 60);
Boton11.setBackground(Color.white);
Component agregar11 = add(Boton11);
Boton11.addActionListener(new Diseños());

Boton21=new JButton("");
Boton21.setBounds(85, 70, 40, 60);
Boton21.setBackground(Color.white);
Component agregar21 = add(Boton21);
Boton21.addActionListener(new Diseños());

Boton31=new JButton("");
Boton31.setBounds(85, 130, 40, 60);
Boton31.setBackground(Color.white);
Component agregar31 = add(Boton31);
Boton31.addActionListener(new Diseños());

Boton41=new JButton("");
Boton41.setBounds(85, 190, 40, 60);
Boton41.setBackground(Color.white);
Component agregar41 = add(Boton41);
Boton41.addActionListener(new Diseños());

Boton51=new JButton("");
Boton51.setBounds(85, 250, 40, 60);
Boton51.setBackground(Color.white);
Component agregar51 = add(Boton51);
Boton51.addActionListener(new Diseños());

Boton61=new JButton("");
Boton61.setBounds(85, 310, 40, 60);
Boton61.setBackground(Color.white);
Component agregar61 = add(Boton61);
Boton61.addActionListener(new Diseños());

Boton12=new JButton("");
Boton12.setBounds(145, 10, 40, 60);
Boton12.setBackground(Color.white);
Component agregar12 = add(Boton12);
Boton12.addActionListener(new Diseños());

Boton22=new JButton("");
Boton22.setBounds(145, 70, 40, 60);
Boton22.setBackground(Color.white);
Component agregar22 = add(Boton22);
Boton22.addActionListener(new Diseños());

Boton32=new JButton("");
Boton32.setBounds(145, 130, 40, 60);
Boton32.setBackground(Color.white);
Component agregar32 = add(Boton32);
Boton32.addActionListener(new Diseños());

Boton42=new JButton("");
Boton42.setBounds(145, 190, 40, 60);
Boton42.setBackground(Color.white);
Component agregar42 = add(Boton42);
Boton42.addActionListener(new Diseños());

Boton52=new JButton("");
Boton52.setBounds(145, 250, 40, 60);
Boton52.setBackground(Color.white);
Component agregar52 = add(Boton52);
Boton52.addActionListener(new Diseños());

Boton62=new JButton("");
Boton62.setBounds(145, 310, 40, 60);
Boton62.setBackground(Color.white);
Component agregar62 = add(Boton62);
Boton62.addActionListener(new Diseños());

Boton13=new JButton("");
Boton13.setBounds(205, 10, 40, 60);
Boton13.setBackground(Color.white);
Component agregar13 = add(Boton13);
Boton13.addActionListener(new Diseños());

Boton23=new JButton("");
Boton23.setBounds(205, 70, 40, 60);
Boton23.setBackground(Color.white);
Component agregar23 = add(Boton23);
Boton23.addActionListener(new Diseños());

Boton33=new JButton("");
Boton33.setBounds(205, 130, 40, 60);
Boton33.setBackground(Color.white);
Component agregar33 = add(Boton33);
Boton33.addActionListener(new Diseños());

Boton43=new JButton("");
Boton43.setBounds(205, 190, 40, 60);
Boton43.setBackground(Color.white);
Component agregar43 = add(Boton43);
Boton43.addActionListener(new Diseños());

Boton53=new JButton("");
Boton53.setBounds(205, 250, 40, 60);
Boton53.setBackground(Color.white);
Component agregar53 = add(Boton53);
Boton53.addActionListener(new Diseños());

Boton63=new JButton("");
Boton63.setBounds(205, 310, 40, 60);
Boton63.setBackground(Color.white);
Component agregar63 = add(Boton63);
Boton63.addActionListener(new Diseños());

Boton14=new JButton("");
Boton14.setBounds(265, 10, 40, 60);
Boton14.setBackground(Color.white);
Component agregar14 = add(Boton14);
Boton14.addActionListener(new Diseños());

Boton24=new JButton("");
Boton24.setBounds(265, 70, 40, 60);
Boton24.setBackground(Color.white);
Component agregar24 = add(Boton24);
Boton24.addActionListener(new Diseños());

Boton34=new JButton("");
Boton34.setBounds(265, 130, 40, 60);
Boton34.setBackground(Color.white);
Component agregar34 = add(Boton34);
Boton34.addActionListener(new Diseños());

Boton44=new JButton("");
Boton44.setBounds(265, 190, 40, 60);
Boton44.setBackground(Color.white);
Component agregar44 = add(Boton44);
Boton44.addActionListener(new Diseños());

Boton54=new JButton("");
Boton54.setBounds(265, 250, 40, 60);
Boton54.setBackground(Color.white);
Component agregar54 = add(Boton54);
Boton54.addActionListener(new Diseños());

Boton64=new JButton("");
Boton64.setBounds(265, 310, 40, 60);
Boton64.setBackground(Color.white);
Component agregar64 = add(Boton64);
Boton64.addActionListener(new Diseños());

Boton15=new JButton("");
Boton15.setBounds(325, 10, 40, 60);
Boton15.setBackground(Color.white);
Component agregar15 = add(Boton15);
Boton15.addActionListener(new Diseños());

Boton25=new JButton("");
Boton25.setBounds(325, 70, 40, 60);
Boton25.setBackground(Color.white);
Component agregar25 = add(Boton25);
Boton25.addActionListener(new Diseños());

Boton35=new JButton("");
Boton35.setBounds(325, 130, 40, 60);
Boton35.setBackground(Color.white);
Component agregar35 = add(Boton35);
Boton35.addActionListener(new Diseños());

Boton45=new JButton("");
Boton45.setBounds(325, 190, 40, 60);
Boton45.setBackground(Color.white);
Component agregar45 = add(Boton45);
Boton45.addActionListener(new Diseños());

Boton55=new JButton("");
Boton55.setBounds(325, 250, 40, 60);
Boton55.setBackground(Color.white);
Component agregar55 = add(Boton55);
Boton55.addActionListener(new Diseños());

Boton65=new JButton("");
Boton65.setBounds(325, 310, 40, 60);
Boton65.setBackground(Color.white);
Component agregar65 = add(Boton65);
Boton65.addActionListener(new Diseños());

Boton16=new JButton("");
Boton16.setBounds(385, 10, 40, 60);
Boton16.setBackground(Color.white);
Component agregar16 = add(Boton16);
Boton16.addActionListener(new Diseños());

Boton26=new JButton("");
Boton26.setBounds(385, 70, 40, 60);
Boton26.setBackground(Color.white);
Component agregar26 = add(Boton26);
Boton26.addActionListener(new Diseños());

Boton36=new JButton("");
Boton36.setBounds(385, 130, 40, 60);
Boton36.setBackground(Color.white);
Component agregar36 = add(Boton36);
Boton36.addActionListener(new Diseños());

Boton46=new JButton("");
Boton46.setBounds(385, 190, 40, 60);
Boton46.setBackground(Color.white);
Component agregar46 = add(Boton46);
Boton46.addActionListener(new Diseños());

Boton56=new JButton("");
Boton56.setBounds(385, 250, 40, 60);
Boton56.setBackground(Color.white);
Component agregar56 = add(Boton56);
Boton56.addActionListener(new Diseños());

Boton66=new JButton("");
Boton66.setBounds(385, 310, 40, 60);
Boton66.setBackground(Color.white);
Component agregar66 = add(Boton66);
Boton66.addActionListener(new Diseños());

Boton17=new JButton("");
Boton17.setBounds(445, 10, 40, 60);
Boton17.setBackground(Color.white);
Component agregar17 = add(Boton17);
Boton17.addActionListener(new Diseños());

Boton27=new JButton("");
Boton27.setBounds(445, 70, 40, 60);
Boton27.setBackground(Color.white);
Component agregar27 = add(Boton27);
Boton27.addActionListener(new Diseños());

Boton37=new JButton("");
Boton37.setBounds(445, 130, 40, 60);
Boton37.setBackground(Color.white);
Component agregar37 = add(Boton37);
Boton37.addActionListener(new Diseños());

Boton47=new JButton("");
Boton47.setBounds(445, 190, 40, 60);
Boton47.setBackground(Color.white);
Component agregar47 = add(Boton47);
Boton47.addActionListener(new Diseños());

Boton57=new JButton("");
Boton57.setBounds(445, 250, 40, 60);
Boton57.setBackground(Color.white);
Component agregar57 = add(Boton57);
Boton57.addActionListener(new Diseños());

Boton67=new JButton("");
Boton67.setBounds(445, 310, 40, 60);
Boton67.setBackground(Color.white);
Component agregar67 = add(Boton67);
Boton67.addActionListener(new Diseños());

super.paintComponent(g);
g.drawRect (570, 10, 300, 200);
g.setColor(Color.BLACK);
g.fillRect(570, 10, 300, 200);
g.drawRect (570, 250, 300, 200);
g.setColor(Color.BLACK);
g.fillRect(570, 250, 300, 200);
g.setColor(Color.WHITE);
Font oldFont=getFont();
Font fuente=new Font("Primer Jugador", Font.BOLD, 14);
g.setFont(fuente);
g.drawString("Primer Jugador", 580, 30);
Font titulo=new Font("Segundo Jugador", Font.BOLD, 14);
g.setFont(titulo);
g.drawString("Segundo Jugador", 580, 270);
g.drawString("[ " + Proyecto.primernombre + " ]", 580, 50);
g.drawString("[ " + Proyecto.segundonombre + " ]", 580, 290);
g.setColor(Color.CYAN);
g.fillRoundRect(10, 5, 550, 450, 50, 50);
}


@Override
public void actionPerformed(ActionEvent ae) {
System.out.println("Ayy lmao");
Boton61.setBackground(Color.blue);
Boton61.repaint();
}
}


Line 364 is :

Boton61.setBackground(Color.blue);

最佳答案

建议:

  • 将 ActionListener 添加到按钮。这里的意思是 Boton3.addActionListener(this);
  • 您不应该以任何绘画方法创建组件或将它们添加到 GUI。诸如paint或paintComponent之类的绘画方法应该仅用于绘画和绘画。
    • 您无法直接控制何时甚至是否调用绘画方法,也无法控制调用它的次数,因此这使得它成为一个危险的使用场所用于更改 GUI 的组件。
    • 任何减慢绘制速度的行为都会使您的 GUI 显得响应速度较慢,而用户不会欣赏这一点。因此,绘画方法应力求精益和快速,并且再次应只涉及绘画
  • 您不希望重写 JComponent 的 Paint 方法,而是重写其 PaintComponent 方法。
  • 不要在组件上调用getGraphics(),也不要尝试使用由此获得的 Graphics 对象进行绘制。这样做会给你一个短暂的 Graphics 对象,这将使你的绘图不稳定。相反,使用 JVM 提供给您的 Graphics 对象在 PaintComponent 方法中完成所有绘图。
  • 您可以通过在 BufferedImage 上调用 getGraphics() 并在 that 中进行绘制来绕过上述限制,但您仍然需要在 PaintComponent 方法中绘制 BufferedImage通过 g.drawImage(...) 方法。
  • 我自己会做不同的事情,包括创建一个使用 GridLayout 的 JPanel,该 GridLayout 包含带有圆形 ImageIcons 的 JLabels 和底部的 JButton。然后,当按下按钮时,我会交换 JLabel 中需要交换的 ImageIcons。由于不会对位置进行硬编码,因此维护和增强这一点会容易得多。
<小时/>

例如,您甚至可以取消 JButtons 列,转而使用 JLabels 网格,为它们提供一个 MouseListener 来检查 JLabel 所在的列,然后根据按下的列添加一个彩色磁盘:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

@SuppressWarnings("serial")
public class Connect4Panel extends JPanel {
public static final int ROWS = 6;
public static final int COLUMNS = 7;
public static final int DISK_WIDTH = 50;
public static final int DISK_BORDER_WIDTH = 5;
private static final Color FIRST_COLOR = Color.red;
private static final Color SECOND_COLOR = Color.black;
private Icon emptyIcon;
private Icon firstIcon;
private Icon secondIcon;
private JLabel[][] diskGrid = new JLabel[ROWS][COLUMNS];
private boolean firstTurn = true;

public Connect4Panel() {
emptyIcon = createIcons(new Color(0, 0, 0, 15));
firstIcon = createIcons(FIRST_COLOR);
secondIcon = createIcons(SECOND_COLOR);

DiskListener diskListener = new DiskListener();
// setBackground(Color.DARK_GRAY);
setLayout(new GridLayout(ROWS, COLUMNS, 2, 0));
for (int row = 0; row < diskGrid.length; row++) {
for (int col = 0; col < diskGrid[row].length; col++) {
JLabel disk = new JLabel(emptyIcon);
disk.addMouseListener(diskListener);
diskGrid[row][col] = disk;
add(disk);
}
}
}

private Icon createIcons(Color color) {
int width = DISK_WIDTH + 2 * DISK_BORDER_WIDTH;
BufferedImage bImg = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bImg.createGraphics();
g2.setColor(color);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x = DISK_BORDER_WIDTH;
int oWidth = DISK_WIDTH;
g2.fillOval(x, x, oWidth, oWidth);
g2.dispose();
return new ImageIcon(bImg);
}

private class DiskListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
JLabel disk = (JLabel) e.getSource();
int selectedColumn = -1;
for (int row = 0; row < diskGrid.length; row++) {
for (int col = 0; col < diskGrid[row].length; col++) {
if (disk == diskGrid[row][col]) {
selectedColumn = col;
}
}
}

for (int row = ROWS - 1; row >= 0; row--) {
if (diskGrid[row][selectedColumn].getIcon() == emptyIcon) {
Icon icon = firstTurn ? firstIcon : secondIcon;
diskGrid[row][selectedColumn].setIcon(icon);
firstTurn = !firstTurn;
break;
}
}

// TODO check for win
}
}

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

JFrame frame = new JFrame("Connect 4 Panel");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

JOptionPane.showMessageDialog(frame, "Click on panel to see how it works!");
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 为什么 ActionListener 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28143599/

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