// This program creates a blank window, and shows how to
// set the size and title.
// Written 1999 by Wayne Pollock, Tampa FL USA.
import java.awt.*;
public class SimpleGUI1
{ public static void main ( String [] args )
{ Frame myFrame = new Frame();
myFrame.setTitle( "Wonder Window!" );
myFrame.setSize( 250, 100 );
myFrame.setVisible( true );
}
}
|
// This program creates a blank window, and shows how to
// set the size and title. It also shows the use of a
// WindowListener, so the window can be closed by clicking
// the close box.
// Written 1999 by Wayne Pollock, Tampa FL USA.
import java.awt.*;
import java.awt.event.*;
public class SimpleGUI2
{ public static void main ( String [] args )
{ Frame myFrame = new Frame();
myFrame.setTitle( "Wonder Window!" );
myFrame.setSize( 250, 100 );
myFrame.setVisible( true );
MyWindowHandler mwh = new MyWindowHandler();
myFrame.addWindowListener( mwh );
}
}
class MyWindowHandler implements WindowListener
{
public void windowClosed ( WindowEvent we ) { }
public void windowDeiconified ( WindowEvent we ) { }
public void windowIconified ( WindowEvent we ) { }
public void windowActivated ( WindowEvent we ) { }
public void windowDeactivated ( WindowEvent we ) { }
public void windowOpened ( WindowEvent we ) { }
public void windowClosing ( WindowEvent we ) { System.exit( 0 ); }
}
|
// This program creates a blank window, and shows how to
// set the size and title. This version shows the use of
// a WindowAdapter class. Since this program doesn't extend
// from any other class, it could extend from WindowAdapter. But
// applets and many other programs will already extend from some
// class or another, so a separate class is needed.
// Written 1999 by Wayne Pollock, Tampa FL USA.
import java.awt.*;
import java.awt.event.*;
public class SimpleGUI3
{ public static void main ( String [] args )
{ Frame myFrame = new Frame();
myFrame.setTitle( "Wonder Window!" );
myFrame.setSize( 250, 100 );
myFrame.setVisible( true );
MyWindowHandler mwh = new MyWindowHandler();
myFrame.addWindowListener( mwh );
}
}
class MyWindowHandler extends WindowAdapter
{ public void windowClosing ( WindowEvent we )
{ System.exit( 0 );
}
}
|
// This program creates a blank window, and shows how to
// set the size and title. The class itself implements the
// WindowListener interface. Since an object must be constructed,
// Most of the window details have been moved from "main" to the
// constructor. (Note the use of an implied "this" in front of
// methods such as "setTitle".)
// Written 1999 by Wayne Pollock, Tampa FL USA.
import java.awt.*;
import java.awt.event.*;
public class SimpleGUI4 extends Frame implements WindowListener
{
public static void main ( String [] args )
{ Frame myFrame = new SimpleGUI4();
}
SimpleGUI4 ()
{ setTitle( "Wonder Window!" );
setSize( 250, 100 );
setVisible( true );
addWindowListener( this );
}
public void windowClosed ( WindowEvent we ) { }
public void windowDeiconified ( WindowEvent we ) { }
public void windowIconified ( WindowEvent we ) { }
public void windowActivated ( WindowEvent we ) { }
public void windowDeactivated ( WindowEvent we ) { }
public void windowOpened ( WindowEvent we ) { }
public void windowClosing ( WindowEvent we ) { System.exit( 0 ); }
}
|
// This program creates a blank window, and shows how to
// set the size and title. In this version, an "anonymous"
// inner class is used. (An inner class is one defined inside
// another class, an anonymous class is an un-named class.) This
// is probably the most commonly used method for simple event
// handlers, although anonymous inner classes don't work too well
// in more complex cases. (And are darned ugly too!)
// Written 1999 by Wayne Pollock, Tampa FL USA.
import java.awt.*;
import java.awt.event.*;
public class SimpleGUI5 extends Frame
{
public static void main ( String [] args )
{ Frame myFrame = new SimpleGUI5();
}
SimpleGUI5 ()
{ setTitle( "Wonder Window!" );
setSize( 250, 100 );
setVisible( true );
addWindowListener( new WindowAdapter()
{ public void windowClosing ( WindowEvent we )
{ System.exit( 0 );
}
}
);
}
}
|
// This program creates a window, and shows how to add an
// Icon, set a Font, add a Label, and center the Frame on
// the screen. (We do it all!)
// Written 2001 by Wayne Pollock, Tampa FL USA.
import java.awt.*;
import java.awt.event.*;
public class SimpleGUI6 extends Frame
{
public static void main ( String [] args )
{ Frame myFrame = new SimpleGUI6();
myFrame.addWindowListener( new WindowAdapter()
{ public void windowClosing ( WindowEvent we )
{ System.exit( 0 );
}
}
);
// Shrink the Frame to be just big enough for the contents:
// myFrame.pack();
myFrame.setVisible( true ); // Frames can use toFront, toBack too.
}
SimpleGUI6 ()
{ setTitle( "Wonder Window!" );
setSize( 350, 200 );
// The AWT toolkit has lots of useful methods:
Toolkit tk = Toolkit.getDefaultToolkit();
// Center the Frame on the screen:
int screenWidth = tk.getScreenSize().width;
int screenHeight = tk.getScreenSize().height;
int frameWidth = getWidth();
int frameHeight = getHeight();
setLocation( ( screenWidth - frameWidth ) / 2,
( screenHeight - frameHeight ) / 2 );
// Add an Icon:
Image icon = tk.getImage( "WPIcon.gif" );
setIconImage( icon );
// Set the Font:
setFont( new Font( "Serif", Font.BOLD, 18 ) );
// Add a Label:
add( new Label( "Is this cool or what?", Label.CENTER ) );
}
}
|
// This program creates a blank window, and shows how to
// set the size and title. This version shows the swing JFrame
// and how to handle window closing.
// (C) 2003 by Wayne Pollock, Tampa FL USA. All Rights Reserved.
import javax.swing.*;
public class SimpleGUI3Swing
{ public static void main ( String [] args )
{ JFrame myFrame = new JFrame();
myFrame.setTitle( "Wonder Window!" );
myFrame.setSize( 250, 100 );
myFrame.setVisible( true );
myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
|
// This swing GUI program creates a window, shows how to add an
// Icon, set a Font, add a Button (which changes color when clicked),
// and center the Frame on the screen. (We do it all!)
// Written 2003 by Wayne Pollock, Tampa FL USA.
// Modified 6/2014: Added Java 8 Lambda syntax for ActionListener.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleGUI6Swing extends JFrame {
public static void main ( String [] args ) {
SimpleGUI6Swing myFrame = new SimpleGUI6Swing();
myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
myFrame.setVisible( true ); // Frames can use toFront, toBack too.
}
SimpleGUI6Swing () {
setTitle( "Wonder Window - swing style!" );
//pack(); // Shrink the Frame to be just big enough for the contents
setSize( 350, 200 ); // or set the window size.
setLocationRelativeTo( null ); // Center window on screen
// Add an Icon:
ImageIcon icon = new ImageIcon( getClass().getResource("WPIcon.gif") );
setIconImage( icon.getImage() ); // Use default icon if no image loaded.
// Add a button:
final JButton b = new JButton( "Change the color" );
// Set the button's Font:
b.setFont( new Font( "Serif", Font.BOLD, 18 ) );
// Add button to JFrame's content pane:
add( b );
// Add event listener for button:
b.addActionListener( e -> { b.setBackground( Color.BLUE ); } );
}
}
|
// JavaFx version of SimpleGUI6.
// From Oracle's JavaFx API docs (for class Stage).
//
// With JavaFx, the top-level window is a "Stage", which contains a "Scene".
// A Scene contains a hierarchy of "Nodes", such as Buttons and Text (label).
// Nodes can be styled using standard CSS stylesheets; Oracle doesn't supply
// any native platform stylesheets however. (The top-level window *is*
// native.)
//
// Written 8/2014 by Wayne Pollock, Tampa Florida USA.
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.text.*;
import javafx.stage.Stage;
public class SimpleGUIJavaFx extends Application {
public static void main(String[] args) {
Application.launch(args); // Invokes the start method
}
@Override public void start(Stage stage) {
Text text = new Text(10, 40, "SimpleGUI6 - JavaFX version");
text.setFont(new Font(40));
Scene scene = new Scene(new Group(text));
stage.setTitle("Wonder Window");
stage.setScene(scene);
stage.sizeToScene();
stage.show();
}
}
|