JButton无法响应鼠标点击。

huangapple go评论69阅读模式
英文:

JButton is not responding to mouse click

问题

这是从我之前收到的示例中复制并调整的内容。

我目前有两个问题:

首先,我只能在屏幕上显示一个JButton,然后当我按下它时,该JButton没有响应。
另外,我如何阻止第一个正方形出现?

我知道程序目前是一个大的代码块,一旦我解决了这些问题,我会将其分成单独的类。

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

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

    static void createAndShowGUI() {
        System.out.println("Created GUI on EDT? " +
                SwingUtilities.isEventDispatchThread());
        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new MyPanel());
        f.pack();
        f.setLayout(null);
        JButton seed = new JButton("Seed");
        seed.setBounds(1050, 50, 100, 50);
        f.add(seed);
        seed.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.out.println("Start");
            }
        });
        JButton start = new JButton("Start");
        start.setBounds(1050, 150, 100, 50);
        f.add(start);
        JButton stop = new JButton("Stop");
        stop.setBounds(1050, 250, 100, 50);
        //f.add(stop);
        JButton reset = new JButton("Reset");
        reset.setBounds(1050, 350, 100, 50);
        //f.add(reset);
        f.setVisible(true);
        int lifegrid[][][] = new int[42][62][2];
        for (int i = 0; i < 42; i++) {
            for (int j = 0; j < 62; j++) {
                lifegrid[i][j][0] = 0;
                lifegrid[i][j][1] = 0;
            }
        }
    }
}

class MyPanel extends JPanel {
    int lifegrid[][][] = new int[62][42][2];
    int squareX = 0;
    int squareY = 0;
    int gridX = 0;
    int gridY = 0;

    public MyPanel() {
        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                squareX = e.getX();
                squareY = e.getY();
                if ((squareX > 50 & squareX < 950) & (squareY > 50 & squareY < 650)) {
                    gridX = (squareX - 50) / 15 + 1;
                    gridY = (squareY - 50) / 15 + 1;
                    squareX = (squareX - 50) / 15 * 15 + 50;
                    squareY = (squareY - 50) / 15 * 15 + 50;
                    lifegrid[gridX][gridY][0] = 1;
                    System.out.println(gridX + " " + gridY);
                    repaint(squareX, squareY, 15, 15);
                } else {
                }
            }
        });
    }

    public Dimension getPreferredSize() {
        return new Dimension(1280, 800);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillRect(squareX, squareY, 13, 13);
        g.setColor(Color.BLACK);
        g.drawRect(squareX, squareY, 13, 13);
    }
}
英文:

This is copied and adapted from the example I received from an earlier question

I have 2 issues at the moment

Firstly, I can only get one JButton to appear on screen, then that JButton is not responding when I press it.
Also, how can I stop the first square from appearing?

I know the program is in one big blob ATM, I will separate into individual classes once I sort these issues out.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingPaintDemo3 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI(); 
}
});
}
static void createAndShowGUI() {
System.out.println(&quot;Created GUI on EDT? &quot;+
SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame(&quot;Swing Paint Demo&quot;);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
f.add(new MyPanel());
f.pack();
f.setLayout(null);
JButton seed = new JButton (&quot;Seed&quot;);
seed.setBounds(1050,50,100,50);
f.add(seed);
seed.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.out.println(&quot;Start&quot;);
}	
});
JButton start = new JButton (&quot;Start&quot;);
start.setBounds(1050,150,100,50);
f.add(start);
JButton stop = new JButton (&quot;Stop&quot;);
stop.setBounds(1050,250,100,50);
//f.add(stop);
JButton reset = new JButton (&quot;Reset&quot;);
reset.setBounds(1050,350,100,50);
//f.add(reset);
f.setVisible(true);
int lifegrid [] [] [] = new int [42] [62] [2];
for (int i=0; i&lt;42; i++) {
for (int j=0; j&lt;62; j++) {
lifegrid [i] [j] [0] = 0;
lifegrid [i] [j] [1] = 0;
}
}
} 
}
class MyPanel extends JPanel {
int lifegrid [] [] [] = new int [62] [42] [2];
int squareX = 0;
int squareY = 0;
int gridX = 0;
int gridY = 0;
public MyPanel() {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
squareX = e.getX();
squareY = e.getY();
if ((squareX&gt;50 &amp; squareX&lt;950) &amp; (squareY&gt;50 &amp; squareY &lt;650) ){
gridX =(squareX-50)/15+1;
gridY =(squareY-50)/15+1;
squareX = (squareX -50)/15 * 15 + 50;
squareY = (squareY -50)/15 * 15 + 50;
lifegrid [gridX] [gridY] [0] = 1;
System.out.println(gridX + &quot; &quot; + gridY);
repaint(squareX,squareY,15,15);}
else {}
}
});
}
public Dimension getPreferredSize() {
return new Dimension(1280,800);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);       
g.setColor(Color.RED);
g.fillRect(squareX,squareY,13,13);
g.setColor(Color.BLACK);
g.drawRect(squareX,squareY,13,13);
}  
}

答案1

得分: 3

你有两个按钮。

一个按钮上面写着“Seed”。当你按下它时,你的动作监听器会打印出“Start”。

另一个按钮上面写着“Start”。它没有动作监听器,因此,如果按下它,什么都不会发生。

来源:就是...阅读你的代码,它就在那里。

我猜那是一个bug。如果不是的话,那就相当令人困惑:为什么按下上面 没有 写着“Start”的按钮会在控制台打印出“Start”?

然后,你将这两个按钮添加到一个布局可疑的面板上。

我猜你正在按下唯一能看见的那个按钮(上面写着“Start”),因为它正好在另一个按钮的正上方,然后你什么也没看到。

这正是你编程的结果。

可能的解决方案:

  • 修复你的布局,或者使用布局管理器。然后按下正确的按钮(上面写着“Seed”的那个)
  • 为你的“Start”按钮添加动作监听器,并让它打印出“start”。
  • 修复点击“Seed”按钮会打印出“Start”的bug。你应该让它打印出“Seed”。
英文:

You have 2 buttons.

One button's face reads 'Seed'. When you press it, your action listener will print "Start".

The other button's face reads 'Start'. It has no action listener, therefore, if pressed, nothing happens.

SOURCE: Just.. read your code, it's right there.

I assume that's a bug. If not, that is quite confusing: Why would pressing the button that does NOT have 'Start' written on it, print 'Start' to sysout?

You then add both buttons to a panel with dubious layouting.

I assume you're pressing the only button you can see (With 'Start' on it), because it is right on top of the other button, and you observe nothing.

Which is exactly what you programmed.

Probable solutions:

  • Fix your layouting, or use a layout manager. Then press the right button (the one that reads 'Seed')
  • Add an action listener to your start button and make it print 'start'.
  • Fix the bug where clicking the Seed button prints Start. You should make that print 'Seed'.

huangapple
  • 本文由 发表于 2020年9月9日 20:45:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63812006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定