



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




public class Font { public final static int BOLD = 1; public final static int ITALIC = 2; // Creates a new Font from the specified name, style and point size. public Font(String name, int style, int size) }
import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; public class ShowFontAsEnum extends JFrame { public static void main(String[] args) { out .println(Font. BOLD ); out .println(Font. ITALIC ); ShowFontAsEnum oneJableInAFrame = new ShowFontAsEnum(); oneJableInAFrame.setVisible( true ); } public ShowFontAsEnum() { super ("Title: Font"); // Call JFrame's constructor this .setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE ); this .setSize(600, 120); Container cp = this .getContentPane(); JLabel label = new JLabel("This is a message of the emergency broadcast system"); label.setFont( new Font("Courier", 18, Font. BOLD )); cp.add(label, BorderLayout. CENTER ); } }
public enum FontStyle { BOLD , ITALIC ; } public class Font { // The current javax.swing.Font in Java allows integers to be passed as styles // The frame above shows what can happen. This would have been better: public Font(String name, FontStyle style, int size) }
label.setFont( new Font("Courier", 18, FontStyle. BOLD ));
public class ShowFontAsEnum extends JFrame {
public ShowFontAsEnum() { super ("Title: Font"); // Call JFrame's constructor
@Override public String toString() { String result = "" + this .getClass(); result += "\n============\n"; result += super .toString(); // Call super class's toString with super return result; } class ShowFontAsEnum ============ ShowFontAsEnum[frame0,0,0,600x90,invalid,hidden,layout=java.awt.BorderLayout, title=Title: Font , resizable,normal, defaultCloseOperation=EXIT_ON_CLOSE ,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,l ayout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSiz e=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
abstract public class Account { private double balance; public Account( double initialBalance) { balance = initialBalance; } public double getBalance() { return balance; } public void debit( double amount) { balance -= amount; } protected void deposit( double amount) { balance += amount; } // All subclasses must implement this method in their own way abstract boolean withdraw( double amount); }
public class RuleParser extends AbstractRuleParser { public RuleParser(String configFileName, String dtdFileName) { super (configFileName, dtdFileName); } // ... private List
RuleParser rp = new RuleParser("sample-rules-config.xml", "rules-config.dtd"); // You still need to call the parse method so it can throw exceptions later rp.parse();
public void parse() throws ParserConfigurationException, SAXException, IOException, ValidationException { Element root = this .getRootElement(); // ... parse the DOM tree to get the lists
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, ValidationException { RuleParser rp = new RuleParser("sample-rules-config.xml", "rules-config.dtd"); rp.parse(); // Show the three children of the root node config Element root = rp.getRootElement(); NodeList children = root.getChildNodes(); for ( int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child instanceof Element) { // Don't print whitespace System. out .print("child " + i + ") " + child.getNodeName()); System. out .println("='" + child.getTextContent() + "'"); } } } Output should be child 1) allowed-list='[email protected]@[email protected]' child 3) blocked-list='[email protected]@[email protected]' child 5) rules='.spam..Bad Text..Bad Text.[email protected]@[email protected].@.spam.' Without checking for instanceof Element (remove the if above) you see whitespace with new lines child 0) #text=' ' child 1) allowed-list='[email protected]@[email protected]' child 2) #text=' ' child 3) blocked-list='[email protected]@[email protected]' child 4) #text=' ' child 5) rules='.spam..Bad Text..Bad Text.[email protected]@[email protected].@.spam.' child 6) #text=' '