|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
where x and y specify the drawing origin; the drawing itself happens in Graphics g. You can use the Component to get properties such as the foreground or background colorin practice, its usually ignored.
Swing provides one Icon class for youImageIcon, used for displaying Images. Listing 38.8 is an example of an Icon. Listing 38.8 TBigBlackDot.javaYou Can Add This Icon to Swing Buttons and Labels public class TBigBlackDot implements javax.swing.Icon { public void paintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y) { g.setColor(java.awt.Color.black); g.fillOval(x, y, getIconWidth(), getIconHeight()); } public int getIconWidth() { return 100; } public int getIconHeight() { return 100; } } Adding an Instance of JLabelA label enables you to place static text onto the screen. We took our first look at Label when we discussed the AWT. The Swing JLabel improves upon java.awt.Label by enabling you to add an Icon and giving you better control over the position of the text. Listing 38.9 shows a JLabel in action on a JPanel. Listing 38.9 TLabelPanel.javaThis Panel Contains a Label with a TBigBlackDot Icon public class TLabelPanel extends javax.swing.JPanel { public TLabelPanel() { javax.swing.JLabel theLabel = new com.sun.java.swing.JLabel(Example of TBigBlackDot); // we dont have to settle for plain vanilla text java.awt.Font theBigBoldFont = new java.awt.Font(Serif, java.awt.Font.BOLD, 32); theLabel.setFont(theBigBoldFont); // now add an icon javax.swing.Icon aDot = new TBigBlackDot(); theLabel.setIcon(aDot); theLabel.setPreferredSize(new java.awt.Dimension(600, 150)); // place the text to the right of the icon theLabel.setHorizontalAlignment(javax.swing.JLabel.RIGHT); // and add the whole thing to the panel add(theLabel); } }
Using JButtonJButton behaves much like Button; you add it to a JPanel and listen for its Action with an ActionListener.
As with JLabel, you can add an Icon to JButton by calling the setIcon() method.
Adding an Instance of JCheckBoxRecall from the earlier section of this chapter, Using the Abstract Windowing Toolkit, that you can implement check boxes with java.awt.Checkbox. You implemented radio buttons by placing the Checkbox into a CheckboxGroup. In Swing, the concept of a radio button in handled explicitlySwing has its own JRadioButton class with an associated ButtonGroup. You use JCheckBox just to implement check boxes.
Using JRadioButtonTo make a group of radio buttons, make instances of JRadioButton and add them to a ButtonGroup. Listing 38.10 shows an example of some radio buttons. Listing 38.10 TDoseNotGivenPanel.javaThis Panel Contains a Group of Radio Buttons public class TDoseNotGivenPanel extends javax.swing.JPanel { public TDoseNotGivenPanel() { // make room for a label and four buttons setLayout(new java.awt.GridLayout(5, 1)); javax.swing.ButtonGroup aReason = new javax.swing.ButtonGroup(); javax.swing.JLabel theLabel = new javax.swing.JLabel(Dose not given because); theLabel.setFont(new java.awt.Font(SansSrif, java.awt.Font.BOLD, 14)); add(theLabel); javax.swing.JRadioButton thePatientNotAvailableButton = new javax.swing.JRadioButton(Patient not available); thePatientNotAvailableButton.setHorizontalAlignment( javax.swing.AbstractButton.LEFT); thePatientNotAvailableButton.setKeyAccelerator(A); thePatientNotAvailableButton.setSelected(true); //default add(thePatientNotAvailableButton); aReason.add(thePatientNotAvailableButton); javax.swing.JRadioButton thePatientOffWardButton = new javax.swing.JRadioButton(Patient off ward); thePatientOffWardButton.setHorizontalAlignment( javax.swing.AbstractButton.LEFT); thePatientNotAvailableButton.setKeyAccelerator(O); add (thePatientOffWardButton); aReason.add(thePatientOffWardButton); javax.swing.JRadioButton thePatientRefusedButton = new javax.swing.JRadioButton(Patient refused dose); thePatientRefusedButton.setHorizontalAlignment( javax.swing.AbstractButton.LEFT); thePatientNotAvailableButton.setKeyAccelerator(R); add (thePatientRefusedButton); aReason.add(thePatientRefusedButton); javax.swing.JRadioButton thePatientExpelledDoseButton = new javax.swing.JRadioButton(Patient expelled dose); thePatientExpelledDoseButton.setHorizontalAlignment( javax.swing.AbstractButton.LEFT); thePatientNotAvailableButton.setKeyAccelerator(X); add (thePatientExpelledDoseButton); aReason.add(thePatientExpelledDoseButton); } }
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |