oop lecture9
TRANSCRIPT
More on implementing Listeners
Lecture 9
Object Oriented ProgrammingEastern University, Dhaka
Md. Raihan Kibria
Add a combo box
We will add an item change listener and print to console.
See next page for code
public class ComboDemo extends JFrame {
ComboDemo(){ super.setBounds(0, 0, 500, 400); super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); super.setLayout(new FlowLayout());
final JTextField text = new JTextField("Select an item"); class MyItemListener implements ItemListener{
JComboBox combo; MyItemListener(JComboBox combo){ this.combo = combo; }
public void itemStateChanged(ItemEvent e) { text.setText(combo.getSelectedItem().toString()); } }
JComboBox combo = new JComboBox(new String[]{"Square", "Circle", "Triangle"}); combo.addItemListener(new MyItemListener(combo)); super.add(combo); super.add(text);
super.setVisible(true); }
public static void main(String[] args){ new ComboDemo(); }}
Output
Overriding paint method of Component
public class OverridePaintDemo {
public static void main(String[] args) { JFrame f = new JFrame(); f.setBounds(0, 0, 500, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new FlowLayout());
f.add(new MyFirstPanel());
f.setVisible(true); }}
class MyFirstPanel extends JPanel{ MyFirstPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); }}
Output
We want to add a circle to the panel above
public class OverridePaintDemo {
public static void main(String[] args) { JFrame f = new JFrame(); f.setBounds(0, 0, 500, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new FlowLayout());
f.add(new MyFirstPanel());
f.setVisible(true); }}
class MyFirstPanel extends JPanel{ MyFirstPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); }
public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawOval(5, 5, 90, 90); }}
Output
Now we want to add a circle, a rectangle and a triangle
public class OverridePaintDemo {
public static void main(String[] args) { JFrame f = new Jframe(); f.setBounds(0, 0, 500, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); f.setLayout(new FlowLayout());
f.add(new MyFirstPanel()); f.add(new MySecondPanel()); f.add(new MyThirdPanel());
f.setVisible(true); }}
More code next..
class MyFirstPanel extends JPanel{ MyFirstPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); }
public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawOval(5, 5, 90, 90); }}
class MySecondPanel extends JPanel{ MySecondPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); }
public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawRect(10, 15, 80, 60); }}
More code next..
class MyThirdPanel extends JPanel{ MyThirdPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); }
public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawLine(10, 90, 90, 90); g.drawLine(10, 90, 90, 10); g.drawLine(90, 10, 90, 90); }}
More on overriding
public class OverrideMethodDemo {
public static void main(String[] args) { System.out.println("CGPA by class I: " + new I().getCgpa()); System.out.println("CGPA by class J: " + new J().getCgpa()); }}
class I{ int[] gpas = new int[]{3, 4, 2, 4, 5};
int calculateSum(){ int sum = 0; for (int i=0; i<gpas.length; i++) sum += gpas[i]; return sum; }
public float getCgpa(){ int sum = calculateSum(); return sum/gpas.length; }}
class J extends I{
public float getCgpa() { int sum = calculateSum(); return (float)sum / (float)gpas.length; }}
Output
CGPA by class I: 3.0CGPA by class J: 3.6