Home > other >  Why doesn't my second button show in Java
Why doesn't my second button show in Java

Time:01-10

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

}

CodePudding user response:

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)

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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

  •  Tags:  
  • Related