This commit is contained in:
Rémi Heredero 2022-05-05 16:17:24 +02:00
parent f562f5b5dd
commit dae551807e
7 changed files with 105 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
bin/C14_GUI/EventDemo.class Normal file

Binary file not shown.

View File

@ -0,0 +1,7 @@
package C14_GUI;
public class ComponentDemo {
public static void main(String[] args) {
DemoJFrame d = new DemoJFrame();
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}