From 053113577fa8ea70232b5c1c068469e5eb779354 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Sun, 1 May 2022 23:24:00 +0800 Subject: [PATCH] lab10 --- lab/lab10/Question1/MyFrame.java | 17 ++++++++ lab/lab10/Question1/Start.java | 47 +++++++++++++++++++++ lab/lab10/Question2/MyFrame.java | 31 ++++++++++++++ lab/lab10/Question2/Start.java | 20 +++++++++ lab/lab10/Question3/MyFrame.java | 47 +++++++++++++++++++++ lab/lab10/Question3/Start.java | 18 ++++++++ lab/lab10/Question4/MyFrame.java | 47 +++++++++++++++++++++ lab/lab10/Question4/Start.java | 34 +++++++++++++++ lab/lab10/Question5/MyFrame.java | 72 ++++++++++++++++++++++++++++++++ lab/lab10/Question5/Start.java | 34 +++++++++++++++ lab/lab10/Question6/MyFrame.java | 27 ++++++++++++ lab/lab10/Question6/MyPanel.java | 15 +++++++ lab/lab10/Question6/Start.java | 15 +++++++ lab/lab10/Question7/MyFrame.java | 27 ++++++++++++ lab/lab10/Question7/MyPanel.java | 27 ++++++++++++ lab/lab10/Question7/Start.java | 15 +++++++ 16 files changed, 493 insertions(+) create mode 100644 lab/lab10/Question1/MyFrame.java create mode 100644 lab/lab10/Question1/Start.java create mode 100644 lab/lab10/Question2/MyFrame.java create mode 100644 lab/lab10/Question2/Start.java create mode 100644 lab/lab10/Question3/MyFrame.java create mode 100644 lab/lab10/Question3/Start.java create mode 100644 lab/lab10/Question4/MyFrame.java create mode 100644 lab/lab10/Question4/Start.java create mode 100644 lab/lab10/Question5/MyFrame.java create mode 100644 lab/lab10/Question5/Start.java create mode 100644 lab/lab10/Question6/MyFrame.java create mode 100644 lab/lab10/Question6/MyPanel.java create mode 100644 lab/lab10/Question6/Start.java create mode 100644 lab/lab10/Question7/MyFrame.java create mode 100644 lab/lab10/Question7/MyPanel.java create mode 100644 lab/lab10/Question7/Start.java diff --git a/lab/lab10/Question1/MyFrame.java b/lab/lab10/Question1/MyFrame.java new file mode 100644 index 0000000..2548e7d --- /dev/null +++ b/lab/lab10/Question1/MyFrame.java @@ -0,0 +1,17 @@ +/* + * Author: CHEN Yongyuan (Walter) from OOP(1007) + * Date: 2022-05-01 + * Description: This is the Main Frame class. + */ + +import javax.swing.JFrame; + +public class MyFrame extends JFrame { + public MyFrame() { + setSize(400, 300); + setTitle("MyFrame"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + setVisible(true); + } +} diff --git a/lab/lab10/Question1/Start.java b/lab/lab10/Question1/Start.java new file mode 100644 index 0000000..9abcb63 --- /dev/null +++ b/lab/lab10/Question1/Start.java @@ -0,0 +1,47 @@ +/** + * Answer to question. + * + * - Q: In the constructor of the MyFrame class, comment out the call to the + * setTitle method. Run the program. What happens? Then undo the commenting out. + * + * - A: The title will become empty. + * + * - Q: Comment out the call to the setSize method. Run the program. What + * happens? Then undo the commenting out + * + * - A: Yes the program is still running. Because the "close windows" event is + * not linked to the program ended event. So the program won't known the windows + * is closed and will continue running. + * + * - Q: Comment out the call to the setVisible method. Run the program. What + * happens? + * + * - A: Frame will not show and program will end. + * + * - Q: In a line before the call to the setVisible method, add a call to the + * setLocationRelativeTo method of the frame and give it an argument of null. + * Run the program. What happens? + * + * - A: The frame will show in the center of the screen. But did not work as + * expected while using mutiple screen and x11-wayland or wayland platform. + * + * - Q: In the run method of the anonymous class inside the Start class, add a + * second MyFrame object. Run the program. What happens? (Use the mouse to move + * the frame to check what happens!) What happens when you close one of the + * two frames? Then undo the change in the run method. + * + * - A: Two frame will show in the screen. If one of the frame is closed, the + * both frame will be closed. Because they are connnect to the same event. + * + */ +public class Start { + public static void main(String[] args) { + // Dispather + javax.swing.SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new MyFrame(); + } + }); + } +} diff --git a/lab/lab10/Question2/MyFrame.java b/lab/lab10/Question2/MyFrame.java new file mode 100644 index 0000000..7193fc8 --- /dev/null +++ b/lab/lab10/Question2/MyFrame.java @@ -0,0 +1,31 @@ +/* + * Author: CHEN Yongyuan (Walter) from OOP(1007) + * Date: 2022-05-01 + * Description: This is the Main Frame class. + */ + +import javax.swing.JFrame; +import java.awt.FlowLayout; +import javax.swing.JButton; + +public class MyFrame extends JFrame { + // add two buttons with different text + private JButton button1 = new JButton("Button 1"); + private JButton button2 = new JButton("Button 2"); + + public MyFrame() { + setSize(400, 300); + setTitle("MyFrame"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + + // set the layout of the frame + setLayout(new FlowLayout(FlowLayout.LEFT, 20, 40)); + + // add the buttons to the frame + add(button1); + add(button2); + + setVisible(true); + } +} diff --git a/lab/lab10/Question2/Start.java b/lab/lab10/Question2/Start.java new file mode 100644 index 0000000..dcaf27f --- /dev/null +++ b/lab/lab10/Question2/Start.java @@ -0,0 +1,20 @@ +/** + * Answer to question. + * + * -Q: Instead of adding to the frame two different buttons, add twice the same + * button object. Run the program. What happens? + * + * -A: Only one button is added to the frame. + * + */ +public class Start { + public static void main(String[] args) { + // Dispather + javax.swing.SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new MyFrame(); + } + }); + } +} diff --git a/lab/lab10/Question3/MyFrame.java b/lab/lab10/Question3/MyFrame.java new file mode 100644 index 0000000..5a02240 --- /dev/null +++ b/lab/lab10/Question3/MyFrame.java @@ -0,0 +1,47 @@ +/* + * Author: CHEN Yongyuan (Walter) from OOP(1007) + * Date: 2022-05-01 + * Description: This is the Main Frame class. + */ + +import java.awt.FlowLayout; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JRadioButton; +import javax.swing.JTextField; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; + +public class MyFrame extends JFrame { + + public MyFrame() { + setSize(400, 300); + setTitle("MyFrame"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + + // set the layout of the frame + setLayout(new FlowLayout(FlowLayout.LEFT, 20, 40)); + + // add the buttons to the frame + add(new JButton("Button 1")); + add(new JButton("Button 2")); + + JLabel l = new JLabel("Enter your name: "); + JTextField tx = new JTextField("Type Text Here"); + JCheckBox cb = new JCheckBox("I agree"); + JRadioButton rb = new JRadioButton("Yes"); + JComboBox cb1 = new JComboBox(new String[] { "Red", "Green", "Blue" }); + JComboBox cb2 = new JComboBox(new Integer[] { 2, 7, -3, 24 }); + // add the components to the frame + add(l); + add(tx); + add(cb); + add(rb); + add(cb1); + add(cb2); + + setVisible(true); + } +} diff --git a/lab/lab10/Question3/Start.java b/lab/lab10/Question3/Start.java new file mode 100644 index 0000000..b96f242 --- /dev/null +++ b/lab/lab10/Question3/Start.java @@ -0,0 +1,18 @@ +/** + * Answer to question. + * + * - Q: Also use the mouse to resize the frame and check what happens with the components. + * + * - A: The frame is resized and the components are repositioned. + */ +public class Start { + public static void main(String[] args) { + // Dispather + javax.swing.SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new MyFrame(); + } + }); + } +} diff --git a/lab/lab10/Question4/MyFrame.java b/lab/lab10/Question4/MyFrame.java new file mode 100644 index 0000000..612dd95 --- /dev/null +++ b/lab/lab10/Question4/MyFrame.java @@ -0,0 +1,47 @@ +/* + * Author: CHEN Yongyuan (Walter) from OOP(1007) + * Date: 2022-05-01 + * Description: This is the Main Frame class. + */ + +import java.awt.BorderLayout; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JRadioButton; +import javax.swing.JTextField; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; + +public class MyFrame extends JFrame { + + public MyFrame() { + setSize(400, 300); + setTitle("MyFrame"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + + // set the layout of the frame + setLayout(new BorderLayout()); + + // add the buttons to the frame + add(new JButton("Button 1")); + add(new JButton("Button 2")); + + JLabel l = new JLabel("Enter your name: "); + JTextField tx = new JTextField("Type Text Here"); + JCheckBox cb = new JCheckBox("I agree"); + JRadioButton rb = new JRadioButton("Yes"); + JComboBox cb1 = new JComboBox(new String[] { "Red", "Green", "Blue" }); + JComboBox cb2 = new JComboBox(new Integer[] { 2, 7, -3, 24 }); + // add the components to the frame + add(l, BorderLayout.PAGE_START); + add(tx, BorderLayout.LINE_START); + add(cb, BorderLayout.CENTER); + add(rb, BorderLayout.LINE_END); + add(cb1, BorderLayout.PAGE_END); + add(cb2); + + setVisible(true); + } +} diff --git a/lab/lab10/Question4/Start.java b/lab/lab10/Question4/Start.java new file mode 100644 index 0000000..1408920 --- /dev/null +++ b/lab/lab10/Question4/Start.java @@ -0,0 +1,34 @@ +/** + * Answer to question. + * + * - Q: What happens when you run the program? + * + * - A: The layout change to 2 columns. Direction is LTR. The program change the + * layout to my desired, the layout use the number of columns that I specified. + * + * - Q: Use the mouse to resize the frame. Does the layout change? + * + * - A: No, the layout doesn't change. + * + * - Q: Set the number of rows to zero in the grid layout manager and try again. + * What happens? + * + * - A: BOOM SHAKARAKA! + * + * - Q: What happens when you add more than one component to one of the five + * areas of the frame? What happens when one of the five areas does not contain + * any component? + * + * - A: The component will overlapping. + */ +public class Start { + public static void main(String[] args) { + // Dispather + javax.swing.SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new MyFrame(); + } + }); + } +} diff --git a/lab/lab10/Question5/MyFrame.java b/lab/lab10/Question5/MyFrame.java new file mode 100644 index 0000000..851b0dc --- /dev/null +++ b/lab/lab10/Question5/MyFrame.java @@ -0,0 +1,72 @@ + +/* + * Author: CHEN Yongyuan (Walter) from OOP(1007) + * Date: 2022-05-01 + * Description: This is the Main Frame class. + */ +import java.awt.BorderLayout; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JRadioButton; +import javax.swing.JTextField; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JPanel; +import java.awt.Color; +import java.awt.FlowLayout; +import java.awt.GridLayout; + +public class MyFrame extends JFrame { + + public MyFrame() { + setTitle("MyFrame"); + setSize(400, 300); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // center the frame + setLocationRelativeTo(null); + + setLayout(new BorderLayout()); + + // first panel + JPanel p1 = new JPanel(); + p1.setLayout(new BorderLayout(10, 10)); + p1.setBackground(Color.BLUE); + // second panel + JPanel p2 = new JPanel(); + p2.setLayout(new FlowLayout()); + p2.setBackground(Color.GREEN); + // third panel + JPanel p3 = new JPanel(); + p3.setLayout(new GridLayout(2, 2, 2, 2)); + // add panel to frame + add(p1, BorderLayout.PAGE_START); + add(p2, BorderLayout.CENTER); + add(p3, BorderLayout.PAGE_END); + + // tow button in the first panel + JButton b1 = new JButton("Button1"); + p1.add(BorderLayout.LINE_START, b1); + JButton b2 = new JButton("Button2"); + p1.add(BorderLayout.LINE_END, b2); + + // two label in the second panel + JLabel l = new JLabel("Enter your name: "); + p2.add(l); + JTextField tx = new JTextField("Type Text Here"); + p2.add(tx); + + // checkboxs in the third panel + JCheckBox cb = new JCheckBox("I agree"); + p3.add(cb); + JRadioButton rb = new JRadioButton("Yes"); + p3.add(rb); + JComboBox cb1 = new JComboBox(new String[] { "Red", "Green", "Blue" }); + p3.add(cb1); + JComboBox cb2 = new JComboBox(new Integer[] { 2, 7, -3, 24 }); + p3.add(cb2); + + setVisible(true); + } +} diff --git a/lab/lab10/Question5/Start.java b/lab/lab10/Question5/Start.java new file mode 100644 index 0000000..1408920 --- /dev/null +++ b/lab/lab10/Question5/Start.java @@ -0,0 +1,34 @@ +/** + * Answer to question. + * + * - Q: What happens when you run the program? + * + * - A: The layout change to 2 columns. Direction is LTR. The program change the + * layout to my desired, the layout use the number of columns that I specified. + * + * - Q: Use the mouse to resize the frame. Does the layout change? + * + * - A: No, the layout doesn't change. + * + * - Q: Set the number of rows to zero in the grid layout manager and try again. + * What happens? + * + * - A: BOOM SHAKARAKA! + * + * - Q: What happens when you add more than one component to one of the five + * areas of the frame? What happens when one of the five areas does not contain + * any component? + * + * - A: The component will overlapping. + */ +public class Start { + public static void main(String[] args) { + // Dispather + javax.swing.SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new MyFrame(); + } + }); + } +} diff --git a/lab/lab10/Question6/MyFrame.java b/lab/lab10/Question6/MyFrame.java new file mode 100644 index 0000000..c483b0c --- /dev/null +++ b/lab/lab10/Question6/MyFrame.java @@ -0,0 +1,27 @@ + +/* + * Author: CHEN Yongyuan (Walter) from OOP(1007) + * Date: 2022-05-01 + * Description: This is the Main Frame class. + */ +import java.awt.BorderLayout; +import javax.swing.JFrame; + +public class MyFrame extends JFrame { + + public MyFrame() { + setTitle("MyFrame"); + setSize(400, 300); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // center the frame + setLocationRelativeTo(null); + + setLayout(new BorderLayout()); + + // my draw panel + add(new MyPanel()); + + setVisible(true); + } +} diff --git a/lab/lab10/Question6/MyPanel.java b/lab/lab10/Question6/MyPanel.java new file mode 100644 index 0000000..efacfb2 --- /dev/null +++ b/lab/lab10/Question6/MyPanel.java @@ -0,0 +1,15 @@ +import java.awt.Color; +import java.awt.Graphics; +import javax.swing.JPanel; + +public class MyPanel extends JPanel { + @Override + protected void paintComponent(Graphics g) { + // clean the panel + super.paintComponent(g); + // draw + g.setColor(Color.RED); + g.drawString("hello", (int) Math.round(Math.random() * this.getWidth()), + (int) Math.round(Math.random() * this.getHeight())); + } +} diff --git a/lab/lab10/Question6/Start.java b/lab/lab10/Question6/Start.java new file mode 100644 index 0000000..f3e4297 --- /dev/null +++ b/lab/lab10/Question6/Start.java @@ -0,0 +1,15 @@ +/** + * Answer to question. + * + */ +public class Start { + public static void main(String[] args) { + // Dispather + javax.swing.SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new MyFrame(); + } + }); + } +} diff --git a/lab/lab10/Question7/MyFrame.java b/lab/lab10/Question7/MyFrame.java new file mode 100644 index 0000000..c483b0c --- /dev/null +++ b/lab/lab10/Question7/MyFrame.java @@ -0,0 +1,27 @@ + +/* + * Author: CHEN Yongyuan (Walter) from OOP(1007) + * Date: 2022-05-01 + * Description: This is the Main Frame class. + */ +import java.awt.BorderLayout; +import javax.swing.JFrame; + +public class MyFrame extends JFrame { + + public MyFrame() { + setTitle("MyFrame"); + setSize(400, 300); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // center the frame + setLocationRelativeTo(null); + + setLayout(new BorderLayout()); + + // my draw panel + add(new MyPanel()); + + setVisible(true); + } +} diff --git a/lab/lab10/Question7/MyPanel.java b/lab/lab10/Question7/MyPanel.java new file mode 100644 index 0000000..b33093a --- /dev/null +++ b/lab/lab10/Question7/MyPanel.java @@ -0,0 +1,27 @@ +import java.awt.Color; +import java.awt.Graphics; +import javax.swing.JPanel; + +public class MyPanel extends JPanel { + @Override + protected void paintComponent(Graphics g) { + // clean the panel + super.paintComponent(g); + // draw + g.setColor(Color.RED); + + g.drawRect(30, 30, 60, 60); + g.drawRect(15, 40, 60, 60); + g.drawLine(30, 30, 15, 40); + g.drawLine(90, 30, 75, 40); + g.drawLine(90, 90, 75, 100); + g.drawLine(30, 90, 15, 100); + // use g to draw a Cylinder + g.setColor(Color.BLUE); + g.drawOval(120, 20, 60, 20); + g.drawLine(120, 30, 120, 90); + g.drawLine(180, 30, 180, 90); + g.drawOval(120, 80, 60, 20); + + } +} diff --git a/lab/lab10/Question7/Start.java b/lab/lab10/Question7/Start.java new file mode 100644 index 0000000..f3e4297 --- /dev/null +++ b/lab/lab10/Question7/Start.java @@ -0,0 +1,15 @@ +/** + * Answer to question. + * + */ +public class Start { + public static void main(String[] args) { + // Dispather + javax.swing.SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new MyFrame(); + } + }); + } +}