[英]Can I add an ActionListener to a JButton that is not explicitely defined?
我正在嘗試制作一種棋盤式的東西,每個圖塊都是一個JButton。 我想將相同的actionListener添加到每個按鈕。 這是代碼:
package checker;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class mainClass extends JFrame {
JPanel board = new JPanel();
ActionListener btnPrs = new btnPressed();
public mainClass()
{
board.setLayout(new GridLayout(8,8,0,0));
for(int i = 0; i<8; i++)
for(int j = 0; j<8; j++)
{
if( i%2 == 0 && j%2 == 0 || i%2 == 1 && j%2 == 1)
{
board.add(new DrawWhite());
//board.add(new DrawWhite().addActionListener(btnPrs));
}
else board.add(new DrawBlack());
}
add(board);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
mainClass frame = new mainClass();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
frame.setTitle("Checker");
frame.setLocationRelativeTo(null);
}
class DrawWhite extends JButton
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0,0, 50,50);
}
}
class DrawBlack extends JButton
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, 50, 50);
}
}
class btnPressed implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("pressed!");
}
}
}
我不想顯式定義64個按鈕,並手動將ActionListeners添加到每個按鈕。 我嘗試過將它們包含在第23行的注釋中。是否有適當的方法?
任何幫助將不勝感激。
只需將按鈕存儲在一個臨時變量中:
DrawWhite dw = new DrawWhite();
dw.addActionListener(btnPrs);
board.add(dw);
如果您確實希望“ 每個圖塊都是JButton ”,那么您將需要定義64個JButton,因為同一組件不能兩次添加到其父級。
但是,如果您真正需要的是每個圖塊都是可單擊的,則根本不需要添加JButton。 整個板只使用一個自定義的JPanel
,覆蓋其paintComponent
方法以繪制黑色和白色正方形。 附加到此JPanel
單個mouseListener
可以根據x和y坐標區分白色瓷磚中的黑色。
工作代碼如下:
package checker;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class mainClass extends JFrame {
JPanel board = new BoardPanel();
MouseListener btnPrs = new btnPressed();
public mainClass()
{
board.addMouseListener(btnPrs);
add(board);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
mainClass frame = new mainClass();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
frame.setTitle("Checker");
frame.setLocationRelativeTo(null);
}
private Color blackOrWhite(int i, int j) {
return (i + j) % 2 == 0 ?
Color.WHITE
: Color.BLACK;
}
class BoardPanel extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
Color color = blackOrWhite(i, j);
g.setColor(color);
g.fillRect(i * 50, j * 50, 50, 50);
}
}
}
}
class btnPressed extends MouseAdapter
{
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("pressed!");
System.out.println(blackOrWhite(x/50, y/50));
}
}
}
不好的是,您在BorderLayout
失去了JButton
的“可調整大小”。 而且,您不再能夠通過鍵盤(就像使用JButton
)通過鍵盤來激活磁貼。 好消息是,您現在減少了實例化對象的方式,這帶來了更好的內存占用。
希望它有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.