为什么 ActionListener 不起作用?
Why the ActionListener does not work?
我一直在尝试做这个并且它确实有效,除了当我点击按钮时没有任何反应,这是一个大学项目。
我只需要知道如何使按钮按预期工作,使我对其进行编码的功能。
/*
* 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);
你需要做 Boton3.addActionListener(new Disenos());
建议:
- 将 ActionListener 添加到按钮。这意味着
Boton3.addActionListener(this);
.
- 您不应该以任何绘制方法创建组件或将它们添加到 GUI。 paint 或 paintComponent 等绘画方法应该只用于绘画和绘画。
- 您无法直接控制何时甚至 if 绘画方法将被调用,您也无法控制它被调用的次数,因此这使得它用于更改 GUI 组件的危险场所。
- 任何减慢绘画速度的事情都会使您的 GUI 看起来反应迟钝,这将不会被用户欣赏。为此,画法要力求精快,又要涉及画只.
- 您不想覆盖 JComponent 的 paint 方法,而是覆盖它的 paintComponent 方法。
- 不要在组件上调用
getGraphics()
也不要尝试使用由此获得的 Graphics 对象进行绘制。这样做会给你一个短暂的 Graphics 对象,这会使你的绘图不稳定。而是使用 JVM 提供给您的 Graphics 对象在 paintComponent 方法中完成所有绘图。
- 您可以通过在 BufferedImage 上调用
getGraphics()
并在 that 中绘制来绕过上述限制,但是您仍然需要在 paintComponent 方法中绘制 BufferedImage通过 g.drawImage(...)
方法。
- 我自己,我会做一些不同的事情,包括创建一个 JPanel,它使用一个 GridLayout,它包含 JLabels,上面有圆形 ImageIcons,底部有一个 JButton。然后,当按下按钮时,我会在需要交换的 JLabel 中交换 ImageIcons。维护和增强它会容易得多,因为没有位置的硬编码。
例如,您甚至可以取消 JButton 列,而是使用 JLabel 网格,为它们提供一个 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();
}
});
}
}
我一直在尝试做这个并且它确实有效,除了当我点击按钮时没有任何反应,这是一个大学项目。
我只需要知道如何使按钮按预期工作,使我对其进行编码的功能。
/*
* 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);
你需要做 Boton3.addActionListener(new Disenos());
建议:
- 将 ActionListener 添加到按钮。这意味着
Boton3.addActionListener(this);
. - 您不应该以任何绘制方法创建组件或将它们添加到 GUI。 paint 或 paintComponent 等绘画方法应该只用于绘画和绘画。
- 您无法直接控制何时甚至 if 绘画方法将被调用,您也无法控制它被调用的次数,因此这使得它用于更改 GUI 组件的危险场所。
- 任何减慢绘画速度的事情都会使您的 GUI 看起来反应迟钝,这将不会被用户欣赏。为此,画法要力求精快,又要涉及画只.
- 您不想覆盖 JComponent 的 paint 方法,而是覆盖它的 paintComponent 方法。
- 不要在组件上调用
getGraphics()
也不要尝试使用由此获得的 Graphics 对象进行绘制。这样做会给你一个短暂的 Graphics 对象,这会使你的绘图不稳定。而是使用 JVM 提供给您的 Graphics 对象在 paintComponent 方法中完成所有绘图。 - 您可以通过在 BufferedImage 上调用
getGraphics()
并在 that 中绘制来绕过上述限制,但是您仍然需要在 paintComponent 方法中绘制 BufferedImage通过g.drawImage(...)
方法。 - 我自己,我会做一些不同的事情,包括创建一个 JPanel,它使用一个 GridLayout,它包含 JLabels,上面有圆形 ImageIcons,底部有一个 JButton。然后,当按下按钮时,我会在需要交换的 JLabel 中交换 ImageIcons。维护和增强它会容易得多,因为没有位置的硬编码。
例如,您甚至可以取消 JButton 列,而是使用 JLabel 网格,为它们提供一个 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();
}
});
}
}