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,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);
}
}

View File

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