0

I'm new to JFrame buttons and I have two JButtons. The one with Submit works but when I try to use the other with a specific name it doesn't output a string into the console. In the end the second JButton (btn) will have different names but when clicked on the button doesn't do anything.

package schoolprgm;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;


public class Jeopardy extends JFrame  {


static JButton btn = new JButton();static JButton Submit = new JButton();
static JFrame frame = new JFrame("Jeopardy");static JPanel panel = new JPanel(new GridLayout(7,5,10,10));
static JTextField textField = new JTextField(10);
public  Jeopardy(){


    frame.setDefaultCloseOperation(3);
    frame.setVisible(true);

    Submit.addActionListener(new ButtonListener());
     btn.addActionListener(new ButtonListener());
 }

public static void main(String[] args) {
    // TODO Auto-generated method stub


     frame.setPreferredSize(new Dimension(1000,400));



        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        int Score = 0;
       JLabel l2 = new JLabel("Score: "+ String.valueOf(Score));
       JLabel l3 = new JLabel();
       ArrayList question = new ArrayList();
        JLabel l = new JLabel("Question: "+ question);

        Jeopardy.Submit.setText("Submit");

        l.setLabelFor(textField);


       /*label1.setText("Label1");

        panel.add(label1);
        panel.add(label2);
        panel.add(label3);
        panel.add(label4);
        panel.add(label5);
        panel.add(label6);
        *///panel.add(label7);


        String words = ("Category 1,Category 2,Category 3,Category 4,Category 5,Category 6,Category 7,C1: 100,100,100,100,100,100,100,200,200,200,200,200,200,200,300,300,300,300,300,300,300,400,400,400,400,400,400,400,500,500,500,500,500,500,500,999,999,99,999,999,999,999,999,999,999,999,999");
        ArrayList btnnames = new ArrayList();
        String[] words2 = words.split(",");
        btnnames.addAll(Arrays.asList(words2));
      frame.add(panel);
        for(int x=0;x<42;x++){

            if( x >= 0 && x<=6){
                JLabel label = new JLabel(String.valueOf(btnnames.get(x)));
                panel.add(label);
                label.setText(String.valueOf(btnnames.get(x)));
            }
            else{
           btn = new JButton(String.valueOf(btnnames.get(x)));
          Jeopardy.btn.setText(String.valueOf(btnnames.get(x)));
            panel.add(btn);
        }}


        panel.add(l);
        panel.add(textField);
        panel.add(Submit);
        panel.add(l2);
        panel.add(l3);



        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setResizable(false);

    frame.getContentPane().add(new Jeopardy());

}

}

class ButtonListener implements ActionListener {
@Override   

public void actionPerformed(ActionEvent e) {


     if (e.getActionCommand()=="Submit"){
        System.out.println("submit");
        System.out.println(Jeopardy.textField.getText());

    } if (e.getActionCommand()==("C1: 100")) {
      System.out.println("100 has been clicked");
    }


    }
}
  • Implement a separate listener for each button. – bradimus Mar 04 '16 at 00:05
  • Also avoid `static` references – MadProgrammer Mar 04 '16 at 00:22
  • Thanks for the help but the problem turns out that since I kept changing the btn to equal something new it won't check the older buttons made in the for statement. – Alton Guyton Mar 04 '16 at 00:46
  • So I guess the answer would be, don't use `static` – MadProgrammer Mar 04 '16 at 02:56
  • Actually no I left the static in the program what I did was change the button to an array and each button was created in the for statement with a value set with setName(String.valueOf(x-7)) and with the ActionListener with this... if (e.getSource() instanceof JButton && e.getActionCommand() != "Submit")...inside this I'm going to add a case to check value of button to display a certain question on frame. @MadProgrammer – Alton Guyton Mar 05 '16 at 04:05

0 Answers0