JFrame utilites
Page 1 of 1
JFrame utilites
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...
Now lets have the screenshot of the output-
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:
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-
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!!!
Similar topics
» Reusing JFrame As windowUtilities
» Java Swing: adding confirmation dialogue for closing Window in JFrame
» Java Swing: adding confirmation dialogue for closing Window in JFrame
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford