ew JButton ( Red ) ;. addActionListener (this) ;. add (stopButton); prepareButton=new JButton ( Yellow ) ;. addActionListener (this) ;. add (prepareButton); goButton=new JButton ( Green ) ;. addActionListener (this) ;. add (goButton) ; resetButton=new JButton ( Reset ) ;. addActionListener (this) ;. add (resetButton) ;. add (buttonPanel, BorderLayout.SOUTH) ;. first (deckPanel);//Optional
}
void actionPerformed (ActionEvent e)
{actionCommand=e.getActionCommand ();
(actionCommand.equals ( Red )). show (deckPanel, red ); if (actionCommand.equals ( Yellow )). show (deckPanel, yellow raquo ;); if (actionCommand.equals ( Green )). show (deckPanel, green ); if (actionCommand.equals ( Reset )). show (deckPanel, start ) ;. out.println ( Error in CardLayout Demo. );
} static void main (String [] args)
{demoGui=new CardLayoutDemo () ;. setVisible (true);}}
Додаток 10.javax.swing. *; java.awt. *; java.awt.event. *; class DrawStringDemo extends JFrameActionListener
{static final int WIDTH=350; static final int HEIGHT=200; static final int X_START=20; static final int Y_START=100; static final int POINT_SIZE=24;
String theText= Push a button! raquo ;;
static void main (String [] args)
{w=new DrawStringDemo () ;. setVisible (true);
}
DrawStringDemo ()
{(WIDTH, HEIGHT); contentPane=getContentPane (); (new WindowDestroyer ()); ( drawString Demonstration ) ;. setBackground (Color.white) ;. setLayout (new BorderLayout ( ));
buttonPanel=new JPanel (); helloButton=new Button ( Hello ) ;. addActionListener (this) ;. add (helloButton); byeButton=new Button ( Goodbye ) ;. addActionListener (this) ;. add (byeButton) ;. add (buttonPanel, BorderLayout.SOUTH);
}
void paint (Graphics g)
{. paint (g); f=Font ( Serif raquo ;, Font.BOLD | Font.ITALIC, POINT_SIZE) ;. setFont (f) ;. drawString (theText, X_START, Y_START) ;
}
void actionPerformed (ActionEvent e)
{(e.getActionCommand (). equals ( Hello ))= How are you. raquo ;; if (e.getActionCommand (). equals ( Goodbye )) = It was good talking with you. raquo ;;= Error in button interface. raquo ;;
();
}
}
Додаток 10
javax.swing. *; java.awt. *; java.awt.event. *;
class MenuAdd extends JFrame implements ActionListener
{static final int WIDTH=600; static final int HEIGHT=300; static final int LINES=10; static final int CHAR_PER_LINE=40;
JTextArea theText; String memo1= No Memo 1. raquo ;; String memo2= No Memo 2. raquo ;;
MenuAdd ()
{(WIDTH, HEIGHT); (new WindowDestroyer ()); ( Memo Saver ); contentPane=getContentPane () ;. setLayout (new BorderLayout ());
memoMenu=new JMenu ( Memos ); m;
=new JMenuItem ( Save Memo 1 ) ;. addActionListener (this) ;. add (m);
=new JMenuItem ( Save Memo 2 ) ;. addActionListener (this) ;. add (m);
=new JMenuItem ( Get Memo 1 ) ;. addActionListener (this) ;. add (m);
=new JMenuItem ( Get Memo 2 ) ;. addActionListener (this) ;. add (m);
=new JMenuItem ( Clear ) ;. addActionListener (this) ;. add (m);
=new JMenuItem ( Exit ) ;. addActionListener (this) ;. add (m);
mBar=new JMenuBar () ;. add (memoMenu) ;. add (mBar, BorderLayout.SOUTH);
textPanel=new JPanel () ;. setBackground (Color.blue);=new JTextArea (LINES, CHAR_PER_LINE) ;. setBackground (Color.white) ;. add (theText) ;. add (textPanel , BorderLayout.CENTER);
}
void actionPerformed (ActionEvent e)
{actionCommand=e.getActionCommand (); (actionCommand.equals ( Save Memo 1 ))=theText.getText (); if (actionCommand.equals ( Save Memo 2 ) )=theText.getText (); if (actionCommand.equals ( Clear )). setText ( ); if (actionCommand.equals ( Get Memo 1 )). setText (memo1); if (actionCommand.equals ( Get Memo 2 )). setText (memo2); if (actionCommand.equals ( Exit )). exit (0) ;. setText ( Error in memo interface );
}
static void main (String [] args)
{gui=new MenuAdd () ;. setVisible (true);
}
...