lab10
This commit is contained in:
31
lab/lab10/Question2/MyFrame.java
Normal file
31
lab/lab10/Question2/MyFrame.java
Normal 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);
|
||||
}
|
||||
}
|
||||
20
lab/lab10/Question2/Start.java
Normal file
20
lab/lab10/Question2/Start.java
Normal 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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user