所以我试图编写一个危险的游戏代码,但问题是我只是试图将我的按钮分配给一个动作监听器,这样所有按钮都可以自己运行,但是从1个动作监听器开始工作。
我已经尝试了很多,没有任何作用!包jep;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class jep implements ActionListener{
public JButton[][] t = new JButton[6][6];
public static void main(String[] args) {
new jep();
}
static int n = 100;
public jep() {
JFrame frame = new JFrame("Jeopardy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1920,1080);
frame.setLayout(new GridLayout(6, 5));
frame.setVisible(true);
for (int r = 0; r < 6; r++) {
for (int c = 0; c < 5; c++) {
String vakue = String.valueOf(n);
t[r][c] = new JButton(vakue);
t[r][c].setBackground(Color.BLUE);
t[r][c].setForeground(Color.YELLOW);
t[r][c].addActionListener(this);
frame.add(t[r][c]);
}
n = n +300;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
我试图得到它,以便我可以只使用一个动作监听器点击多个按钮,但我能得到的只是一个网格
你可以使用AbstractButton(JButton的超类).setActionCommand,然后在你的监听器中使用action命令你可以找出你的行和列
您应该使用JTable,将渲染器设置为按钮(或其他东西),而不是创建一个6 x 6的JButton数组,并按照此处的建议将listSelectionListener添加到tablemodel以获取行和列值点击。正如在此推荐:Get position of Click JTable while using Cell renderer
学习JTable以及如何使用渲染等可能需要一些时间。但我认为,对于这个问题,无论如何你都处在一个学习环境中,这不是为业务做的事情,或者?所以我建议你花点时间学习JTable。你最终会对我向你承诺的最终产品感到高兴。
这是在按下按钮时打印到控制台的更正代码。请检查代码中的注释:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Jep implements ActionListener { // class name has to start with a capital letter
int i = 6;
int j = 5;
public JButton[][] t = new JButton[i][j];
public static void main(String[] args) {
new Jep();
}
static int n = 100;
public Jep() {
JFrame frame = new JFrame("Jeopardy");
JPanel[][] panelHolder = new JPanel[i][j]; // use panels to add you buttons, check this for details:
// https://stackoverflow.com/questions/2510159/can-i-add-a-component-to-a-specific-grid-cell-when-a-gridlayout-is-used
frame.setLayout(new GridLayout(i, j));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1920, 1080);
frame.setVisible(true);
for (int r = 0; r < 6; r++) {
for (int c = 0; c < 5; c++) {
String vakue = String.valueOf(n);
t[r][c] = new JButton(vakue);
t[r][c].setBackground(Color.BLUE);
t[r][c].setForeground(Color.BLACK);
t[r][c].addActionListener(this);
panelHolder[r][c] = new JPanel();
panelHolder[r][c].add(t[r][c]);
frame.add(panelHolder[r][c]);
n = n + 300;
}
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("press, value = " + arg0.getActionCommand()); // here is a simple system out log statement
}
}
输出(当您按下几个按钮时):
press, value = 100
press, value = 400
press, value = 700
press, value = 1000
press, value = 1300
press, value = 1600
应用程序窗口:qazxsw poi
希望这会有所帮助。