BIS 3523, Advanced Languages I

Exam #2

Text-based Application

You will create one file for this exam. Be sure to name your file Exam2.java.

Write a text-based application that will implement the items in the following list. Each item is worth 5 points.

  1. Create 3 buttons, named as: countButton, removeButton and quitButton.
  1. Create a TextArea.
  1. Set you project title as Exam 2.
  1. Set the output window 800-pixel width and 600- pixel tall.
  1. Use layout manager to create an output screen, with a similar structure as follow:

If a drag the boarder, it could become:

  1. Add buttons to the panel and give each button a label.
  1. Create a loop with a string of number, andthe first number should be “0”.
  1. If I click the countButton, the result of this loop will be show in the text area as:

Hint:you may want to use following code:

display = new JTextArea();

JScrollPane scrollPane = new JScrollPane(display);

this.add(scrollPane);

  1. If I click the quitButton, the program would be closed.
  1. If I click removeButton, the output screen will become (you do not have to clear the text area):

Hint: you may want to use the following code:

panel.remove(removeButton);

panel.validate();

  1. Set the “setForeground” properties to green for all the three buttons.
  1. Use comments/indent to indicate which lines of code complete which function.

Upload your Exam2.java to your mislab account, and compile Exam2.java on your mislab account to be absolutely sure that it runs there.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

// it should be runs ~~you may import other packages, as long as your code runs. You are good.

publicclassExam2extends JFrame implements ActionListener {

private JPanel panel;

private JButton countButton, removeButton, quitButton; // 1.Create 3 buttons, named as:

// 1. CountButton, removeButton and quitButton.

private JTextArea display; // 2. Create a TextArea

publicstaticvoid main(String[] args) {

new Exam2();

}

public Exam2() {

this.setTitle("Exam 2");// 3. Set you project title as Exam 2

this.setSize(800, 600); // 4. Set the output window 800-pixel width and 600- pixel tall.

this.setLayout(new GridLayout(2,1));

//5.Use layout manager to create an output screen, with a similar structure as follow:

panel = new JPanel(new GridLayout(1,3));

//5.Use layout manager to create an output screen, with a similar structure as follow:

this.add(panel);

countButton = new JButton("Count"); // 6.Give each button a label.

countButton.addActionListener(this);// 8. If I click the countButton~~

countButton.setForeground(Color.GREEN);// 11.Set the “setForeground” properties to green for all the three buttons.

panel.add(countButton); // 6.Add buttons to the panel

removeButton = new JButton("Remove"); // 6.Give each button a label.

removeButton.addActionListener(this);// 10.If I click removeButton

removeButton.setForeground(Color.GREEN);// 11.Set the “setForeground” properties to green for all the three buttons.

panel.add(removeButton);// 6.Add buttons to the panel

quitButton = new JButton("Quit"); // 6.Give each button a label.

quitButton.addActionListener(this);// 9.If I click quitButton

quitButton.setForeground(Color.GREEN); // 11.Set the “setForeground” properties to green for all the three buttons.

panel.add(quitButton);// 6.Add buttons to the panel

display = new JTextArea(); // 8.~~ the result of this loop will be show in the text area as

JScrollPane scrollPane = new JScrollPane(display); // 8.~~ the result of this loop will be show in the text area as

this.add(scrollPane); // 8.~~ the result of this loop will be show in the text area as

this.setVisible(true);// 12. it should be displayed

}

publicvoid actionPerformed(ActionEvent e) {// 8. If I click the countButton,

if (e.getSource() == this.countButton) {

display.setText("");

// 8. the result of this loop will be show in the text area as:

int start = 0;

int stop = 20;

for (int i = start; i <= stop; i++) { // 7.`Create a loop with a string of number, as follow:~~

String x = Integer.toString(i);

display.append(x + "\n"); // 7. Create a loop with a string of number, as follow:~~

}

}

if (e.getSource() == this.removeButton) { // 10. If I click removeButton, the output screen will become:

panel.remove(removeButton);

panel.validate();

}

if (e.getSource() == this.quitButton) { // 9. If I click the quitButton, the program would be closed.

System.exit(0);

}

}

}