One of the things that drives me absolutely nuts about the Java materials I am using is that the majority of the programs are console programs.
Sorry that really sucks when my regular children get to do real live Windows programs.
So I have decided I am going to rewrite the labs so that they at least display the answer with Swing Frames and Panels. It’s either that or rewrite them in C#, which I am also close to doing. Unfortunately we have a district final.
We’ve been purchasing Stacy Armstrong’s materials so I am going to give some of the labs a try. He does some graphic programs where he has a runner that does the heavy work, so I think I’m going to try the same thing with SWING.
Frankly, this is more exciting can a console window.
However, I am scared of this:
/** * @(#)Lab02aFrame.java * * JFC Lab02a application * * @author * @version 1.00 2010/4/21 */import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.BoxLayout;
public class Lab02aFrame extends JFrame {
/**
* The constructor
*/
public Lab02aFrame() {
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
menuFile.setText("File");
menuFileExit.setText("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Lab02aFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("Lab02a");
setJMenuBar(menuBar);
setSize(new Dimension(400, 400));
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
// test cases
Rectangle test = new Rectangle();
JLabel label1 = new JLabel();
test.setLengthWidth(2,6);
test.calculatePerimeter( );
label1.setText(test.print());
panel.add(label1);JLabel label2 = new JLabel();
test.setLengthWidth(12,5);
test.calculatePerimeter( );
label2.setText(test.print());
panel.add(label2);
//add more test cases
this.add(panel);
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Lab02aFrame.this.windowClosed();
}
}
);
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
The students need to add their test cases between the comments, and I’ll have to teach them to make a new label each time they need a new test case.
Leave a Reply