JPanelTest.java
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class JPanelTest extends JPanel {
private static final long serialVersionUID = 1L;
/**
* Create the panel.
*/
public JPanelTest(int x, int y, int width, int height) {
setBorder(new LineBorder(Color.BLACK));
setBounds(x, y, width, height);
JLabel lbl = new JLabel("Test");
add(lbl);
addMouseListener(ma);
}
MouseAdapter ma = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(e.getX()+" "+e.getY()); // relatif to JPanel (0,0)
}
};
}
UISusah.java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.border.LineBorder;
import java.awt.Color;
public class UISusah extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UISusah frame = new UISusah();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private int width = 530 ; // 85+350+85 = 520 -> 530
private int height = 495 ; // 85+290+85 = 460 -> 495
/**
* Create the frame.
*/
public UISusah() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(width, height);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
/*
JPanel panel = new JPanel();
FlowLayout flowLayout = (FlowLayout) panel.getLayout();
flowLayout.setVgap(0);
flowLayout.setHgap(0);
panel.setBounds(0, 0, 85, 85);
contentPane.add(panel);
*/
// 1
JPanelTest panel = new JPanelTest(0, 0, 85, 85);
contentPane.add(panel);
// 2
JPanel panel1 = new JPanel();
panel1.setBorder(new LineBorder(Color.BLACK));
FlowLayout flowLayout_1 = (FlowLayout) panel1.getLayout();
flowLayout_1.setVgap(0);
flowLayout_1.setHgap(0);
panel1.setBounds(85-1, 0, 350, 85);
contentPane.add(panel1);
/*
JPanel panel2 = new JPanel();
panel2.setBorder(new LineBorder(Color.BLACK));
FlowLayout flowLayout_2 = (FlowLayout) panel2.getLayout();
flowLayout_2.setVgap(0);
flowLayout_2.setHgap(0);
panel2.setBounds(350+85-2, 0, 85, 85);
contentPane.add(panel2);
*/
// 3
JPanelTest panel2 = new JPanelTest(350+85-2, 0, 85, 85);
contentPane.add(panel2);
// 4
JPanel panel3 = new JPanel();
panel3.setBorder(new LineBorder(Color.BLACK));
FlowLayout flowLayout_3 = (FlowLayout) panel3.getLayout();
flowLayout_3.setVgap(0);
flowLayout_3.setHgap(0);
panel3.setBounds(0, 85-1, 85, 290);
contentPane.add(panel3);
// 5
JPanel panel4 = new JPanel();
panel4.setBorder(new LineBorder(Color.BLACK));
FlowLayout flowLayout_4 = (FlowLayout) panel4.getLayout();
flowLayout_4.setVgap(0);
flowLayout_4.setHgap(0);
panel4.setBounds(85-1, 85-1, 350, 290);
contentPane.add(panel4);
// 6
JPanel panel5 = new JPanel();
panel5.setBorder(new LineBorder(Color.BLACK));
FlowLayout flowLayout_5 = (FlowLayout) panel5.getLayout();
flowLayout_5.setVgap(0);
flowLayout_5.setHgap(0);
panel5.setBounds(350+85-2, 85-1, 85, 290);
contentPane.add(panel5);
// 7
JPanel panel6 = new JPanel();
panel6.setBorder(new LineBorder(Color.BLACK));
FlowLayout flowLayout_6 = (FlowLayout) panel6.getLayout();
flowLayout_6.setVgap(0);
flowLayout_6.setHgap(0);
panel6.setBounds(0, 290+85-2, 85, 85);
contentPane.add(panel6);
// 8
JPanel panel7 = new JPanel();
panel7.setBorder(new LineBorder(Color.BLACK));
FlowLayout flowLayout_7 = (FlowLayout) panel7.getLayout();
flowLayout_7.setVgap(0);
flowLayout_7.setHgap(0);
panel7.setBounds(85-1, 290+85-2, 350, 85);
contentPane.add(panel7);
// 9
JPanel panel8 = new JPanel();
panel8.setBorder(new LineBorder(Color.BLACK));
FlowLayout flowLayout_8 = (FlowLayout) panel8.getLayout();
flowLayout_8.setVgap(0);
flowLayout_8.setHgap(0);
panel8.setBounds(350+85-2, 290+85-2, 85, 85);
contentPane.add(panel8);
}
}
No comments:
Post a Comment