JFrame utilites

View previous topic View next topic Go down

JFrame utilites

Post by BIT0112-Rokon on Wed Jun 23, 2010 9:44 pm

In my previous tutorial, I show you how to create a Frame in Java. Today Im here with some Frame utilities of Java. So First Take look of our program then I'll try to explain its different part...
Code:

package my.awesome.blog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MyFrame extends JFrame implements ActionListener {

    private JPanel buttonPanel; // reference of buttonPanel that holds all
    // button
    private JPanel textPanel; // reference of textPanel that holds all textArea
    private TextArea textPane; // reference of TextArea by which we display text
    private JButton button; // reference of JButtons
    private String buttonName = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // name of the
    // buttons
    private int width, height; // they hold height and width of Jframe

    public MyFrame() {
        setTitle("My Frame utitlity");
        Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); // to get
        // the
        // screen
        // size
        // of
        // your
        // display
        this.width = (int) (size.getWidth() - 500); // set the width and cast it
        // to integer
        this.height = (int) (size.getHeight() - 100); // set the height and cast
        // it to integer
        setSize(width, height); // set the size of Frame

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set Default close
        // Oparation
        setLocationRelativeTo(null);// so that our frame can appear in the
        // middle of our screen
        setLayout(new FlowLayout()); // set the layout of Our Frame, here I used
        // Flow layout

        init(); // calling the init method and here all internal functionalities
        // is going to be started
        setVisible(true); // and at lastly visible the frame
    }

    public void init() {
        buttonPanel = new JPanel(); // instantiate the buttonPanel
        buttonPanel.setLayout(new GridLayout(4, 7, 20, 20)); // set the layout
        // of the button
        // panel for the
        // content of
        // its
        buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel")); // an
        // excellent
        // utility
        // the
        // panel
        // to
        // create
        // a
        // border
        buttonPanel.setPreferredSize(new Dimension(width, 6 * height / 10)); // set
        // preferred
        // size

        textPanel = new JPanel();// instance of textPanel
        textPanel.setBorder(BorderFactory.createTitledBorder("Text Panel")); // border
        // utility
        // as
        // before
        // in
        // button
        // panel
        textPanel.setLayout(new BorderLayout()); // set the layout of
        // textPanel.. here i used
        // BorderLayout
        textPanel.setPreferredSize(new Dimension(width, 3 * height / 10));// set
        // preferred
        // size
        // of
        // textpanel

        textPane = new TextArea(); // here I import awt library for textarea
        // instead fo JTextArea. Because by default
        // we can get here Scrollber

        addTextFieldToTheTextPanel(); // method call of adding text pane in the
        // TextPanel
        addButtonsTobuttonPanel(); // method call of adding button panel in the
        // ButtonPanel

        add(textPanel); // add text panel to the Frame
        add(buttonPanel); // add button panel in the Frame
    }

    public void addButtonsTobuttonPanel() {

        // this part pretty much easy..
        // just use a for loop to instantiate button and use substring() method
        // of String class to the
        // buttonName string to get the buttonName
        for (int i = 0; i < buttonName.length(); i++) {
            String temp = buttonName.substring(i, i + 1);
            button = new JButton(temp);
            button.setFont(new Font("Verdana", Font.BOLD, 32));
            button.addActionListener(this);// add actionListener of the button
            // that
            // is implemented with our Frame already

            buttonPanel.add(button);
        }
        button = new JButton("Enter");
        buttonPanel.add(button);
        button.setFont(new Font("Verdana", Font.BOLD, 16));
        button.addActionListener(this);// add actionListener of the button that
        // is implemented with our Frame already

        button = new JButton("Space");
        buttonPanel.add(button);
        button.setFont(new Font("Verdana", Font.BOLD, 16));
        button.addActionListener(this);
    }

    public void addTextFieldToTheTextPanel() {
        textPane.setFont(new Font("Verdana", Font.PLAIN, 32));// set the font
        // for the
        // textArea
        textPane.setSize(width - 40, 3 * height / 10); // set the size of
        // textArea
        textPanel.add(textPane, BorderLayout.CENTER); // add the textarea to the
        // textPanel
    }

    // implementation of ActionListener interface
    @Override
    public void actionPerformed(ActionEvent e) {
        String temp = textPane.getText();
        if (e.getActionCommand().equalsIgnoreCase("enter")) {
            textPane.setText(temp + "\n");
        } else if (e.getActionCommand().equalsIgnoreCase("space")) {
            textPane.setText(temp + " ");
        } else {
            textPane.setText(temp + e.getActionCommand());
        }
    }

    public static void main(String[] args) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {

        // set lookandfeel to get our JFrame more locative ...
        UIManager
                .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        new MyFrame();
    }

}


Now lets have the screenshot of the output-
[click on the image to get it larger]






I think, everything is clear to you. Here I have written two methods, addTextFieldToTheTextPanel() and addButtonsTobuttonPanel(), which may not important to you and it is possible to make the same output without writing those. I think it is a good practice to write these type of method. Why it is good practice I'll tell you someday. Here I show you some simple utilities, you can extend more ...
Cheers!!!

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum