Java Swing

Author Topic: Java Swing  (Read 3685 times)

Offline kazi shahin

  • Hero Member
  • *****
  • Posts: 607
  • Fear is not real
    • View Profile
    • Personal website of kazi shahin
Java Swing
« on: July 30, 2010, 02:36:22 PM »
Swing class is important part of Java language. Java Swing class is used for Graphics User Interface (GUI). It' a popular class in Java.
 



                                              Swing Components
« Last Edit: July 30, 2010, 02:38:22 PM by kazi shahin »
Kazi Shahin                   
092-15-795
Department of CSE   
Cell : 01718 699 590
Blood Group: O+
Google + :  https://plus.google.com/u/0/101741817431143727344/about?hl=en
Facebook : http://www.facebook.com/kazishahin.rahman
Web : http://www.kazishahin.com/

Offline kazi shahin

  • Hero Member
  • *****
  • Posts: 607
  • Fear is not real
    • View Profile
    • Personal website of kazi shahin
Re: Java Swing
« Reply #1 on: July 30, 2010, 02:44:21 PM »
                   

  A simple JFRame Code

import javax.swing.JFrame;


public class Simple extends JFrame {

    public Simple() {

        setSize(300, 200);
        setTitle("Simple");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
   }

    public static void main(String[] args) {

        Simple simple = new Simple();
        simple.setVisible(true);

    }
}



While this code is very small, the application window can do quite a lot. It can be resized, maximized, minimized. All the complexity that comes with it has been hidden from the application programmer.
 
import javax.swing.JFrame;

Here we import the JFrame widget. It is a toplevel container, which is used for placing other widgets.

 setSize(300, 200);
 setTitle("Simple");


This code will resize the window to be 300px wide and 200px tall. It will set the title of the window to Simple.

setDefaultCloseOperation(EXIT_ON_CLOSE);

This method will close the window, if we click on the close button. By default nothing happens.


This code generate the below JFrame, titled Simple.

          


 

« Last Edit: July 30, 2010, 02:54:40 PM by kazi shahin »
Kazi Shahin                   
092-15-795
Department of CSE   
Cell : 01718 699 590
Blood Group: O+
Google + :  https://plus.google.com/u/0/101741817431143727344/about?hl=en
Facebook : http://www.facebook.com/kazishahin.rahman
Web : http://www.kazishahin.com/

Offline kazi shahin

  • Hero Member
  • *****
  • Posts: 607
  • Fear is not real
    • View Profile
    • Personal website of kazi shahin
Re: Java Swing
« Reply #2 on: August 01, 2010, 04:57:28 PM »
The above program code makes the window & set it by default at the left top. The below code will make a window & adjust it at the middle of a screen


import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class CenterOnScreen extends JFrame {

    public CenterOnScreen() {

        setSize(300, 200);
        setTitle("CenterOnScreen");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        Toolkit toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        setLocation(size.width/2 - getWidth()/2,
      size.height/2 - getHeight()/2);
    }

    public static void main(String[] args) {

        CenterOnScreen cos = new CenterOnScreen();
        cos.setVisible(true);

    }
}



To center the window on the screen, you must know the resolution of the monitor. For this, here it is useing the Toolkit class.

 Toolkit toolkit = getToolkit();
 Dimension size = toolkit.getScreenSize();


We get the toolkit and figure out the screen size.

 setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);

To place the window on the screen, here it need to call the setLocation() method
Kazi Shahin                   
092-15-795
Department of CSE   
Cell : 01718 699 590
Blood Group: O+
Google + :  https://plus.google.com/u/0/101741817431143727344/about?hl=en
Facebook : http://www.facebook.com/kazishahin.rahman
Web : http://www.kazishahin.com/

Offline kazi shahin

  • Hero Member
  • *****
  • Posts: 607
  • Fear is not real
    • View Profile
    • Personal website of kazi shahin
Re: Java Swing
« Reply #3 on: August 02, 2010, 04:16:51 PM »
The below code is about J button.There are two buttons here. The first button will beep a sound and the second button will close the window.




import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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


public class Buttons extends JFrame {

    private Toolkit toolkit;

    public Buttons() {

        setTitle("Buttons");
        setSize(300, 200);

        toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        setLocation((size.width - getWidth())/2, (size.height - getHeight())/2);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        getContentPane().add(panel);

   panel.setLayout(null);

        JButton beep = new JButton("Beep");
        beep.setBounds(150, 60, 80, 30);
        beep.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                toolkit.beep();
            }
        });

       JButton close = new JButton("Close");
       close.setBounds(50, 60, 80, 30);
       close.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent event) {
               System.exit(0);
          }
       });

        panel.add(beep);
        panel.add(close);

    }

    public static void main(String[] args) {

        Buttons buttons = new Buttons();
        buttons.setVisible(true);

    }
}




The above code showing you about Layout management and event handling also.  

JPanel panel = new JPanel();
 getContentPane().add(panel);

It  creates a JPanel component. It is a generic lightweight container.  Added the JPanel to the JFrame.

 panel.setLayout(null);
By default, the JPanel has a FlowLayout manager. The layout manager is used to place widgets onto the containers. If we call setLayout(null) we can position our components absolutely. For this, just use the setBounds() method.

 JButton beep = new JButton("Beep");
 beep.setBounds(150, 60, 80, 30);
 beep.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent event) {
         toolkit.beep();
     }
 });

Here the code create a button. Positioning it by calling the setBounds() method. Then it is necessary to  add an action listener. The action listener will be called, when we perform an action on the button. In our case, if we click on the button. The beep button will play a simple beep sound.

 System.exit(0);
The close button will exit the application. For this, we call the System.exit() method.

panel.add(beep);
 panel.add(close);

In order to show the buttons, we must add them to the pane

The output is below

             

Kazi Shahin                   
092-15-795
Department of CSE   
Cell : 01718 699 590
Blood Group: O+
Google + :  https://plus.google.com/u/0/101741817431143727344/about?hl=en
Facebook : http://www.facebook.com/kazishahin.rahman
Web : http://www.kazishahin.com/

Offline ananda

  • Faculty
  • Jr. Member
  • *
  • Posts: 57
    • View Profile
Re: Java Swing
« Reply #4 on: August 06, 2010, 11:48:29 PM »
It is really a good effort. Please keep it up. One opinion, please start from the very beginning. That is how the platform will be set up and what software required to install (NetBean , Eclipse etc.  ). Actually, please ahead in such a way that one who is not totally new in Java also get interest to learn by this post.

Again I would like to thank you for this good initiatives.

......................................
Kamanashis Biswas
Assistant Professor
CSE, CIS & CS, FSIT

[Happiness never decreases by being shared - Goutam Buddha]

Offline kazi shahin

  • Hero Member
  • *****
  • Posts: 607
  • Fear is not real
    • View Profile
    • Personal website of kazi shahin
Re: Java Swing
« Reply #5 on: August 07, 2010, 12:25:40 AM »
Dear sir

I'm delighted to find you on this topic. You are very much right.
I'm preparing myself & also preparing a tutorial.

To run Java program at first we need to install Java software. Java software for your computer, or the Java Runtime Environment, is also referred to as the Java Runtime, Runtime Environment, Runtime, JRE, Java Virtual Machine, Virtual Machine, Java VM, JVM, VM, or Java download.

It is free & you can download it from the internet.

Your cooperation is highly appreciate-able & I'm looking forward to get your cooperation.
« Last Edit: August 07, 2010, 12:29:46 AM by kazi shahin »
Kazi Shahin                   
092-15-795
Department of CSE   
Cell : 01718 699 590
Blood Group: O+
Google + :  https://plus.google.com/u/0/101741817431143727344/about?hl=en
Facebook : http://www.facebook.com/kazishahin.rahman
Web : http://www.kazishahin.com/

Offline ananda

  • Faculty
  • Jr. Member
  • *
  • Posts: 57
    • View Profile
Re: Java Swing
« Reply #6 on: August 07, 2010, 11:21:28 AM »
Dear Shahin,

Thanks for your reply. I would try my best to participate in this forum and keep in mind that I am also a learner. I would share my experiences if needed. Moreover, I would like to request other CSE faculties (Kaiser & Monir sir) to join in this forum as they are conducting object oriented programming. If they ask their students to participate here, then it will be fruitful for all of us.


With thanks-
Kamanashis Biswas
......................................
Kamanashis Biswas
Assistant Professor
CSE, CIS & CS, FSIT

[Happiness never decreases by being shared - Goutam Buddha]