1.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ButtonTest extends JFrame implements ActionListener {
JButton button = new JButton("Button1");
JRadioButton radioButton = new JRadioButton("Button2");
JCheckBox checkBox = new JCheckBox("Button3");
JLabel label = new JLabel();
public ButtonTest() {
getContentPane().setLayout(new GridLayout(0,1));
getContentPane().add(button);
button.addActionListener(this);
getContentPane().add(radioButton);
radioButton.addActionListener(this);
getContentPane().add(checkBox);
checkBox.addActionListener(this);
getContentPane().add(label);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button) {
label.setText("JButton");
} else if (e.getSource() == radioButton) {
label.setText("JRadioButton");
} else if (e.getSource() == checkBox) {
label.setText("JCheckBox");
}
}
public static void main(String[] args) {
JFrame f = new ButtonTest();
f.setSize(200,100);
f.setVisible(true);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
2.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ClickTest extends JFrame {
public static int NOSHAPE = 0;
public static int CIRCLE = 1;
public static int SQUARE = 2;
private int x, y;
private int shape = NOSHAPE;
public ClickTest() {
setBackground(Color.white);
addMouseListener(new MyMouseListener());
}
public void paint(Graphics g) {
if(shape == NOSHAPE) {
// do nothing
} else if(shape == CIRCLE) {
g.setColor(Color.yellow);
g.fillOval(x,y,20,20);
} else if(shape == SQUARE) {
g.setColor(Color.green);
g.fillRect(x,y,20,20);
}
}
class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 1) {
shape = CIRCLE;
x = e.getX();
y = e.getY();
} else { // click more than one time
shape = SQUARE;
x = e.getX();
y = e.getY();
}
repaint();
}
}
public static void main(String[] args) {
JFrame f = new ClickTest();
f.setSize(200,200);
f.setVisible(true);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
3.
[Java, 10]
[Java, PIC, 10, 5.2]
[Java, PIC, 7, 5.2]
0
4. I write down the whole test program. You only have to provide the method body
during the exam.
import java.util.*;
public class VectorTest2 {
// Return a Vector consist of integers bigger than num in Vector v
// notice that v can contains other type of class
public static Vector bigger(Vector v, int num) {
Vector temp = new Vector();
for(int i = 0; i < v.size() ; i++) {
if(v.get(i) instanceof Integer) {
if(((Integer)v.get(i)).intValue() > num) {
temp.add(v.get(i));
}
}
}
return temp;
}
public static void main(String[] args) {
Vector v = new Vector();
v.add(new Integer(5));
v.add(new Integer(7));
v.add(new Integer(10));
v.add("Hi");
v.add(new Integer(8));
v.add("hello");
v.add(new Integer(12));
System.out.println(bigger(v,6));
}
}
5.
import java.awt.*;
import javax.swing.*;
public class CountdownClock extends JFrame implements Runnable {
private JLabel timeLabel = new JLabel();
private int initialTime;
public CountdownClock(int initialTime) {
this.initialTime = initialTime;
timeLabel.setText(Integer.toString(initialTime));
timeLabel.setHorizontalAlignment(JLabel.RIGHT);
timeLabel.setOpaque(true);
timeLabel.setBackground(Color.white);
timeLabel.setForeground(Color.green.darker());
timeLabel.setFont(new Font("Westminster", Font.BOLD, 50));
getContentPane().add(timeLabel);
}
public void run() {
for(int i = initialTime; i >= 0; i--) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
timeLabel.setText(Integer.toString(i));
}
}
public static void main(String[] args) {
CountdownClock clock = new CountdownClock(1000);
clock.pack();
clock.setVisible(true);
clock.setDefaultCloseOperation(EXIT_ON_CLOSE);
new Thread(clock).start();
}
}
6.
import java.awt.*;
import javax.swing.*;
public class Clock extends JFrame implements Runnable {
private int time = 0; // from 1 to 60
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0,0,200,200); // clear the frame
g.setColor(Color.blue);
g.fillArc(50,50,100,100,90, -6*time);
}
public void run() {
while(true) { // infinite loop
for(int i = 0; i < 60; i++) {
try {
Thread.sleep(1000); // stop one second
} catch (InterruptedException ex) {
// do nothing
}
time = i;
repaint();
}
}
}
public static void main(String[] args) {
Clock clock = new Clock();
clock.setSize(200,200);
clock.setVisible(true);
clock.setResizable(false);
clock.setDefaultCloseOperation(EXIT_ON_CLOSE);
new Thread(clock).start();
}
}