Home | Course Index | Next >> | PDF Version of this Page |
Course API Comparison
|
For an introduction into Javas AWT- and Swing-APIs see: AWT and Swing.
1) Install Eclipse Platform 3.4.2.
2) Start Eclipse → File → New → Java Project → Project name: compareJava → Finish.
3) Main menu of Eclipse → File → New → Class → Name: compareJava → Finish.
4) Minimize the Welcome-tab of Eclipse and remove any default code from compareJava.java.
Copy the following 64 lines of code into the empty compareJava.java:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; class compareJava extends JFrame implements ActionListener, WindowListener { public static void main( String args[] ) { new compareJava(); } JButton button1 = new JButton( "Talk!" ); JButton button2 = new JButton( "Clear" ); JLabel label = new JLabel ( "" ); public compareJava() { super( "Java Program" ); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setSize( 600, 80 ); Container pane = getContentPane(); JPanel panel = new JPanel( new BorderLayout() ); Border border = BorderFactory.createLineBorder( Color.BLACK, 3 ); Border margin = new EmptyBorder( 3,3,3,3 ); panel.setBorder( new CompoundBorder( border, margin ) ); panel.add( button1, BorderLayout.WEST ); panel.add( button2, BorderLayout.EAST ); panel.add( label , BorderLayout.CENTER ); label.setOpaque( true ); label.setBackground( Color.GRAY ); label.setHorizontalAlignment( SwingConstants.CENTER ); pane.add( panel ); ActionListener click = new ActionListener() { public void actionPerformed( ActionEvent actionEvent ) { if ( (JButton)actionEvent.getSource() == button1 ) { label.setText( "Java Application. Resize!" ); } else label.setText( "" ); } }; ComponentListener resize = new ComponentListener() { public void componentResized( ComponentEvent evt ) { Component c = (Component)evt.getSource(); double w = (c.getSize()).getWidth(); Font font = new Font( "Arial", Font.BOLD, (int)(w/25) ); button1.setFont( font ); label .setFont( font ); button2.setFont( font ); } public void componentMoved ( ComponentEvent evt ) {} public void componentShown ( ComponentEvent evt ) {} public void componentHidden( ComponentEvent evt ) {} }; pane.addComponentListener( resize ); button1.addActionListener( click ); button2.addActionListener( click ); addWindowListener(this); setVisible(true); } protected void processWindowEvent(WindowEvent e) { if (e.getID() == WindowEvent.WINDOW_CLOSING) System.exit(0); super.processWindowEvent(e); } public void windowClosing ( WindowEvent e ) {} public void windowOpened ( WindowEvent e ) {} public void windowIconified ( WindowEvent e ) {} public void windowDeiconified( WindowEvent e ) {} public void windowClosed ( WindowEvent e ) {} public void windowActivated ( WindowEvent e ) {} public void windowDeactivated( WindowEvent e ) {} public void actionPerformed ( ActionEvent e ) {} }
Main menu of Eclipse → Run → Run → Save and Launch → Check this CheckBox: Always save resources before launching → OK.
top of page: |