This commit is contained in:
2022-05-18 23:06:27 +08:00
parent 1357f94bfb
commit d0416b02bd
6 changed files with 140 additions and 0 deletions

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

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

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

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

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

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