diff --git a/bin/C14_GUI/ButtonListener.class b/bin/C14_GUI/ButtonListener.class new file mode 100644 index 0000000..4904a68 Binary files /dev/null and b/bin/C14_GUI/ButtonListener.class differ diff --git a/bin/C14_GUI/ComponentDemo.class b/bin/C14_GUI/ComponentDemo.class new file mode 100644 index 0000000..22f4917 Binary files /dev/null and b/bin/C14_GUI/ComponentDemo.class differ diff --git a/bin/C14_GUI/DemoJFrame.class b/bin/C14_GUI/DemoJFrame.class new file mode 100644 index 0000000..a0dc390 Binary files /dev/null and b/bin/C14_GUI/DemoJFrame.class differ diff --git a/bin/C14_GUI/EventDemo.class b/bin/C14_GUI/EventDemo.class new file mode 100644 index 0000000..7d00acc Binary files /dev/null and b/bin/C14_GUI/EventDemo.class differ diff --git a/src/C14_GUI/ComponentDemo.java b/src/C14_GUI/ComponentDemo.java new file mode 100644 index 0000000..c88eaf6 --- /dev/null +++ b/src/C14_GUI/ComponentDemo.java @@ -0,0 +1,7 @@ +package C14_GUI; + +public class ComponentDemo { + public static void main(String[] args) { + DemoJFrame d = new DemoJFrame(); + } +} diff --git a/src/C14_GUI/DemoJFrame.java b/src/C14_GUI/DemoJFrame.java new file mode 100644 index 0000000..b8c8b6f --- /dev/null +++ b/src/C14_GUI/DemoJFrame.java @@ -0,0 +1,42 @@ +package C14_GUI; + +import java.awt.Color; +import java.awt.FlowLayout; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; + +class DemoJFrame extends JFrame{ + DemoJFrame(){ + this.setTitle("Our first demo"); + + // Creating components + JLabel jl1 = new JLabel("A text"); + JButton jb1 = new JButton("Foo"); + JButton jb2 = new JButton("Bar"); + + // Choosing a layout manager for the JFrame + FlowLayout fl = new FlowLayout(); + this.setLayout(fl); + + // Creating the panels for putting the components on + JPanel jp1 = new JPanel(); + jp1.setBackground(Color.BLUE); + JPanel jp2 = new JPanel(); + jp2.setBackground(Color.GREEN); + + // Adding components to panels + jp1.add(jl1); + jp2.add(jb1); + jp2.add(jb2); + + // Adding panels to JFrame + this.add(jp1); + this.add(jp2); + + this.setSize(300,300); + this.setVisible(true); + } +} diff --git a/src/C14_GUI/EventDemo.java b/src/C14_GUI/EventDemo.java new file mode 100644 index 0000000..5882381 --- /dev/null +++ b/src/C14_GUI/EventDemo.java @@ -0,0 +1,56 @@ +package C14_GUI; + +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; + +class ButtonListener implements ActionListener{ + JLabel jl; + int nClicks = 0; + + ButtonListener(JLabel jl){ + this.jl = jl; + } + + @Override + public void actionPerformed(ActionEvent e) { + nClicks++; + jl.setText("Button clicked " + nClicks + " times"); + } +} + +public class EventDemo extends JFrame { + EventDemo() { + setSize(50, 100); + setLocation(1500, 600); + + JLabel jl1 = new JLabel("Nothing..."); + JButton jb1 = new JButton("Click me"); + + ButtonListener bl = new ButtonListener(jl1); + jb1.addActionListener(bl); + +// jb1.addActionListener(new ButtonListener() { +// @Override +// public void actionPerformed(ActionEvent e) { +// jl1.setText("Button clicked !"); +// } +// }); + + this.setLayout(new FlowLayout()); + + this.add(jb1); + this.add(jl1); + + this.setVisible(true); + } + + public static void main(String[] args) { + EventDemo e = new EventDemo(); + + } +}