This commit is contained in:
2022-05-01 23:24:00 +08:00
parent 0b354c7561
commit 053113577f
16 changed files with 493 additions and 0 deletions

View File

@@ -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<String> cb1 = new JComboBox<String>(new String[] { "Red", "Green", "Blue" });
p3.add(cb1);
JComboBox<Integer> cb2 = new JComboBox<Integer>(new Integer[] { 2, 7, -3, 24 });
p3.add(cb2);
setVisible(true);
}
}

View File

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