





































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Introduction to Advanced Studies II; Subject: Computer Science; University: Illinois Institute of Technology; Term: Unknown 1989;
Typology: Study notes
1 / 77
This page cannot be seen from the preview
Don't miss anything!






































































22 String string = "This is a demo string to\n" + 23 "illustrate copying text\n" + 24 "from one TextArea to \n" + 25 "another TextArea using an\n" + "external event\n"; 26 27 // set up textArea 28 textArea1 = new JTextArea( string, 10, 15 ); 29 box.add( new JScrollPane( textArea1 ) ); 30 31 // set up copyButton 32 copyButton = new JButton( "Copy >>>" ); 33 copyButton.addActionListener( 34 36 new ActionListener() { 37 38 // set text in textArea2 to selected 39 // text from textArea 40 public void actionPerformed( ActionEvent event ) 41 { 42 textArea2.setText( textArea1.getSelectedText() ); 43 }
45 } // end anonymous inner class 46 47 ); // end call to addActionListener 48 49 box.add( copyButton ); 50 51 // set up textArea 52 textArea2 = new JTextArea( 10, 15 ); 53 textArea2.setEditable( false ); 54 box.add( new JScrollPane( textArea2 ) ); 55 56 // add box to content pane 57 Container container = getContentPane(); 58 container.add( box ); // place in BorderLayout.CENTER 59 60 setSize( 425, 200 ); 61 setVisible( true ); 62 } 63
1 // CustomPanel.java 2 // A customized JPanel class. 3 4 // Java core packages 5 import java.awt.; 6 7 // Java extension packages 8 import javax.swing.; 9 10 public class CustomPanel extends JPanel { 11 public final static int CIRCLE = 1, SQUARE = 2; 12 private int shape; 13 14 // use shape to draw an oval or rectangle 15 public void paintComponent( Graphics g ) 16 { 17 super.paintComponent( g ); 18 19 if ( shape == CIRCLE ) 20 g.fillOval( 50, 10, 60, 60 ); 21 else if ( shape == SQUARE )
1 // CustomPanelTest.java 2 // Using a customized Panel object. 3 4 // Java core packages 5 import java.awt.; 6 import java.awt.event.; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class CustomPanelTest extends JFrame { 12 private JPanel buttonPanel; 13 private CustomPanel myPanel; 14 private JButton circleButton, squareButton; 15 16 // set up GUI 17 public CustomPanelTest() 18 { 19 super( "CustomPanel Test" ); 20
21 // create custom drawing area 22 myPanel = new CustomPanel(); 23 myPanel.setBackground( Color.green ); 24 25 // set up squareButton 26 squareButton = new JButton( "Square" ); 27 squareButton.addActionListener( 28 29 // anonymous inner class to handle squareButton events 30 new ActionListener() { 31 32 // draw a square 33 public void actionPerformed( ActionEvent event ) 34 { 35 myPanel.draw( CustomPanel.SQUARE ); 36 } 37 38 } // end anonymous inner class 39 40 ); // end call to addActionListener 41
64 // attach button panel & custom drawing area to content pane 65 Container container = getContentPane(); 66 container.add( myPanel, BorderLayout.CENTER ); 67 container.add( buttonPanel, BorderLayout.SOUTH ); 68 69 setSize( 300, 150 ); 70 setVisible( true ); 71 } 72 73 // execute application 74 public static void main( String args[] ) 75 { 76 CustomPanelTest application = new CustomPanelTest(); 77 78 application.setDefaultCloseOperation( 79 JFrame.EXIT_ON_CLOSE ); 80 } 81 82 } // end class CustomPanelTest
22 // anonymous inner class for mouse pressed and 23 // released event handling 24 new MouseAdapter() { 25 26 // handle mouse press event 27 public void mousePressed( MouseEvent event ) 28 { 29 x1 = event.getX(); 30 y1 = event.getY(); 31 } 32 33 // handle mouse release event 34 public void mouseReleased( MouseEvent event ) 35 { 36 x2 = event.getX(); 37 y2 = event.getY(); 38 repaint(); 39 } 40 41 } // end anonymous inner class 42
43 ); // end call to addMouseListener 44 45 // set up mouse motion listener 46 addMouseMotionListener( 47 48 // anonymous inner class to handle mouse drag events 49 new MouseMotionAdapter() { 50 51 // handle mouse drag event 52 public void mouseDragged( MouseEvent event ) 53 { 54 x2 = event.getX(); 55 y2 = event.getY(); 56 repaint(); 57 } 58 59 } // end anonymous inner class 60 61 ); // end call to addMouseMotionListener 62 63 } // end constructor