Java: JFrame tester
Our first submission:
So here is my silly Java JFrame that I use to test all sorts of things; Swing components, logging frameworks or just to see pretty colors on my screen.Really though, just follow the comments and play with what a JFrame can do. In addition there is a convenient comment that instructs you where to place your code to make this handy JFrame do something you need it to do.
It only relies on standard Java stuff like Swing and AWT libraries so it should work for most anyone.
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.WindowConstants; /** * It is a Java Swing Window or JFrame to be specific. * * @author MasterJigger * */ public class JavaTestFrame extends JFrame implements KeyListener { private static final long serialVersionUID = 1L; public JavaTestFrame() { super("Jigcode Java Test Frame"); setSize(new Dimension(700, 500)); //This is a usable window size //Center me on the screen Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension screenSize = toolkit.getScreenSize(); int x = (screenSize.width - getWidth()) / 2; int y = (screenSize.height - getHeight()) / 2; setLocation(x, y); //close and exit when the user request it! setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent winEvt) { System.exit(0); } }); addKeyListener(this); //Lets show keystrokes in the consol for something to do! /** * This is where the usefulness comes in, place what you need on the JFrame here. * Try Swing components out, test logging frameworks, see pretty colors. */ //********************************************************************************************* getContentPane().setLayout(new BorderLayout()); //Setup to put something useful on the JFrame add(new JLabel("Hello fellow Jigcoders"), BorderLayout.CENTER); //Time for something useful //********************************************************************************************* setUndecorated(false); //false will display frame decorations ha ha! that's what Java says, these are the min/max/close etc on the title bar setResizable(true); //pretty self-explanatory setFocusable(true); //a must have for any good frame setVisible(true); //unless you want to keep this for yourself it should be visible. requestFocus(); //selfish but necessary } /** * @param args */ public static void main(String[] args) { new JavaTestFrame(); } public void keyPressed(KeyEvent e) { System.out.println(e.getKeyChar()); } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } }


Comments are closed for this entry.