CSE8B/CSE 11: Reference
Sheet for Final Exam Spring 2001
READ THIS -- VERY IMPORTANT!
These are fragments of code to jar your memory for spelling and some details
about parameters. They are not complete and not sufficient models if you do
not know the material. Statements like "but I did it like on the reference
sheet" will not get you more points when appealing a grade.
A Sample Class Definition:
public class PetRecord
{
private String name;
private int age;//in years
private double weight;//in pounds
public
void writeOutput()
{
System.out.println("Name:
" + name);
System.out.println("Age:
" + age + " years");
System.out.println("Weight:
" + weight + " pounds");
}
public
PetRecord(String initialName, int initialAge,
double
initialWeight)
{
name = initialName;
if ((initialAge < 0) || (initialWeight
< 0))
{
System.out.println("Error:
Negative age or weight.");
System.exit(0);
}
else
{
age
= initialAge;
weight
= initialWeight;
}
}
public
void set(String newName, int newAge, double newWeight)
{
name = newName;
if ((newAge < 0) || (newWeight
< 0))
{
System.out.println("Error:
Negative age or weight.");
System.exit(0);
}
else
{
age
= newAge;
weight
= newWeight;
}
}
public
PetRecord(String initialName)
{
name = initialName;
age = 0;
weight = 0;
}
public
void set(String newName)
{
name = newName;
}
public
PetRecord(int initialAge)
{
name = "No name yet.";
weight = 0;
if (initialAge < 0)
{
System.out.println("Error:
Negative age.");
System.exit(0);
}
else
age
= initialAge;
}
public
void set(int newAge)
{
if (newAge < 0)
{
System.out.println("Error:
Negative age.");
System.exit(0);
}
else
age
= newAge;
file://name and weight are unchanged.
}
public
PetRecord(double initialWeight)
{
name = "No name yet";
age = 0;
if (initialWeight < 0)
{
System.out.println("Error:
Negative weight.");
System.exit(0);
}
else
weight
= initialWeight;
}
public
void set(double newWeight)
{
if (newWeight < 0)
{
System.out.println("Error:
Negative weight.");
System.exit(0);
}
else
weight
= newWeight; file://name and age are unchanged.
}
public
PetRecord()
{
name = "No name yet.";
age = 0;
weight = 0;
}
public
String getName()
{
return name;
}
public
int getAge()
{
return age;
}
public
double getWeight()
{
return weight;
}
}
File I/O:
PrintWriter outputStream =
new PrintWriter(new FileOutputStream("out.txt"));
PrintWriter outputStream =
new PrintWriter(new FileOutputStream("out.txt",
true));
BufferedReader inputStream =
new
BufferedReader(new FileReader("data.txt"));
DataOutputStream myOutputStream =
new DataOutputStream(new FileOutputStream("myfile.dat"));
DataOutputStream outputStream =
new DataOutputStream(
new
FileOutputStream("numbers.dat", true));
DataInputStream myInputStream =
new DataInputStream(new FileInputStream("myfile.dat"));
File fileObject = new File("treasure.dat");
writeUTF
readUTF
Listeners:
public void actionPerformed(ActionEvent
e)
public void windowOpened(WindowEvent
e)
public void windowClosing(WindowEvent e)
public void windowClosed(WindowEvent e)
public void windowIconified(WindowEvent e)
public void windowDeiconified(WindowEvent e)
public void windowActivated(WindowEvent e)
public void windowDeactivated(WindowEvent e)
Menus:
JMenuItem m;
m = new JMenuItem("Save Memo 1");
m.addActionListener(this);
memoMenu.add(m);
mBar.add(memoMenu);
setJMenuBar(mBar);
Icons:
JButton helloButton = new JButton("Hello");
ImageIcon dukeWavingIcon = new ImageIcon("duke_waving.gif");
helloButton.setIcon(dukeWavingIcon);
Scroll
Bars
JTextArea theText = new JTextArea(LINES, CHAR_PER_LINE);
JScrollPane scrolledText = new JScrollPane(theText);
textPanel.add(scrolledText);
contentPane.add(textPanel, BorderLayout.CENTER);
Borders:
public class BorderDemo extends JFrame
implements ActionListener
{
private JTextField name;
public
BorderDemo()
{
Container content = getContentPane();
content.setLayout(new GridLayout(2,
1));
JPanel
namePanel = new JPanel();
namePanel.setLayout(new BorderLayout());
name
= new JTextField(20);
name.setBorder(new EtchedBorder(Color.green,
Color.blue));
namePanel.add(name, BorderLayout.SOUTH);
JLabel nameLabel = new JLabel("Enter
your name here:");
nameLabel.setBorder(new EmptyBorder(20,
10, 0, 0));
namePanel.add(nameLabel, BorderLayout.CENTER);
namePanel.setBorder(new
LineBorder(Color.black, 10));
content.add(namePanel);
JPanel
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
JButton testButton = new JButton("Test");
testButton.addActionListener(this);
testButton.setBorder(
new
BevelBorder(BevelBorder.LOWERED));
buttonPanel.add(testButton);
JButton
clearButton = new JButton("Clear");
clearButton.addActionListener(this);
clearButton.setBorder(
new BevelBorder(BevelBorder.RAISED));
buttonPanel.add(clearButton);
buttonPanel.setBorder(
new
MatteBorder(60, 40, 30, 20, Color.pink));
content.add(buttonPanel);
}
Look and Feel:
try
{
UIManager.setLookAndFeel(Look_And_Feel_Class_Name);
SwingUtilities.updateComponentTreeUI(this);
}
catch (Exception e)
{//You might use a more explicit error message:
System.out.println("Could not load the desired
look and feel");
}
"javax.swing.plaf.metal.MetalLookAndFeel"
"com.sun.java.swing.plaf.motif.MotifLookAndFeel"
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
Box Layout
verticalPanel.setLayout(
new BoxLayout(verticalPanel,
BoxLayout.Y_AXIS));
Component horizontalStrut =
Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
Box horizontalBox = new Box(BoxLayout.X_AXIS);
Box horizontalBox = Box.createHorizontalBox();
The CardLayout Manager:
deckPanel = new JPanel();
dealer = new CardLayout();
deckPanel.setLayout(dealer);
deckPanel.add("start", startCardPanel);
deckPanel.add("red", redCardPanel);
dealer.show(deckPanel, "red");
Programming the Close
Window Button:
x.setDefaultCloseOperation(
WindowConstants.DO_NOTHING_ON_CLOSE);
x is a JFrame.