JAVA Swing Tutorials


Java 2 makes it easy to create windows with the Swing class that is made available by declaring "import javax.swing.*;" A JFrame sub class of Java Swing creates windows frames that can be resized, maximized, minimized and closed. JFrame class and specifies four features: 1. The super() method takes a string argument that will appear on the windows title bar. 2. The setSize() method takes two arguments that set the windows initial width and height. 3. The setDefaultCloseOperation() method takes a single argument to specify and action when the window closes. 4. The setVisible() method takes a boolean arguments of true to make window actually appear. Ex.

import javax.swing.*;
class SwingWindow extends JFrame
{
 public SwingWindow()
   {
     super("Hellow World");
     setSize(300,100);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setVisible(true);
   }
}

public class HellowSwing
{
   public static void main(String[] args)
   {
    SwingWindow Hellow = new SwingWindow();
  }
}



The previous program creates the windows shown below that has all the usual window feature - but lacks a content area. A Container class is included in Java Abstract Windowing Toolkit class made available with import java.awt.*; An instance of a Container class is created with the "new" Java keyword and the getContentPane() method.

Container contentArea = getCintentPain();
contentArea .setBackgrounf(Color:white);



Positioning of interface components in a windows is not specified absolutely by the program but should instead use a Java layout-manager. Swing has a JButton class that is used in the program opposite to create interface push buttons in the Java Swing window that is illustrated bellow

import javax.awing.*;
import java.awt.*;

class SwingWindow extends JFrame
{
  public SwingWindow()
   {
     super("Push Button");
     setSize(300,100);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setVisible(true);

    Container contentArea = getCintentPain();
    contentArea .setBackgrounf(Color:white);

    FlowLayout flowManager = new FlowLayout();
   contentArea.setLayout(flowManager);

  JButton playButton = new JButton("play");
  contentArea.add(playButton );

  JButton pauseButton = new JButton("pause");
  contentArea.add(pauseButton );

  JButton stopButton = new JButton("stop");
  contentArea.add(stopButton );

  setContentPane(contentArea);
  }
}

public class SwingButtons
{
   public static void main(String[] args)
   {
    SwingWindow Buttons= new SwingWindow();
  }
}



The label is created as an instance of the JLabel class using the Java "new" keyword and the JLabel() method. A text field is created as an instance of the JTextField class using the Java "new" keyword the JTextField method.

JLabel textLabel = new JLabel ("TEST");
contentArea.add(textLabel );

JTextField text = new JTestField("text value",15);
contentArea.add(text);

setCotentPane(contentArea);


Comments


Post Comments

  • 0 comments