Upload
reynold-hutchinson
View
214
Download
0
Embed Size (px)
Citation preview
More GUIs, events, static
model.addElement(335);
model.addElement(436);
}
private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");
private JList<Integer> guiViewOfList; // The view
private DefaultListModel<Integer> model; // the Model
private void setLayout() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Do this almost always
this.setSize(120, 180); // 120 pixels wide and 180 pixels tall
this.setLocation(110, 50); // upper left corner
// DefaultListModel and JList work together as model/view
guiViewOfList = new JList<Integer>();
guiViewOfList.setModel(model);
// Adds two elements to the center (BorderLayout.CENTER is default), only see
add(guiViewOfList);
add(lineDisplay);
• What is wrong with this picture?
Layout and Sizing Issues
• Use BorderLayout for now• If you have more than one element• Set the preferred size of the components• Add them to a new JPanel• Add that JPanel to the center of the JFrame
add(aPanel, BorderLayout.CENTER);
How to fix things?
// Solution? Set the preferred size, // add both to a JPanel, then add one JPanel
JPanel panel = new JPanel();
lineDisplay.setPreferredSize(new Dimension(150, 100));
guiViewOfList.setPreferredSize(new Dimension(50, 140));
guiViewOfList.setBackground(Color.CYAN);
panel.add(guiViewOfList);
panel.add(lineDisplay);
add(panel);
Can set fonts, background, and editable // Get a fixed pitch font lineDisplay.setFont(new Font("Courier", Font.BOLD, 14));
lineDisplay.setBackground(Color.CYAN);
lineDisplay.setEditable(false);
guiViewOfList.setBackground(Color.GRAY);
panel.setBackground(Color.RED);
Model / View relationship with list
• DefaultListModel implements ListModel and therefore can be used with JList, a graphical view of a list (construct with or set model)
// DefaultListModel and JList work together as model/view
guiViewOfList = new JList<Integer>();
guiViewOfList.setModel(model);
The JList is selectable, set and get
private class JListListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent arg0) {
int index = guiViewOfList.getSelectedIndex();
if (index >= 0 && index < model.size()) {
// The JList is listening for changes to the model
model.remove(index);
// after a remove, the JList updates itself
}
} }
Add a mouse listener to out panel
class ListenToMouse implements MouseListener
Difference between static and non-static• A static method can be called without instantiating it’s class
• Math.abs(5-12);
• This won’t compile (the constructor is private)• Math m = new Math();
• Use static methods when you don’t need instance variables
TemperatureConverter has static methodspublic class TemperatureConverter {
// Return the Celcius equivalent of Fahrenheit
public static double F2C(double f) {
return roundToOneDecimal((5.0 / 9.0) * (f - 32.0));
}
• Tests do not need to instantiate the class @Test
public void testF2C() {
assertEquals(100.0, TemperatureConverter.F2C(212.0), 0.1);
assertEquals(0.0, TemperatureConverter.F2C(32.0), 0.1);
assertEquals(37.0, TemperatureConverter.F2C(98.6), 0.1);
}