comment on GUI exercice

This commit is contained in:
Fastium 2022-05-19 09:36:10 +02:00
parent 2120ee1c5d
commit 5fb795df73
6 changed files with 24 additions and 22 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -8,36 +8,43 @@ import java.awt.FlowLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
/**
* main class for create the window
*/
public class GUI1 extends JFrame { public class GUI1 extends JFrame {
public static void main(String[] args) { public static void main(String[] args) {
new GUI1(); new GUI1();
} }
GUI1(){ GUI1(){
//set up the window
setTitle("Me"); setTitle("Me");
setSize(200,200); setSize(200,200);
setLocation(600,200); setLocation(600,200);
this.getContentPane().setBackground(Color.GREEN);
//set the layout for the window
FlowLayout fl = new FlowLayout(); FlowLayout fl = new FlowLayout();
this.setLayout(fl); this.setLayout(fl);
this.getContentPane().setBackground(Color.GREEN); //create and add component on the window
JButton jb1 = new JButton("Foo"); JButton jb1 = new JButton("Foo");
JButton jb2 = new JButton("bar"); JButton jb2 = new JButton("bar");
ButtonListener bl = new ButtonListener(this); ButtonListener bl = new ButtonListener(this);
jb1.addActionListener(bl); jb1.addActionListener(bl);
jb2.addActionListener(bl); jb2.addActionListener(bl);
this.add(jb1); this.add(jb1);
this.add(jb2); this.add(jb2);
this.setVisible(true); this.setVisible(true);
} }
} }
/**
* set up the class for detect button
*/
class ButtonListener implements ActionListener{ class ButtonListener implements ActionListener{
JFrame f1; JFrame f1;

View File

@ -7,57 +7,53 @@ import javax.swing.event.ChangeListener;
import java.awt.Container; import java.awt.Container;
/** /**
* * main class for create the window
*/ */
public class GUI2 extends JFrame{ public class GUI2 extends JFrame{
int number; int number;
public static void main(String[] args) { public static void main(String[] args) {
new GUI2(); new GUI2();
} }
/**
*
*/
GUI2(){ GUI2(){
//set up the window with the layout
setSize(200,200); setSize(200,200);
setLocation(600,200); setLocation(600,200);
Container c = this.getContentPane(); Container c = this.getContentPane();
BoxLayout fl = new BoxLayout(c, BoxLayout.Y_AXIS); BoxLayout fl = new BoxLayout(c, BoxLayout.Y_AXIS);
c.setLayout(fl); c.setLayout(fl);
//create component on the window
JSlider js1 = new JSlider(0, 100); JSlider js1 = new JSlider(0, 100);
js1.setValue(0); js1.setValue(0);
JProgressBar jp1 = new JProgressBar(0, 100); JProgressBar jp1 = new JProgressBar(0, 100);
JLabel jl1 = new JLabel("waiting"); JLabel jl1 = new JLabel("waiting");
//create the listener for the slider
Listener l = new Listener(jp1, jl1, js1); Listener l = new Listener(jp1, jl1, js1);
js1.addChangeListener(l); js1.addChangeListener(l);
//addd component on the window
c.add(js1); c.add(js1);
c.add(jp1); c.add(jp1);
c.add(jl1); c.add(jl1);
this.setVisible(true); this.setVisible(true);
} }
} }
/** /**
* * class for detect slider mouvement
*/ */
class Listener implements ChangeListener { class Listener implements ChangeListener {
JProgressBar jp1; JProgressBar jp1;
JLabel jl1; JLabel jl1;
JSlider js1; JSlider js1;
int number = 0; int number = 0;
/** /**
* * initialize the instance with parameters
* @param jp1 * @param jp1 progress bar who display the number of the slider
* @param jl1 * @param jl1 label progress bar who display the number of the slider
* @param js1 * @param js1 slider who modify the label and the progress bar
*/ */
Listener(JProgressBar jp1, JLabel jl1, JSlider js1){ Listener(JProgressBar jp1, JLabel jl1, JSlider js1){
this.jp1 = jp1; this.jp1 = jp1;
@ -66,8 +62,7 @@ class Listener implements ChangeListener {
} }
/** /**
* * modify the value of the label and the progress bar with the slider
* @param e
*/ */
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {