-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6d22654
Showing
12 changed files
with
646 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package lec17.ex1; | ||
|
||
import javax.swing.JFrame; | ||
import javax.swing.JLabel; | ||
import javax.swing.JPanel; | ||
import javax.swing.SwingConstants; | ||
|
||
import java.awt.BorderLayout; | ||
import java.awt.Color; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
JFrame main_frame = new JFrame(); | ||
main_frame.setTitle("Hello, World"); | ||
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
|
||
JPanel main_panel = new JPanel(); | ||
main_frame.setContentPane(main_panel); | ||
|
||
main_panel.setLayout(new BorderLayout()); | ||
|
||
JLabel hello_world_label = new JLabel("Hello, World!"); | ||
hello_world_label.setHorizontalAlignment(SwingConstants.CENTER); | ||
hello_world_label.setForeground(Color.BLUE); | ||
hello_world_label.setOpaque(true); | ||
hello_world_label.setBackground(Color.YELLOW); | ||
|
||
main_panel.add(hello_world_label, BorderLayout.CENTER); | ||
|
||
main_frame.pack(); | ||
main_frame.setVisible(true); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package lec17.ex2; | ||
|
||
import javax.swing.*; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
int choice = JOptionPane.showConfirmDialog(null, "Is it safe?"); | ||
System.out.println("You chose: " + choice); | ||
|
||
String input = JOptionPane.showInputDialog("What say you?"); | ||
|
||
JOptionPane.showMessageDialog(null, "You said: " + input); | ||
System.out.println("Done"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package lec17.ex3; | ||
|
||
import java.awt.event.ActionEvent; | ||
import java.awt.event.ActionListener; | ||
import javax.swing.*; | ||
|
||
public class ButtonResponder implements ActionListener { | ||
|
||
private JLabel target; | ||
|
||
public ButtonResponder(JLabel target_label) { | ||
target = target_label; | ||
} | ||
|
||
public void actionPerformed(ActionEvent e) { | ||
String command = e.getActionCommand(); | ||
if (command.equals("hello")) { | ||
target.setText("Hello!"); | ||
} else if (command.equals("bye")) { | ||
target.setText("Bye!"); | ||
} else if (command.equals("yes")) { | ||
target.setText("Yes!"); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package lec17.ex3; | ||
|
||
import java.awt.BorderLayout; | ||
import java.awt.GridLayout; | ||
import java.awt.Color; | ||
|
||
import javax.swing.JFrame; | ||
import javax.swing.JButton; | ||
import javax.swing.JLabel; | ||
import javax.swing.JPanel; | ||
import javax.swing.SwingConstants; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
JFrame main_frame = new JFrame(); | ||
main_frame.setTitle("Button Example"); | ||
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
main_frame.setSize(300, 100); | ||
|
||
JPanel main_panel = new JPanel(); | ||
main_frame.setContentPane(main_panel); | ||
|
||
main_panel.setLayout(new BorderLayout()); | ||
|
||
JLabel my_label = new JLabel(""); | ||
my_label.setHorizontalAlignment(SwingConstants.CENTER); | ||
my_label.setForeground(Color.BLUE); | ||
my_label.setOpaque(true); | ||
my_label.setBackground(Color.YELLOW); | ||
|
||
main_panel.add(my_label, BorderLayout.CENTER); | ||
|
||
JPanel button_panel = new JPanel(); | ||
button_panel.setLayout(new GridLayout(1,3)); | ||
|
||
ButtonResponder responder = new ButtonResponder(my_label); | ||
|
||
JButton b1 = new JButton("Say Hello"); | ||
b1.setActionCommand("hello"); | ||
b1.addActionListener(responder); | ||
button_panel.add(b1); | ||
|
||
JButton b2 = new JButton("Say Bye"); | ||
b2.setActionCommand("bye"); | ||
b2.addActionListener(responder); | ||
button_panel.add(b2); | ||
|
||
JButton b3 = new JButton("Say Yes"); | ||
b3.setActionCommand("yes"); | ||
b3.addActionListener(responder); | ||
button_panel.add(b3); | ||
|
||
main_panel.add(button_panel, BorderLayout.SOUTH); | ||
main_frame.pack(); | ||
main_frame.setVisible(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package lec17.ex4.v1; | ||
|
||
import java.awt.BorderLayout; | ||
import java.awt.Color; | ||
import java.awt.Dimension; | ||
import java.awt.GridLayout; | ||
|
||
import javax.swing.JLabel; | ||
import javax.swing.JPanel; | ||
import javax.swing.JSlider; | ||
import javax.swing.SwingConstants; | ||
|
||
public class ColorChooser extends JPanel { | ||
|
||
private Color color; | ||
private JSlider red_slider; | ||
private JSlider green_slider; | ||
private JSlider blue_slider; | ||
private JLabel color_label; | ||
|
||
public ColorChooser(Color init_color) { | ||
color = init_color; | ||
|
||
setLayout(new BorderLayout()); | ||
|
||
JPanel slider_panel = new JPanel(); | ||
slider_panel.setLayout(new GridLayout(3,1)); | ||
|
||
red_slider = new JSlider(0, 255, init_color.getRed()); | ||
green_slider = new JSlider(0, 255, init_color.getGreen()); | ||
blue_slider = new JSlider(0, 255, init_color.getBlue()); | ||
|
||
Dimension slider_dim = new Dimension(200,20); | ||
red_slider.setPreferredSize(slider_dim); | ||
green_slider.setPreferredSize(slider_dim); | ||
blue_slider.setPreferredSize(slider_dim); | ||
|
||
slider_panel.add(red_slider); | ||
slider_panel.add(green_slider); | ||
slider_panel.add(blue_slider); | ||
|
||
add(slider_panel, BorderLayout.SOUTH); | ||
|
||
color_label = new JLabel(init_color.toString()); | ||
color_label.setBackground(this.color); | ||
color_label.setOpaque(true); | ||
color_label.setPreferredSize(new Dimension(200, 40)); | ||
color_label.setHorizontalAlignment(SwingConstants.CENTER); | ||
|
||
add(color_label, BorderLayout.CENTER); | ||
} | ||
|
||
public ColorChooser() { | ||
this(Color.GREEN); | ||
} | ||
|
||
public Color getColor() { | ||
return color; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package lec17.ex4.v1; | ||
|
||
import javax.swing.JFrame; | ||
import javax.swing.JLabel; | ||
import javax.swing.JPanel; | ||
import javax.swing.SwingConstants; | ||
|
||
import java.awt.BorderLayout; | ||
import java.awt.Color; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
JFrame main_frame = new JFrame(); | ||
main_frame.setTitle("Color Picker"); | ||
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
|
||
JPanel main_panel = new JPanel(); | ||
main_frame.setContentPane(main_panel); | ||
|
||
main_panel.setLayout(new BorderLayout()); | ||
|
||
ColorChooser color_chooser = new ColorChooser(); | ||
main_panel.add(color_chooser, BorderLayout.CENTER); | ||
|
||
main_frame.pack(); | ||
main_frame.setVisible(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package lec17.ex4.v2; | ||
|
||
import java.awt.BorderLayout; | ||
import java.awt.Color; | ||
import java.awt.Dimension; | ||
import java.awt.GridLayout; | ||
|
||
import javax.swing.JLabel; | ||
import javax.swing.JPanel; | ||
import javax.swing.JSlider; | ||
import javax.swing.SwingConstants; | ||
import javax.swing.event.ChangeEvent; | ||
import javax.swing.event.ChangeListener; | ||
|
||
public class ColorChooser extends JPanel implements ChangeListener { | ||
|
||
Color color; | ||
JSlider red_slider; | ||
JSlider green_slider; | ||
JSlider blue_slider; | ||
JLabel color_label; | ||
|
||
public ColorChooser(Color init_color) { | ||
color = init_color; | ||
|
||
setLayout(new BorderLayout()); | ||
|
||
JPanel slider_panel = new JPanel(); | ||
slider_panel.setLayout(new GridLayout(3,1)); | ||
|
||
red_slider = new JSlider(0, 255, init_color.getRed()); | ||
green_slider = new JSlider(0, 255, init_color.getGreen()); | ||
blue_slider = new JSlider(0, 255, init_color.getBlue()); | ||
|
||
red_slider.setPreferredSize(new Dimension(200, 20)); | ||
green_slider.setPreferredSize(new Dimension(200, 20)); | ||
blue_slider.setPreferredSize(new Dimension(200, 20)); | ||
|
||
slider_panel.add(red_slider); | ||
slider_panel.add(green_slider); | ||
slider_panel.add(blue_slider); | ||
|
||
add(slider_panel, BorderLayout.SOUTH); | ||
|
||
color_label = new JLabel(init_color.toString()); | ||
color_label.setBackground(this.color); | ||
color_label.setOpaque(true); | ||
color_label.setPreferredSize(new Dimension(200, 40)); | ||
color_label.setHorizontalAlignment(SwingConstants.CENTER); | ||
|
||
add(color_label, BorderLayout.CENTER); | ||
|
||
red_slider.addChangeListener(this); | ||
green_slider.addChangeListener(this); | ||
blue_slider.addChangeListener(this); | ||
} | ||
|
||
public ColorChooser() { | ||
this(Color.GREEN); | ||
} | ||
|
||
public Color getColor() { | ||
return color; | ||
} | ||
|
||
@Override | ||
public void stateChanged(ChangeEvent e) { | ||
color = new Color(red_slider.getValue(), | ||
green_slider.getValue(), | ||
blue_slider.getValue()); | ||
color_label.setText(color.toString()); | ||
color_label.setBackground(color); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package lec17.ex4.v2; | ||
|
||
import javax.swing.JFrame; | ||
import javax.swing.JLabel; | ||
import javax.swing.JPanel; | ||
import javax.swing.SwingConstants; | ||
|
||
import java.awt.BorderLayout; | ||
import java.awt.Color; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
JFrame main_frame = new JFrame(); | ||
main_frame.setTitle("Color Picker"); | ||
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
|
||
JPanel main_panel = new JPanel(); | ||
main_frame.setContentPane(main_panel); | ||
|
||
main_panel.setLayout(new BorderLayout()); | ||
|
||
ColorChooser color_chooser = new ColorChooser(); | ||
main_panel.add(color_chooser, BorderLayout.CENTER); | ||
|
||
main_frame.pack(); | ||
main_frame.setVisible(true); | ||
} | ||
} |
Oops, something went wrong.