我对Java比较陌生。我所看到的所有示例在程序中仅具有一种侦听器类型。我想开发一个程序来查看鼠标事件,按钮和其他组件的单击或动作以及键盘事件。该程序以绝对模式进行编码,因为我想准确地放置多边形并能够在其中单击/。以下测试程序开始起作用。
公共类Layout2扩展了JFrame {
public JPanel panel;
public Panel cmds;
JButton btn_test = new JButton("Test");
JLabel lb_test = new JLabel("Object");
JLabel lb_test2 = new JLabel("LB - Test");
Polygon[] poly = new Polygon[3];
public void buildPanel() {
panel = new JPanel();
panel.setLayout(null);
panel.setPreferredSize(new Dimension(1000, 100));
JButton b1 = new JButton("MC");
JButton b2 = new JButton("MR");
JButton b3 = new JButton("MS");
JButton b4 = new JButton("M+");
JButton b5 = new JButton("M-");
//Insets insets = pane.getInsets();
//Dimension size = b1.getPreferredSize();
b1.setBounds(5, 5, 52, 42);
//size = b2.getPreferredSize();
b2.setBounds(57, 5, 52, 42);
//size = b3.getPreferredSize();
b3.setBounds(109, 5, 52, 42);
b4.setBounds(161, 5, 52, 42);
b5.setBounds(213, 5, 52, 42);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
//JButton btn_test = new JButton("Test");
btn_test.setBounds(410, 5, 80, 42);
panel.add(btn_test);
//JLabel lb_test = new JLabel("Object");
lb_test.setBounds(550, 5, 120, 42);
panel.add(lb_test);
lb_test2.setBounds(100, 5, 80, 42);
lb_test2.setLocation(100, 10);
add(lb_test2, BorderLayout.NORTH);
cmds = new Panel();
cmds.setLayout(null);
cmds.setPreferredSize(new Dimension(1000, 100));
cmds.setBackground(Color.black);
panel.add(cmds);
//panel.add(TrianglePanel);
}
private Layout2() {
//Create and set up the window.
super("AbsoluteLayoutDemo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
//Set up the content pane.
buildPanel();
add(panel, BorderLayout.SOUTH);
add(new TrianglePanel(), BorderLayout.CENTER);
//Size and display the window.
setSize(1280, 1000);
setVisible(true);
}
class TrianglePanel extends JPanel implements MouseListener {
public TrianglePanel() {
//Create triangle
poly[0] = new Polygon();
poly[0].addPoint(150, 300);
poly[0].addPoint(100, 200);
poly[0].addPoint(200, 200);
poly[1] = new Polygon();
poly[1].addPoint(125, 25);
poly[1].addPoint(145, 120);
poly[1].addPoint(180, 90);
poly[1].addPoint(200, 145);
poly[1].addPoint(200, 25);
poly[2] = new Polygon();
poly[2].addPoint(350, 300);
poly[2].addPoint(300, 200);
poly[2].addPoint(400, 200);
//Add mouse Listener
addMouseListener(this);
//Set size to make sure that the whole triangle is shown
setPreferredSize(new Dimension(1000, 800));
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Color.red);
//g2d.fill(triangle);
g2d.draw(poly[0]);
g2d.draw(poly[2]);
g2d.setPaint(Color.blue);
g2d.fill(poly[1]);
g2d.draw(poly[1]);
g2d.setFont(new Font("Courier", Font.PLAIN, 16)); //TimesRoman
g2d.setPaint(Color.yellow);
g2d.drawString("10", 150, 50);
g2d.setPaint(Color.blue);
g2d.fill(poly[1]);
g2d.setPaint(Color.yellow);
g2d.drawString(" ", 150, 50);
g2d.drawString("5", 150, 50);
}
//Required methods for MouseListener, though the only one you care about is click
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
public void btn_testActionPerformed(java.awt.event.MouseEvent evt){//java.awt.event.ActionEvent // TODO:在此处添加您的处理代码: System.out.println(“ btn_test2”); }
@Override
public void mouseClicked(MouseEvent e) {
Point p = e.getPoint();
for (int pn = 0; pn < 3; pn++) {
if (poly[pn].contains(p)) {
lb_test.setText("Polygon: " + pn);
lb_test2.setText("Polygon: " + pn);
}
}
}
}
public static void main(String[] args) {
new Layout2();
}
}