Ein kleines Beispiel für die GUI Programmierung in Java:
Zunächst die Main Class:
[code:1]
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main
{
/**
* @param args
*/
public static void main(String[] args)
{
Window myWindow = new Window();
myWindow.pack(); //Fenster so groß, dass alles reinpasst
myWindow.setSize( 400 , 150 );
myWindow.setLocation( 500 , 250 );
myWindow.setResizable( false );
myWindow.setVisible( true );
Date now = new Date();
myWindow.setTitle( now.toString() );
}
}
[/code:1]
Und die Window Klasse:
[code:1]
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Window extends JFrame implements ActionListener
{
private JButton okButton = new JButton( "OK");
private JButton endButton = new JButton( "Ende");
private JTextField inputField = new JTextField( "", 20 );
private JTextField outputField= new JTextField( "", 20 );
public Window()
{
super( "Windows 2006" );
JPanel cp = new JPanel( );
JPanel cpbutton = new JPanel( );
cp.setLayout( new GridLayout( 3, 1, 5, 5 ) );
cp.add( inputField );
cp.add( outputField );
cp.add( cpbutton );
cpbutton.add( okButton );
cpbutton.add( endButton );
okButton.addActionListener( this );
endButton.addActionListener( this );
setContentPane( cp );
}
public void actionPerformed( ActionEvent event)
{
Object source = event.getSource();
if (source == okButton )
{
doOK();
}
if (source == endButton )
{
doEnd();
}
}
private void doOK()
{
outputField.setText(inputField.getText() );
inputField.setText("");
}
private void doEnd()
{
System.exit(0);
}
}
[/code:1]
Zunächst die Main Class:
[code:1]
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main
{
/**
* @param args
*/
public static void main(String[] args)
{
Window myWindow = new Window();
myWindow.pack(); //Fenster so groß, dass alles reinpasst
myWindow.setSize( 400 , 150 );
myWindow.setLocation( 500 , 250 );
myWindow.setResizable( false );
myWindow.setVisible( true );
Date now = new Date();
myWindow.setTitle( now.toString() );
}
}
[/code:1]
Und die Window Klasse:
[code:1]
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Window extends JFrame implements ActionListener
{
private JButton okButton = new JButton( "OK");
private JButton endButton = new JButton( "Ende");
private JTextField inputField = new JTextField( "", 20 );
private JTextField outputField= new JTextField( "", 20 );
public Window()
{
super( "Windows 2006" );
JPanel cp = new JPanel( );
JPanel cpbutton = new JPanel( );
cp.setLayout( new GridLayout( 3, 1, 5, 5 ) );
cp.add( inputField );
cp.add( outputField );
cp.add( cpbutton );
cpbutton.add( okButton );
cpbutton.add( endButton );
okButton.addActionListener( this );
endButton.addActionListener( this );
setContentPane( cp );
}
public void actionPerformed( ActionEvent event)
{
Object source = event.getSource();
if (source == okButton )
{
doOK();
}
if (source == endButton )
{
doEnd();
}
}
private void doOK()
{
outputField.setText(inputField.getText() );
inputField.setText("");
}
private void doEnd()
{
System.exit(0);
}
}
[/code:1]
mfg KC