MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.awt.BorderLayout;
import java.io.InputStream;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.plaf.synth.SynthLookAndFeel;

public class MainClass {
    public static void main(String args[]) {
        SynthLookAndFeel synth = new SynthLookAndFeel();
        try {
            Class aClass = MainClass.class;
            InputStream is = aClass.getResourceAsStream("config.xml");
            if (is == null) {
                System.err.println("Unable to find theme configuration");
                System.exit(-1);
            }
            synth.load(is, aClass);
        } catch (ParseException e) {
            System.err.println("Unable to load theme configuration");
            System.exit(-2);
        }
        try {
            UIManager.setLookAndFeel(synth);
        } catch (javax.swing.UnsupportedLookAndFeelException e) {
            System.err.println("Unable to change look and feel");
            System.exit(-3);
        }
        JFrame frame = new JFrame("Synth Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button = new JButton("Hello, Synth");
        frame.add(button, BorderLayout.CENTER);
        JTextField textField = new JTextField();
        frame.add(textField, BorderLayout.SOUTH);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }

}

//File: config.xml
/*<synth>
  <style id="button">
<font name="Monospaced" size="24" style="BOLD"/>
<state value="MOUSE_OVER">
  <font name="SansSerif" size="48" style="ITALIC"/>
  <opaque value="TRUE"/>
  <color value="BLUE" type="BACKGROUND"/>
</state>
<state value="PRESSED">
  <font name="Serif" size="36" style="BOLD AND ITALIC"/>
  <opaque value="TRUE"/>
  <color value="RED" type="BACKGROUND"/>
</state>
  </style>
  <bind style="button" type="region" key="Button"/>
  <style id="textfield">
 <opaque value="true"/>
 <state>
    <color value="#C2E2CF" type="BACKGROUND"/>
    <color value="#000000" type="TEXT_FOREGROUND"/>
 </state>
 <imagePainter method="textFieldBorder" path="text.png"
    sourceInsets="3 3 3 3" paintCenter="false"/>
 <insets top="3" left="3" bottom="3" right="3"/>
  </style>
  <bind style="textfield" type="region" key="TextField"/>
</synth>
*/