This commit is contained in:
parent
f562f5b5dd
commit
dae551807e
BIN
bin/C14_GUI/ButtonListener.class
Normal file
BIN
bin/C14_GUI/ButtonListener.class
Normal file
Binary file not shown.
BIN
bin/C14_GUI/ComponentDemo.class
Normal file
BIN
bin/C14_GUI/ComponentDemo.class
Normal file
Binary file not shown.
BIN
bin/C14_GUI/DemoJFrame.class
Normal file
BIN
bin/C14_GUI/DemoJFrame.class
Normal file
Binary file not shown.
BIN
bin/C14_GUI/EventDemo.class
Normal file
BIN
bin/C14_GUI/EventDemo.class
Normal file
Binary file not shown.
7
src/C14_GUI/ComponentDemo.java
Normal file
7
src/C14_GUI/ComponentDemo.java
Normal file
@ -0,0 +1,7 @@
|
||||
package C14_GUI;
|
||||
|
||||
public class ComponentDemo {
|
||||
public static void main(String[] args) {
|
||||
DemoJFrame d = new DemoJFrame();
|
||||
}
|
||||
}
|
42
src/C14_GUI/DemoJFrame.java
Normal file
42
src/C14_GUI/DemoJFrame.java
Normal 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);
|
||||
}
|
||||
}
|
56
src/C14_GUI/EventDemo.java
Normal file
56
src/C14_GUI/EventDemo.java
Normal 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();
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user