因此,我尝试编写危险游戏,但要注意的是,我仅尝试将按钮分配给1个动作侦听器,以便所有按钮都可以独立运行,但可以从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
}
我正在尝试获取它,以便我可以仅使用1个动作侦听器单击多个按钮,但我只能获得一个网格
答案 0 :(得分:0)
您可以使用AbstractButton(JButton的超类).setActionCommand,然后在侦听器中使用action命令确定行和列
您应该使用JTable,而不是创建6 x 6的JButton数组,而是设置呈现器以将单元格值呈现为按钮(或其他方式),并按照此处的建议在表模型中添加listSelectionListener来获取行和点击时的列值。如这里的建议:Get position of Click JTable while using Cell renderer
学习JTable以及如何使用渲染等可能需要一些时间。但是我认为对于这个问题,您无论如何都处于学习环境中,这不是为企业而做的事情吗?因此,我建议您花时间学习JTable。我向您保证,您最终将对最终产品更加满意。
答案 1 :(得分:0)
这里是更正后的代码,按下按钮即可打印到控制台。请检查代码中的注释:
<div class="container">
<div class="myBox headerTitle">
<h1> HELLO THERE </h1>
</div>
<div class="myBox bottomLeft">
<h2> Title </h2>
<p> Some shit </p>
</div>
<div class="myBox bottomLeft">
<h2> Title </h2>
<p> Some shit </p>
</div>
<div class="myBox bottomRight">
<h2> Title </h2>
<p> Some shit </p>
</div>
<div class="myBox bottomRight">
<h2> Title </h2>
<p> Some shit </p>
</div>
</div>
输出(当您按下多个按钮时):
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
}
}
希望这会有所帮助。