Contents

  1. ./ButtonPanel.java
  2. ./ColorJPanel.java
  3. ./FlexibleLayout.java
  4. ./ListFonts.java
  5. ./MyFrame.java
  6. ./PanelWithButtons.java

./ButtonPanel.java 1/6

[
top][prev][next]
import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 
 */

/**
 * @author sprenkle
 *
 */
public class ButtonPanel extends JPanel {
	
	/**
	 * panel with three buttons on it
	 */
	public ButtonPanel() {
		JButton firstButton = new JButton("One");
		this.add(firstButton);
		JButton secondButton = new JButton("Two");
		this.add(secondButton);
		JButton thirdButton = new JButton("Three");
		this.add(thirdButton);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		JFrame frame = new JFrame("Button Panel Practice");
		
		frame.setContentPane(new ButtonPanel());
		
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}

./ColorJPanel.java 2/6

[
top][prev][next]
import java.awt.Graphics;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * Demonstrates use of colors and drawing on a panel.
 * 
 */
public class ColorJPanel extends JPanel {

	public void paintComponent(Graphics g) {
		// draw rectangles and Strings in different colors

		super.paintComponent(g); // call superclass's paintComponent

		this.setBackground(Color.WHITE);

		// set new drawing color using integers
		g.setColor(new Color(255, 0, 0)); // would be better to extract the
											// color as a local variable: easier
											// to understand code.
		g.fillRect(15, 25, 100, 20);
		g.drawString("Current RGB: " + g.getColor(), 130, 40);

		// set new drawing color using floats
		g.setColor(new Color(0.50f, 0.75f, 0.0f));
		g.fillRect(15, 50, 100, 20);
		g.drawString("Current RGB: " + g.getColor(), 130, 65);

		// set new drawing color using static Color objects
		g.setColor(Color.BLUE);
		g.fillRect(15, 75, 100, 20);
		g.drawString("Current RGB: " + g.getColor(), 130, 90);

		// display individual RGB values
		Color color = Color.MAGENTA;
		g.setColor(color);
		g.fillRect(15, 100, 100, 20);
		g.drawString("RGB values: " + color.getRed() + ", " +

		color.getGreen() + ", " + color.getBlue(), 130, 115);

	}

	public static void main(String args[]) {
		// create frame for ColorJPanel
		JFrame frame = new JFrame("Using colors");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ColorJPanel colorJPanel = new ColorJPanel(); // create ColorJPanel
		frame.add(colorJPanel); // add colorJPanel to frame
		frame.setSize(500, 180); // set frame size
		frame.setVisible(true); // display frame

	} 

} 

/*******************************************************************************
 * 
 * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and Pearson Education,
 * Inc. All Rights Reserved.
 * Modified by SES for CSCI209
 ******************************************************************************/


./FlexibleLayout.java 3/6

[
top][prev][next]
import javax.swing.*;
import java.awt.*;

/**
 * Demonstrates flexibility of layout
 */
public class FlexibleLayout {

	public static void main(String args[]) {
		JFrame frame = new JFrame();
		frame.setTitle("Demonstrate Flexibility");

		Container contentPane = frame.getContentPane();

		// create labels for each of the parts of the pane
		JLabel north = new JLabel("North");
		JLabel west = new JLabel("West");
		JLabel east = new JLabel("East");

		contentPane.add(north, BorderLayout.NORTH);
		contentPane.add(west, BorderLayout.WEST);
		contentPane.add(east, BorderLayout.EAST);

		// create buttons and add to button panel
		Button b1 = new Button("One!");
		Button b2 = new Button("a-Two!");
		Button b3 = new Button("a-Three!");

		JPanel buttonPanel = new JPanel();

		// add the buttons to the new JPanel
		// this will use the default flow layout manager
		buttonPanel.add(b1);
		buttonPanel.add(b2);
		buttonPanel.add(b3);

		// add the panel to the South part of the JFrame content pane
		contentPane.add(buttonPanel, BorderLayout.SOUTH);

		frame.pack();
		frame.setVisible(true);
	}
}

./ListFonts.java 4/6

[
top][prev][next]
import java.awt.*;

/**
 * List all the available fonts on the system
 * @author Sara Sprenkle
 *
 */
public class ListFonts {

	public static void main(String[] args) {
		String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getAvailableFontFamilyNames();
		for (int i = 0; i < fontNames.length; i++)
			System.out.println(fontNames[i]);
	}
}

./MyFrame.java 5/6

[
top][prev][next]
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

class MyFrame extends JFrame {

	public MyFrame() {
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenHeight = screenSize.height;
		int screenWidth = screenSize.width;

		setSize(screenWidth / 2, screenHeight / 2);
		setLocation(screenWidth / 4, screenHeight / 4);

		setTitle("My Frame Title");
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

	}

	public static void main(String args[]) {
		MyFrame frame = new MyFrame();

	}
}

./PanelWithButtons.java 6/6

[
top][prev][next]
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;


public class PanelWithButtons extends JFrame {

	public PanelWithButtons() {
		init();
		setVisible(true);
	}
	
	private void init() {
		
		this.setSize(200, 200);
		// otherwise, just closes the window, doesn't quit
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container pane = getContentPane();
		//pane.setLayout(new FlowLayout());
		
		JButton red = new JButton("Red");
		red.setForeground(Color.red);

		JButton yellow = new JButton("Yellow");
		yellow.setBackground(Color.yellow);
		
		JButton blue = new JButton("Blue");
		blue.setForeground(Color.blue);

		pane.add(red, BorderLayout.NORTH);
		pane.add(yellow, BorderLayout.SOUTH);
		pane.add(blue, BorderLayout.CENTER);

		// pack();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		PanelWithButtons ex = new PanelWithButtons();
	}

}

Generated by GNU enscript 1.6.4.