FileDocCategorySizeDatePackage
SwingDemo.javaAPI DocExample2768Fri Dec 07 10:10:50 GMT 2001None

SwingDemo.java

//SwingDemo example from "Java 2 Complete" publisher Sybex[1999]

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingDemo extends JFrame 
{
	private JTextField textfield;
	private JTabbedPane tabbedPane;
	
	public static void main(String[] args)
	{
		SwingDemo that = new SwingDemo();
		that.setVisible(true);
	} //end main

	public  SwingDemo()
	{
		super("Swing Demo");
		setSize(540,420);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		// Textfield at South is for status messages. 
		// The JComponents use it to report events
		textfield=new JTextField();
		getContentPane().add(textfield,BorderLayout.SOUTH);

		// tabbed pane contains panels for the JComponents
		tabbedPane = new JTabbedPane();
		populateTabbedPane();
		getContentPane().add(tabbedPane,BorderLayout.CENTER);
		addMenu();
	} // end constructor
	private void addMenu()
	{
		JMenuBar mbar = new JMenuBar();
		JMenu menu = new JMenu("File");
		menu.add(new JCheckBoxMenuItem("Check me"));
		menu.addSeparator();
		JMenuItem item = new JMenuItem("Exit");
		item.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		menu.add(item);
		mbar.add(menu);
		setJMenuBar(mbar);
	}

	private void populateTabbedPane()
	{
		tabbedPane.addTab("Button", 
			new TabIcon(),
			new ButtonPanel(textfield),
			"Click here for button demo");
		tabbedPane.addTab("Label", 
			null,
			new LabelPanel(),
			"Click here for Label demo");
		tabbedPane.addTab("Toggles", 
			null,
			new TogglePanel(textfield),
			"Click here for Toggle demo");
		tabbedPane.addTab("Combo Box", 
			null,
			new ComboPanel(textfield),
			"Click here for Combo Box demo");
		tabbedPane.addTab("Sliders", 
			null,
			new SliderPanel(textfield),
			"Click here for Slider demo");
		tabbedPane.addTab("Password", 
			null,
			new PasswordPanel(),
			"Click here for Password demo");
		tabbedPane.addTab("Toolbar", 
			null,
			new ToolbarPanel(),
			"Click here for Toolbar demo");
		tabbedPane.addTab("Table", 
			null,
			new TablePanel(),
			"Click here for Table demo");
	}
	// this icon appears on the Button tab
	class TabIcon implements Icon
	{
		public int getIconWidth(){return 16;}
		public int getIconHeight(){return 16;}
		public void paintIcon(java.awt.Component c, java.awt.Graphics g,int x, int y)
		{
		   g.setColor(Color.black);
		   g.fillRect(x+4,y+4,getIconWidth()-8,getIconHeight()-8);
		   g.setColor(Color.cyan);
		   g.fillRect(x+6,y+6,getIconWidth()-12,getIconHeight()-12);
		}
	}

		
	
} // end SwingDemo