length buttons count new JButton Button count 1 26 27 end

Length buttons count new jbutton button count 1 26 27

This preview shows page 11 - 14 out of 22 pages.

; count < buttons.length; count++ ) 24 { 25 buttons[ count ] = new JButton( "Button " + ( count + 1 ) ); 26 27 } // end for 28 29 30 } // end PanelFrame constructor 31 } // end class PanelFrame Fig. 14.45 | JPanel with five JButtons in a GridLayout attached to the SOUTH region of a BorderLayout . 1 // Fig. 14.46: PanelDemo.java 2 // Testing PanelFrame. 3 import javax.swing.JFrame; 4 5 public class PanelDemo extends JFrame 6 { 7 public static void main( String[] args ) 8 { 9 PanelFrame panelFrame = new PanelFrame(); 10 panelFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 11 panelFrame.setSize( 450 , 200 ); // set frame size Fig. 14.46 | Test class for PanelFrame . (Part 1 of 2.) private JPanel buttonJPanel; // panel to hold buttons buttonJPanel = new JPanel(); // set up panel buttonJPanel.setLayout( new GridLayout( 1 , buttons.length ) ); buttonJPanel.add( buttons[ count ] ); // add button to panel add( buttonJPanel, BorderLayout.SOUTH ); // add panel to JFrame
Image of page 11
14.20 JTextArea 615 14.20 JTextArea A JTextArea provides an area for manipulating multiple lines of text . Like class JTextField , JTextArea is a subclass of JTextComponent , which declares common methods for JText- Field s, JTextArea s and several other text-based GUI components. The application in Figs. 14.47–14.48 demonstrates JTextArea s. One JTextArea dis- plays text that the user can select. The other is uneditable and is used to display the text the user selected in the first JTextArea . Unlike JTextField s, JTextArea s do not have action events—when you press Enter while typing in a JTextArea , the cursor simply moves to the next line. As with multiple-selection JList s (Section 14.13), an external event from another GUI component indicates when to process the text in a JTextArea . For example, when typing an e-mail message, you normally click a Send button to send the text of the message to the recipient. Similarly, when editing a document in a word pro- cessor, you normally save the file by selecting a Save or Save As… menu item. In this pro- gram, the button Copy >>> generates the external event that copies the selected text in the left JTextArea and displays it in the right JTextArea . 12 panelFrame.setVisible( true ); // display frame 13 } // end main 14 } // end class PanelDemo 1 // Fig. 14.47: TextAreaFrame.java 2 // Copying selected text from one textarea to another. 3 import java.awt.event.ActionListener; 4 import java.awt.event.ActionEvent; 5 import javax.swing.Box; 6 import javax.swing.JFrame; 7 8 import javax.swing.JButton; 9 10 11 public class TextAreaFrame extends JFrame 12 { 13 14 15 private JButton copyJButton; // initiates copying of text 16 Fig. 14.47 | Copying selected text from one JTextArea to another. (Part 1 of 2.) Fig. 14.46 | Test class for PanelFrame . (Part 2 of 2.) import javax.swing.JTextArea; import javax.swing.JScrollPane; private JTextArea textArea1; // displays demo string private JTextArea textArea2; // highlighted text is copied here
Image of page 12
616 Chapter 14 GUI Components: Part 1 17 // no-argument constructor 18 public TextAreaFrame() 19 { 20 super ( "TextArea Demo" ); 21 22 String demo = "This is a demo string to\n" + 23 "illustrate copying text\nfrom one textarea to \n" + 24 "another textarea using an\nexternal event\n" ; 25 26 27 28 29 copyJButton = new JButton( "Copy >>>" ); // create copy button 30 31 copyJButton.addActionListener( 32 33 new ActionListener()
Image of page 13
Image of page 14

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture