Valentino Vranić - Objektovo-orientovane programovanie 2014/15 Prednáška 4: Model-View-Controller a používateľské rozhranie 19. marec 2015 Kód zo slajdov Príklad: jednoduché okno import javax.swing.*; class C { public static void main(String[] args) { JFrame w = new JFrame("Moje okno"); w.setSize(300, 300); w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); w.setVisible(true); } } Príklad: okno dedením od JFrame import javax.swing.*; class MyWindow extends JFrame { public MyWindow() { setTitle("Moje okno"); setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class C { public static void main(String[] args) { JFrame w = new MyWindow(); w.setVisible(true); } } Príklad: okno s tlačidlom import javax.swing.*; class C { public static void main(String[] args) { JFrame w = new JFrame("Moje okno"); w.setSize(300, 300); JButton b1 = new JButton("Tlacidlo1"); w.add(b1); w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); w.setVisible(true); } } Príklad: rozloženie prvkov v okne (1) import javax.swing.*; import java.awt.*; class C { public static void main(String[] args) { JFrame w = new JFrame("Moje okno"); w.setSize(300, 300); JButton b1 = new JButton("Tlacidlo1"); JButton b2 = new JButton("Tlacidlo2"); // prednastavené rozloženie: // w.setLayout(new BorderLayout()); w.add(b1, BorderLayout.WEST); w.add(b2, BorderLayout.EAST); w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); w.setVisible(true); } } Príklad: kliknutie na tlačidlo import javax.swing.*; import java.awt.*; import java.awt.event.*; // niekedy aj javax.swing.event.* class MyWindow extends JFrame { private JButton t = new JButton("Tlacidlo1"); public MyWindow() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); add(t); t.addActionListener(new MyListener()); } // trieda vhniezdená v MyWindow private class MyListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (t.getText() != "Jeden") t.setText("Jeden"); else t.setText("Dva"); } } } // class MyWindow class C { public static void main(String[] args) { JFrame w = new MyWindow(); w.setVisible(true); } } Príklad: prijímač pomocou anonymnej triedy class MyWindow extends JFrame { private JButton t = new JButton("Jeden"); public MyWindow() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); add(t); t.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (t.getText() != "Jeden") t.setText("Jeden"); else t.setText("Dva"); } }); } } Príklad: sledovanie pohybu myši t.addMouseListener(new MouseListener() { public void mouseEntered(MouseEvent e) { if (t.getText() != "Jeden") t.setText("Jeden"); else t.setText("Dva"); } public void mouseReleased(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } }); Príklad: použitie adaptéra t.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent e) { if (t.getText() != "Jeden") t.setText("Jeden"); else t.setText("Dva"); } }); Príklad: korektná zmena GUI po realizácii class MyWindow extends JFrame { private JLabel l; public MyWindow() { setTitle("Moje okno"); setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); l = new JLabel("Text"); add(l); } public void changeText(String s) { l.setText(s); } } class C { public static void main(String[] args) throws Exception { final MyWindow w = new MyWindow(); w.setVisible(true); SwingUtilities.invokeLater(new Runnable() { public void run() { w.changeText("Novy text"); } }); } } Príklad: spustenie náročného výpočtu z GUI public class NastyComputation { private MyWindow w; public void setWindow(MyWindow w) { this.w = w; } public void compute() { w.setBusy(); for (int i = 0; i < 100000; i++) System.out.println("c"); w.setFree(); } } import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyWindow extends JFrame { private NastyComputation c; private JButton PressButton = new JButton("Press"); private JButton CompButton = new JButton("Comp"); private JLabel l = new JLabel("Free"); public JPanel p = new JPanel(); public MyWindow(NastyComputation c) { super("MyWindow"); this.c = c; setSize(200, 75); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); add(PressButton); add(CompButton); add(l); PressButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (PressButton.getText() != "Press") { PressButton.setText("Press"); } else { PressButton.setText("Again"); } } }); CompButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { MyWindow.this.c.compute(); } }); } // MyWindow(NastyComputation c) public void setBusy() { l.setText("Busy"); } public void setFree() { l.setText("Free"); } public static void main(String[] args) { NastyComputation c = new NastyComputation(); MyWindow w = new MyWindow(c); c.setWindow(w); w.setVisible(true); } } // MyWindow public class NastyComputation { private MyWindow w; public void setWindow(MyWindow w) { this.w = w; } public void compute() { w.setBusy(); new Thread() { public void run() { for (int i = 0; i < 100000; i++) System.out.println("c"); SwingUtilities.invokeLater(new Runnable() { public void run() { w.setFree(); } }); } }.start(); } } public class NastyComputation { public void compute() { for (int i = 0; i < 10000; i++) System.out.println("c"); } } CompButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setBusy(); new SwingWorker() { public Object doInBackground() { c.compute(); return null; } protected void done() { setFree(); } }.execute(); } });