0
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");

I'm totally new to Java. I'm using GUI builder and using Eclipse.

Cardinal System
  • 2,749
  • 3
  • 21
  • 42
PedramCarter
  • 101
  • 4
  • You should also have some code that refers to a checkbox if your question concerns checkboxes. Also, before posting, you can often answer your own question with a quick internet search, which would unearth nuggets like [this](http://www.codejava.net/java-se/swing/jcheckbox-basic-tutorial-and-examples). – MarsAtomic Mar 15 '15 at 19:12
  • I tried to google it, it made no sense to me I found this, and also I added my code in a wrap check, I found this my code is different to this Checkbox c = new Checkbox("Pepperoni")); add(c); add(new Checkbox("Pepperoni")); add(new Checkbox("Pepperoni", null, true)); private void handleCheckbox(Checkbox c) { if (c.getState()) price += 0.50; else price -= 0.50; – PedramCarter Mar 15 '15 at 19:24

2 Answers2

0

First you will need a buttonhandler for your JCheckbox and then you add your new JButton to the button handler then you will add a actionListener, ill give you an example i have for a GUI that has a JButton, essential the same thing. then in your ButtonHandler make it change all the values you want code with an example for this will also be below

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();

        }
    }
}

}

Thorx99
  • 38
  • 1
  • 2
  • 9
  • I'm so sorry to bother you I'm lost I used GUI builder in eclipse so all my code structure looks different which confuses me when I look at yours, I do have buttons, I have 1 that updates total price I also have another to proceed to next page for checkout. What do you think of this, I just made this "private void handleCheckbox(Checkbox Strawberrychk) { if (Strawberrychk.getState()) TotalPrice += 1.50; else TotalPrice -= 1.50; } } " – PedramCarter Mar 15 '15 at 19:50
0

Look at this JCheckbox - ActionListener and ItemListener? You have to define action listener for your checkbox and put your actions inside listener code.

Community
  • 1
  • 1
alnasfire
  • 720
  • 6
  • 23