Download Lab05-PressYourLuck

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Science Unit 3 – GUIs and I/O
Creating your project and programs for Unit 3 – Lab05 Press Your Luck
We're saving our programs for Unit 3 in separate folders... Lab00, Lab01, Lab02 folders, etc.
We'll use 'packages' to create these new folders for our separate programs.
1. Lab05 Press Your Luck, see Packets: Unit3 GUIs and I/O page 22
3. Create Driver05.java, Panel05.java, Display05.java:
Right click on Unit3Progs: Source Packages
New – Java class
Class Name: Driver05
Project: Unit3Progs
Location: Source Packages
Package: Lab05
Right click on Unit3Progs: Source Packages
New – Java class
Class Name: Panel05
Project: Unit3Progs
Location: Source Packages
Package: Lab05
Right click on Unit3Progs: Source Packages
New – Java class
Class Name: Driver05
Project: Unit3Progs
Location: Source Packages
Package: Lab05
4. Type the code for Driver05, Panel05, Driver05
package Lab05;
import javax.swing.JFrame;
public class Driver05
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Press Your Luck");
frame.setSize(250, 200);
frame.setLocation(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new _____________);
frame.setVisible(true);
}
}
5.
package Lab05;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Panel05 extends JPanel
{
private Display05 display;
public Panel05()
{
setLayout(new FlowLayout());
display = new Display05();
add(display);
JButton button = new JButton("Press Your Luck");
button.addActionListener(new Listener());
add(button);
}
private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
/*****************************************************/
/*
*/
/* Your code goes here.
*/
/*
When the button is pressed, update the display object
*/
/*
call the 'update()' method in Display0: display.______ */
/****************************************************/
}
}
}
6.
package Lab05;
import javax.swing.*;
import java.awt.*;
public class Display05 extends JPanel
{
private JLabel label1, label2, label3;
private int value, total, max;
public Display05()
{
setLayout(new GridLayout(3, 1));
Font f = new Font("Serif", Font.BOLD, 30);
total = value = max = 0;
label1 = new JLabel("Value: " + value);
label1.setFont(f);
label1.setForeground(Color.blue);
add(label1);
label2 = new JLabel("Total: " + total);
label2.setFont(f);
label2.setForeground(Color.blue);
add(label2);
label3 = new Jlabel("High score: " + max);
label3.setFont(3);
label3.setForeground(Color.blue);
add(label3);
}
public void update()
{
/************************/
/*
*/
/* Your code goes here.
*/
/*
*/
Set value to a random number between 1 and 11:
value = (int)(Math.random() * ___ + ___);
label1.setText("Value: " + ______);
← Print the value on the label1
if(________ == 1 || _______ == ___) ← If value equals a 1 or a 2, set the total to 0
total ________;
else add value to the total
else
total ____________;
label2.setText("Total: " + ______);
← Print the total on label2
if(______ > _____)
← If total is greater than max
{
______ = ________;
← Set max to total
label3.setText("High Score: " ________); ← print the max on label3
/************************/
}
}
Related documents