lab12
This commit is contained in:
30
lab/lab12/Question1/MyFrame.java
Normal file
30
lab/lab12/Question1/MyFrame.java
Normal file
@@ -0,0 +1,30 @@
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class MyFrame extends JFrame {
|
||||
public MyFrame() {
|
||||
// init frame
|
||||
setSize(400, 300);
|
||||
setTitle("MyFrame");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
// craete components
|
||||
JPanel panel = new JPanel();
|
||||
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
|
||||
JButton button1 = new JButton("left");
|
||||
JButton button2 = new JButton("right");
|
||||
|
||||
// set layout
|
||||
panel.add(button1);
|
||||
panel.add(button2);
|
||||
add(panel, BorderLayout.PAGE_START);
|
||||
|
||||
// MyPanel object
|
||||
MyPanel mp = new MyPanel(new FlowLayout(FlowLayout.CENTER));
|
||||
add(mp, BorderLayout.CENTER);
|
||||
|
||||
// done
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
||||
21
lab/lab12/Question1/MyPanel.java
Normal file
21
lab/lab12/Question1/MyPanel.java
Normal file
@@ -0,0 +1,21 @@
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
public class MyPanel extends JPanel {
|
||||
public MyPanel(LayoutManager layout) {
|
||||
super(layout);
|
||||
|
||||
// add anonymouse listener
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
// print if left clicked
|
||||
if (e.getButton() == MouseEvent.BUTTON1) {
|
||||
System.out.println("Left button clicked at " + e.getX() + "," + e.getY());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
12
lab/lab12/Question1/Start.java
Normal file
12
lab/lab12/Question1/Start.java
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007).
|
||||
*/
|
||||
public class Start {
|
||||
public static void main(String args[]) {
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
new MyFrame();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
28
lab/lab12/Question2/MyFrame.java
Normal file
28
lab/lab12/Question2/MyFrame.java
Normal file
@@ -0,0 +1,28 @@
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class MyFrame extends JFrame {
|
||||
public MyFrame() {
|
||||
// init
|
||||
setSize(400, 300);
|
||||
setTitle("MyFrame");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
// add layout
|
||||
JPanel panel = new JPanel();
|
||||
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
|
||||
JButton button1 = new JButton("left");
|
||||
JButton button2 = new JButton("right");
|
||||
panel.add(button1);
|
||||
panel.add(button2);
|
||||
add(panel, BorderLayout.PAGE_START);
|
||||
|
||||
// MyPanel object
|
||||
MyPanel mp = new MyPanel(new FlowLayout(FlowLayout.CENTER));
|
||||
add(mp, BorderLayout.CENTER);
|
||||
|
||||
// done
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
||||
37
lab/lab12/Question2/MyPanel.java
Normal file
37
lab/lab12/Question2/MyPanel.java
Normal file
@@ -0,0 +1,37 @@
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
public class MyPanel extends JPanel {
|
||||
private int x = -1;
|
||||
private int y = -1;
|
||||
|
||||
public MyPanel(LayoutManager layout) {
|
||||
super(layout);
|
||||
|
||||
// anonymouse listener
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
// left clicked
|
||||
if (e.getButton() == MouseEvent.BUTTON1) {
|
||||
System.out.println("Left button clicked at " + e.getX() + "," + e.getY());
|
||||
}
|
||||
x = e.getX();
|
||||
y = e.getY();
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
// red dot
|
||||
g.setColor(Color.RED);
|
||||
g.drawRect(x, y, 1, 1);
|
||||
}
|
||||
|
||||
}
|
||||
12
lab/lab12/Question2/Start.java
Normal file
12
lab/lab12/Question2/Start.java
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007).
|
||||
*/
|
||||
public class Start {
|
||||
public static void main(String args[]) {
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
new MyFrame();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user