Eljay
Eljay

Reputation: 951

Unable to set size and location of button in java

I was just writing an application and i was not able to set location and size of button to desired values. whenever i open code the button comes at same location and with same size Here is my code

public class main_class {
    public static void main(String args[]){
        Main_page mp = new Main_page();
        mp.start();
    }
}

Second file is:

import javax.swing.*;
import java.awt.*;

public class Main_page extends JPanel {
    private static final long serialVersionUID = 1L;
    public  void start(){
      JPanel panel1 = new JPanel();
      panel1.setBackground(Color.pink);

      JButton Button1 = new JButton("Programmer");
      Button1.setSize(10, 100);
      Button1.setLocation(200,500);
      panel1.add(Button1);

      JFrame  frame1 = new JFrame("Main Window");
      frame1.setSize(700,500);
      frame1.setContentPane(panel1);
      frame1.setResizable(false);
      frame1.setVisible(true);  
    }
}

what is the problem.

Upvotes: 0

Views: 1209

Answers (2)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51533

What is the problem?

I'm so glad you asked that question. It's not just a matter of getting a Swing application to work. You must get the Swing application to work correctly so that you can add more functionality to your Swing application without your Swing application breaking.

  1. I added a call to SwingUtilities invokeLater in the main method. This call ensures that the Swing components are defined and used on the Event Dispatch thread (EDT).

  2. I changed the name of your class to MainPage to conform to Java standards for naming classes.

  3. I implemented Runnable to make the invokeLater method parameter easier to define.

  4. I removed the extends of JPanel. You use Swing components. The only reason to extend a Swing component is when you want to override one of the component methods. The only reason you extend any Java class is when you want to override one of the class methods.

  5. I changed the name of your MainPage method to run.

  6. I set a layout (FlowLayout) for your JPanel. You must always use a layout manager for your Swing components.

  7. I made button1 lowercase. Java field names are lowercase, so you can easily differentiate them from class names.

  8. I added a call to the JFrame setDefaultCloseOperation to your run method. Without this call, your Swing application will not stop executing when you close the window. After a while, you'll have dozens of copies of your Swing application running, with no easy way to stop them.

  9. I added a call to the JFrame pack to let the JFrame expand or contract to fit the components, rather than be a fixed size.

Here's the revised code. I put the main method in the MainPage class to make it easier to paste the code.

package com.ggl.testing;

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MainPage implements Runnable {

    @Override
    public void run() {
        JPanel panel1 = new JPanel();
        panel1.setBackground(Color.pink);
        panel1.setLayout(new FlowLayout());

        JButton button1 = new JButton("Programmer");
        panel1.add(button1);

        JFrame frame1 = new JFrame("Main Window");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.add(panel1);
        frame1.pack();

        frame1.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new MainPage());
    }
}

Upvotes: 2

Nerakai
Nerakai

Reputation: 85

if you want to set the size and location manually then you can use panel1.setLayout(null); as people have said in the comments this isn't recommended.

Upvotes: 0

Related Questions