Java Gui Components
Java Gui Components
Alark Joshi
import java.awt.*;
import javax.swing.*;
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
continued
primary.add (label1);
primary.add (label2);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}
primary.add (label1);
primary.add (label2);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
continued
Copyright © 2012 Pearson
Education, Inc.
continued
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}
• See LabelDemo.java
Copyright © 2012 Pearson Education, Inc.
//********************************************************************
// LabelDemo.java Author: Lewis/Loftus
//
// Demonstrates the use of image icons in labels.
//********************************************************************
import java.awt.*;
import javax.swing.*;
continued
Copyright © 2012 Pearson
Education, Inc.
continued
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
frame.getContentPane().add(new SplatPanel());
frame.pack();
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
frame.getContentPane().add(new SplatPanel());
frame.pack();
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
//-----------------------------------------------------------------
// Constructor: Creates five Circle objects.
//-----------------------------------------------------------------
public SplatPanel()
{
circle1 = new Circle (30, Color.red, 70, 35);
circle2 = new Circle (50, Color.green, 30, 20);
circle3 = new Circle (100, Color.cyan, 60, 85);
circle4 = new Circle (45, Color.yellow, 170, 30);
circle5 = new Circle (60, Color.blue, 200, 60);
continue
Copyright © 2012 Pearson
Education, Inc.
continue
//-----------------------------------------------------------------
// Draws this panel by requesting that each circle draw itself.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent(page);
circle1.draw(page);
circle2.draw(page);
circle3.draw(page);
circle4.draw(page);
circle5.draw(page);
}
}
import java.awt.*;
//-----------------------------------------------------------------
// Constructor: Sets up this circle with the specified values.
//-----------------------------------------------------------------
public Circle (int size, Color shade, int upperX, int upperY)
{
diameter = size;
color = shade;
x = upperX;
y = upperY;
}
continue
//-----------------------------------------------------------------
// Draws this circle in the specified graphics context.
//-----------------------------------------------------------------
public void draw (Graphics page)
{
page.setColor (color);
page.fillOval (x, y, diameter, diameter);
}
//-----------------------------------------------------------------
// Diameter mutator.
//-----------------------------------------------------------------
public void setDiameter (int size)
{
diameter = size;
}
//-----------------------------------------------------------------
// Color mutator.
//-----------------------------------------------------------------
public void setColor (Color shade)
{
color = shade;
}
continue
Copyright © 2012 Pearson
Education, Inc.
continue
//-----------------------------------------------------------------
// X mutator.
//-----------------------------------------------------------------
public void setX (int upperX)
{
x = upperX;
}
//-----------------------------------------------------------------
// Y mutator.
//-----------------------------------------------------------------
public void setY (int upperY)
{
y = upperY;
}
//-----------------------------------------------------------------
// Diameter accessor.
//-----------------------------------------------------------------
public int getDiameter ()
{
return diameter;
}
continue
//-----------------------------------------------------------------
// Color accessor.
//-----------------------------------------------------------------
public Color getColor ()
{
return color;
}
//-----------------------------------------------------------------
// X accessor.
//-----------------------------------------------------------------
public int getX ()
{
return x;
}
//-----------------------------------------------------------------
// Y accessor.
//-----------------------------------------------------------------
public int getY ()
{
return y;
}
}
Component Listener
• See PushCounter.java
• See PushCounterPanel.java
Copyright © 2012 Pearson Education, Inc.
//********************************************************************
// PushCounter.java Authors: Lewis/Loftus
//
// Demonstrates a graphical user interface and an event listener.
//********************************************************************
import javax.swing.JFrame;
frame.getContentPane().add(new PushCounterPanel());
frame.pack();
frame.setVisible(true);
}
}
import javax.swing.JFrame;
frame.getContentPane().add(new PushCounterPanel());
frame.pack();
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//-----------------------------------------------------------------
// Constructor: Sets up the GUI.
//-----------------------------------------------------------------
public PushCounterPanel ()
{
count = 0;
continue
Copyright © 2012 Pearson
Education, Inc.
continue
add (push);
add (label);
//*****************************************************************
// Represents a listener for button push (action) events.
//*****************************************************************
private class ButtonListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the counter and label when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
count++;
label.setText("Pushes: " + count);
}
}
}
• See Fahrenheit.java
• See FahrenheitPanel.java
Copyright © 2012 Pearson Education, Inc.
//********************************************************************
// Fahrenheit.java Author: Lewis/Loftus
//
// Demonstrates the use of text fields.
//********************************************************************
import javax.swing.JFrame;
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
import javax.swing.JFrame;
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//-----------------------------------------------------------------
// Constructor: Sets up the main GUI components.
//-----------------------------------------------------------------
public FahrenheitPanel()
{
inputLabel = new JLabel ("Enter Fahrenheit temperature:");
outputLabel = new JLabel ("Temperature in Celsius: ");
resultLabel = new JLabel ("---");
continue
Copyright © 2012 Pearson
Education, Inc.
continue
add (inputLabel);
add (fahrenheit);
add (outputLabel);
add (resultLabel);
//*****************************************************************
// Represents an action listener for the temperature input field.
//*****************************************************************
private class TempListener implements ActionListener
{
//--------------------------------------------------------------
// Performs the conversion when the enter key is pressed in
// the text field.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
int fahrenheitTemp, celsiusTemp;
continue
Copyright © 2012 Pearson
Education, Inc.
continue