用java写一个鼠标单击事件驱动程序
java题目:编写程序,显示当点击鼠标时,面板上交替出现两个消息“javaisfun”和“javaispowerful”。题目不难,主要是没时间想了,哪位朋友帮忙做一下,...
java题目:编写程序,显示当点击鼠标时,面板上交替出现两个消息“java is fun”和“java is powerful”。
题目不难,主要是没时间想了,哪位朋友帮忙做一下,感激不尽, 展开
题目不难,主要是没时间想了,哪位朋友帮忙做一下,感激不尽, 展开
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class A extends JFrame implements MouseListener {
private int index = 0;
private String[] msgs = { "java is fun", "java is powerful" };
private JLabel lbl = new JLabel(msgs[index]);
public A() {
this.setTitle("test");
this.setSize(300, 200);
this.add(lbl);
lbl.setFocusable(false);
addMouseListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new A();
}
public void mouseClicked(MouseEvent e) {
index = index == 0 ? 1 : 0;
lbl.setText(msgs[index]);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class A extends JFrame implements MouseListener {
private int index = 0;
private String[] msgs = { "java is fun", "java is powerful" };
private JLabel lbl = new JLabel(msgs[index]);
public A() {
this.setTitle("test");
this.setSize(300, 200);
this.add(lbl);
lbl.setFocusable(false);
addMouseListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new A();
}
public void mouseClicked(MouseEvent e) {
index = index == 0 ? 1 : 0;
lbl.setText(msgs[index]);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询