Java Code Snippets
Provides framework templates including Java 2 Application, Java 2 Dialog and Java 2 Applet.
Version 2.0.0.1. Released: 2008-09-02.
References: Java 2 Standard Edition.
/* Standard Java 2 version 1.4+ Application Template
*/
// Uncomment and complete the line below to use pakaging
// package company.appPackage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTemplate {
// Global variables
JFrame frame;
JLabel label;
public JTemplate(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
frame = new JFrame("Title");
label = new JLabel("Template");
// Create panel and add components
final JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
pane.setLayout(new BorderLayout());
pane.add(label, BorderLayout.CENTER);
frame.getContentPane().add(pane, BorderLayout.CENTER);
// Exit app
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* Exit app for more advanced exit functionality
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} // end (method)WindowClosing
} // end (object)WindowAdapter
);
*/
// Sets size and centers the frame
frame.setSize(400, 300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenW = screenSize.width;
int screenH = screenSize.height;
Dimension frameSize = frame.getSize();
int x = (screenW - frameSize.width) / 2;
int y = (screenH - frameSize.height) / 2;
frame.setLocation(x, y);
frame.setVisible(true);
} // end (constructor)JTemplate
// Program entry point
/** Main App Threading for Java 1.4 and higher.
*
* @param sargs Command Line Arguments as a String Array passed from main().
*/
public static void loadApp(String[] sargs) {
JTemplate app = new JTemplate(sargs);
} // end (method)buildApp
/** The main method implementing the application.
*
* @param args Command Line Arguments as a String Array.
*/
public static void main(final String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
loadApp(args);
} // end (method)run
});
} // end (method)main
} // end (class)JTemplate
/* Standard Java 2 Application Template
*/
// Uncomment and complete the line below to use pakaging
// package company.appPackage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTemplate {
// Global variables
JFrame frame;
JLabel label;
public JTemplate() {
frame = new JFrame("Title");
label = new JLabel("Template");
// Create panel and add components
final JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
pane.setLayout(new BorderLayout());
pane.add(label, BorderLayout.CENTER);
frame.getContentPane().add(pane, BorderLayout.CENTER);
// Exit app
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} // end (method)WindowClosing
} // end (object)WindowAdapter
);
/* // Exit app (Java 1.3 and higher)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
*/
// Sets size and centers the frame
frame.setSize(400, 300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenW = screenSize.width;
int screenH = screenSize.height;
Dimension frameSize = frame.getSize();
int x = (screenW - frameSize.width) / 2;
int y = (screenH - frameSize.height) / 2;
frame.setLocation(x, y);
frame.setVisible(true);
} // end (constructor)JTemplate
// Program entry point
/** The main method implementing the application.
*
* @param args Command Line Arguments as a String Array.
*/
public static void main(String[] args) {
JTemplate app = new JTemplate();
} // end (method)main
} // end (class)JTemplate
/* Standard Java 2 Dialog for application
*/
import java.io.*;
import java.io.File;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class JDialogTemplate extends JDialog {
public JDialogTemplate(JFrame aFrame) {
super(aFrame);
// This JDialog method below is for Java 1.4 and higher if you don't have this in the main class file:
// JDialog.setDefaultLookAndFeelDecorated(true);
setTitle("Dialog");
JLabel l = new JLabel("Hello");
JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
pane.setLayout(new BorderLayout());
pane.add(l);
getContentPane().add(pane, BorderLayout.CENTER);
setSize(400, 300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenW = screenSize.width;
int screenH = screenSize.height;
Dimension frameSize = getSize();
int x = (screenW - frameSize.width) / 2;
int y = (screenH - frameSize.height) / 2;
setLocation(x, y);
setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
setResizable(false);
setVisible(true);
} // end (constructor)JDialogTemplate
} // end (class)JDialogTemplate
// Standard Java 2 Applet Template
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JAppletTemplate extends JApplet {
// Global variables
JLabel l;
JMenuBar mb;
JMenu m;
JMenuItem mi;
// Initialise the applet
public void init() {
// Get a parameter from <param name="theText" value="..."> like this below:
// String text = getParameter("theText");
l = new JLabel("You should use object element not applet or embed");
// Setup a menu system
mb = new JMenuBar();
m = new JMenu("Menu");
mb.add(m);
mi = new JMenuItem("Item");
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(getContentPane(), "This is a JApplet
You should use object element not applet or embed");
} // end (method)actionPerformed
});
m.add(mi);
setJMenuBar(mb);
getContentPane().add(l);
} // end (method)init
// Any actions to begin when starting the applet or returning to the webpage
// public void start() {
// } // end (method)start
// Any old applet painting?
// public void paint(Graphics g) {
// } // end (method)paint
// Stop any actions, begun in (method)start, when leaving the webpage
// public void stop() {
// } // end (method)stop
// Your own clean up stuff when the applet is unloaded (such as when you close the web browser).
// public void destroy() {
// } // end (method)destroy
} // end (class)JAppletTemplate
if (a > b) {
// Do this
c = "and this";
} // end if a is greater than b
if (a > b) {
// Do this
c = "and this";
} else {
// Do other
} // end if a is greater than b
if (a > b) {
// Do this
c = "and this";
} else if (a < b) {
// Do that
c = "and this";
} else {
// The same
} // end if a is greater than b
while (c > 25) {
// Do all that
} // end while
// Unfortunately Java's Switch is limited to byte, short, int and char types
switch (var) {
case 1:
a = "Do this";
break;
case 2:
a = "Do that";
break;
default:
a = "Default stuff";
} // end switch var
for (i=0; i<25; i++) {
// Iterate through the range
} // end for i[0-25]
try {
// try something
} catch (ErrorType errorvar) {
// handle error
} // end try-catch
try {
// try something
} catch (ErrorType errorvar) {
// handle error
} finally {
// finalise
} // end try-catch-finally
catch (ErrorType errorvar) {
// handle error
}
Web-based Code Snippets are copywritten 2008 to Legend Scrolls and Peter Davison.
Web-based Code Snippets are licensed under the Creative Commons Attribution 3.0 Unported.
