0

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.

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
Eljay
  • 941
  • 5
  • 15
  • 30
  • 9
    Don't try to set locations and sizes. Use layout managers, which will adapt the locations and sizes based on the OS and the user preferences automatically. http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html. And please respect the Java naming conventions: variables start with a lowercase letter, classes don't contain underscores, but use CamelCase. – JB Nizet Mar 31 '14 at 06:19
  • Use your layout managers and try with setBounds() in your button. – Shriram Mar 31 '14 at 06:21
  • 3
    @Shriram bad advice 1) for suggesting to use setBounds 2) because a layout manager with setBounds does absolutely nothing. – Paul Samsotha Mar 31 '14 at 06:23
  • 3
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Mar 31 '14 at 07:04
  • possible duplicate of [setSize not influencing size of button](http://stackoverflow.com/questions/3076314/setsize-not-influencing-size-of-button) – trashgod Mar 31 '14 at 08:20

2 Answers2

2

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());
    }
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
0

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.

Nerakai
  • 85
  • 3