[英]How do I give a value to my checkbox in java so for example, “when checkbox is selected = add + £20 total”?
Fruitslbl = new JLabel(); getContentPane().add(Fruitslbl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); Fruitslbl.setText("Fruits");
我是Java的新手。 我正在使用GUI构建器和Eclipse。
首先,您将需要一个用于JCheckbox的按钮处理程序,然后将新的JButton添加到按钮处理程序中,然后将添加一个actionListener,这将给我一个具有JButton的GUI的示例,本质上是一样的。 然后在您的ButtonHandler中使其更改您想要的代码的所有值,下面还将提供一个示例
public class GameBoard extends JFrame{
private static final Color red = new Color(200,55,55);
private static final Color blue = new Color(55,55,200);
private static final Color green = new Color(55,220,55);
private static final int SIZE = 10;
static JButton[][] tiles;
private static boolean cpTurn = false;
public static void main(String[] args){
tiles = new JButton[SIZE][SIZE];
JLabel label = new JLabel();
GameBoard gb = new GameBoard();
JButton refreashButton = new JButton("Refreash");
ButtonHandler listener = new ButtonHandler();
refreashButton.addActionListener(listener);
refreashButton.setName("refreash");
JPanel mineField = new JPanel();
mineField.setLayout(new GridLayout(10,10));
mineField.setSize(500,500);
for(int i = 0; i < SIZE; i++){
for(int j = 0; j < SIZE; j++){
tiles[i][j] = new JButton();
tiles[i][j].setSize(50,50);
tiles[i][j].setVisible(true);
tiles[i][j].setBorderPainted(false);
tiles[i][j].setContentAreaFilled(false);
tiles[i][j].setOpaque(true);
tiles[i][j].setBackground(green);//green
tiles[i][j].addActionListener(listener);
tiles[i][j].setName(i+""+j);
mineField.add(tiles[i][j]);
}
}
JPanel board = new JPanel();
board.setLayout(new GridLayout(2,1));
board.add(mineField);
board.add(refreashButton);
board.setBackground(green);*/
JPanel board = new JPanel();
board.setLayout(new BoxLayout(board, BoxLayout.Y_AXIS));
board.add(mineField);
board.add(refreashButton);
JFrame window = new JFrame("BoardGame");
window.setContentPane(board);
window.setSize(600,700);
window.setVisible(true);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
static class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
JButton btn = (JButton)e.getSource();
if(cpTurn == false){
if(btn.getName().equals("refreash")){
cpTurn = false;
}
else{
cpTurn = true;
}
}
else {
cpTurn = false;
}
if(btn.getName().equals("refreash")){
for(int i = 0; i < SIZE; i ++){
for(int j = 0; j < SIZE; j++){
tiles[i][j].setBackground(green);
}
}
}
else if(btn.getBackground().equals(green)){
btn.setBackground(red);
}
else if(btn.getBackground().equals(red)){
btn.setBackground(blue);
String name = btn.getName();
int x = Integer.parseInt(name.substring(0,1));
int y = Integer.parseInt(name.substring(1));
if(x - 1 >= 0 && tiles[x-1][y].getBackground().equals(red) ){
tiles[x-1][y].setBackground(blue);
}
if(x + 1 <= 9 && tiles[x+1][y].getBackground().equals(red) ){
tiles[x+1][y].setBackground(blue);
}
if(y + 1 <= 9 && tiles[x][y+1].getBackground().equals(red)){
tiles[x][y+1].setBackground(blue);
}
if(y - 1 >= 0 && tiles[x][y-1].getBackground().equals(red)){
tiles[x][y-1].setBackground(blue);
}
}
else if(btn.getBackground().equals(blue)){
btn.setBackground(green);
}
if(cpTurn){
int randx = (int)(Math.random() *(10));
int randy = (int)(Math.random() *(10));
tiles[randx][randy].doClick();
}
}
}
}
看看这个JCheckbox-ActionListener和ItemListener? 您必须为复选框定义动作侦听器,然后将动作放入侦听器代码中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.