1

I am trying to add some buttons but it only shows button3 , I don't know why I didn't found any duplicate with other question

How I can make each one of these buttons to show(when someone clicked them) on a label the value of a variable? Please if anyone can help

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class gui extends JFrame {

    public gui() {

        initUI();
    }

    private void initUI() {

        JButton quitButton = new JButton("Quit");
        JButton button1 = new JButton("button1");
        JButton button2 = new JButton("button2");
        JButton button3 = new JButton("button3");

        quitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

        createLayout(quitButton);
        createLayout(button1);
        createLayout(button2);
        createLayout(button3);

        setTitle("example");
        setSize(500, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void createLayout(JComponent... arg) {

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
        );
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                gui ex = new gui();
                ex.setVisible(true);
            }
        });
    }
javAndr
  • 43
  • 2
  • 5
  • Why use `createLayout(JComponent... arg)` when you're only accessing the first element? – Jonny Henly Apr 12 '16 at 00:35
  • @JonnyHenly I don't know that;s what I read on the internet – javAndr Apr 12 '16 at 00:38
  • Each time you call createLayout, you're creating a new GroupLayout and adding a single component to it, so only the last component is actually getting managed. IMHO GroupLayout is not a good choice for manual management, it's more intended for form designers – MadProgrammer Apr 12 '16 at 00:44

1 Answers1

1

If you have no idea and just want to see all of your buttons you could try it like this:

  • delete your createLayout()
  • replace your initUI() method with:

    private void initUI() {
    
        JButton quitButton = new JButton("Quit");
        JButton button1 = new JButton("button1");
        JButton button2 = new JButton("button2");
        JButton button3 = new JButton("button3");
        JPanel panel = new JPanel();
    
        quitButton.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
    
        button1.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
    
        getContentPane().add(panel);
    
        panel.add(quitButton);
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
    
        setTitle("example");
        setSize(500, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    

I'd highly recommend you first to read the documentation about swing layouts: A Visual Guide to Layout Managers

Yev
  • 321
  • 3
  • 9
  • Hi, Thank you for your answer. Can you please tell me how I can make that GUI, so when user clicks the button1 , to show a string( in a label maybe) ..when it clicks the button2 show some other string etc... – javAndr Apr 12 '16 at 17:36
  • @javAndr, declare `JLabel label;` global, then use `label.setText("Button1 clicked");` in the `actionPerformed` -method of ActionListener of button1 instead of `System.exit(0);` And don't forget to add the label to the panel – Yev Apr 13 '16 at 14:24