iSoulFeed1g
iSoulFeed1g

Reputation: 45

Why doesn't my second button show in Java

I am having a problem with my code. For some reason, it won't show my second button b2, and I can't set the size for the first button. I want to have both buttons next to each other in the middle with some space around them.

import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;

public class HW10{
    Button b1, b2;
    L1 l1;
    class L1 implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            int tmp = Integer.parseInt(b1.getLabel());
            tmp++;
            b1.setLabel(""+tmp);
        }
        
    }
    public HW10(){
        JFrame frame = new JFrame("Homework 15");
        l1 = new L1();
        b1 = new Button("0");
        b2 = new Button("KURAC");
        b1.addActionListener(l1);
        b1.setBounds(100, 100, 100, 80);
        frame.add(b1);
        frame.setBounds(200,200,400,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        HW10 kk = new HW10();
    }

}

Upvotes: 2

Views: 264

Answers (5)

Chris
Chris

Reputation: 34199

As others pointed out, you missed adding the second button to your frame. A quick fix would be to add both the buttons as below:

frame.getContentPane().add(b1, BorderLayout.NORTH);
frame.getContentPane().add(b2, BorderLayout.SOUTH);

Upvotes: 1

user8234870
user8234870

Reputation:

Code :

package hw10;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class HW10 {
    JButton b1;
    JButton b2;
//    L1 l1;
/*
    class L1 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
//            int tmp = Integer.parseInt(b1.getLabel());
            int tmp = Integer.parseInt(b1.getText());
            tmp++;
//            b1.setLabel(""+tmp);
            b1.setText(Integer.toString(tmp));
        }

    }
*/

    public HW10() {
        JFrame frame = new JFrame("Homework 15");
//        l1 = new L1();
        b1 = new JButton("0");
//        b1.setSize(100, 40);
        b2 = new JButton("KURAC");
//        b2.setSize(100, 40);
        /*
            We can use lambda expressions
         */
        b1.addActionListener((ActionEvent e) -> {
            int tmp = Integer.parseInt(b1.getText());
            tmp++;
            b1.setText(Integer.toString(tmp));
        });
        //Use GridBagLayout as your layout manager
        GridBagLayout layout = new GridBagLayout();
        JPanel panel = new JPanel();
        panel.setLayout(layout);
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(2, 2, 2, 2);
        //0th row
        c.gridx = 0;
        //0th column
        c.gridy = 0;
        //width
        c.ipadx = 100;
        //height
        c.ipady = 30;
        //Adding first button
        panel.add(b1, c);
        //1st column we
        c.gridx = 1;
        //Adding second button
        panel.add(b2, c);
        frame.add(panel);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void launch() {
        SwingUtilities.invokeLater(() -> {
            new HW10();
        });
    }

    public static void main(String... $) {
        launch();
    }

}

Output :

output

Resource :

geeksforgeeks

Upvotes: 1

daniu
daniu

Reputation: 14999

The other answers are correct that you don't add the second button at all, however, you shouldn't add components to frames directly. What you want to do is

frame.getContentPane().add(button1);
frame.getContentPane().add(button2);

You should also probably be setting a layout manager to the pane.

Upvotes: 2

Alexandru Severin
Alexandru Severin

Reputation: 6228

The second button is not added to frame. You need to call:

frame.add(b2);

Regarding button size, use the method .setSize for each button to declare what size you want them to have.

Upvotes: 2

Drexx_Dusan
Drexx_Dusan

Reputation: 57

Try using JButton instead of Button, if that doesn't work, jbg ga. Also try extending class with JFrame -> extends JFrame, if you want the subclass to inherit everything from jframe class. Also u have to add it using frame.add(button)

Upvotes: 2

Related Questions