swing组件编写一程序J1.java模拟至少5个鼠标事件
1个回答
展开全部
package 自己定个包名
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import org.w3c.dom.events.MouseEvent;
public class ActionTestThree extends JFrame implements MouseListener{//实现MouseListener接口,可以给多种组件添加事件
private static final long serialVersionUID = 1L;
private JTextArea jTextArea1 = new JTextArea(20, 30) ;
private JTextArea jTextArea2 = new JTextArea();
private JMenuBar jMenuBar = new JMenuBar();
private JMenu fileMenu = new JMenu("文件");
private JMenu editMenu = new JMenu("编辑");
private JMenu helpMenu = new JMenu("帮助");
private JMenuItem sendFileItem = new JMenuItem("发送");
private JMenuItem viewFileItem = new JMenuItem("查看");
private JMenuItem copyItem = new JMenuItem("复制");
private JMenuItem pasteItem = new JMenuItem("粘贴");
private JMenuItem knowItem = new JMenuItem("用户须知");
private JMenuItem aboutItem = new JMenuItem("关于我们");
private JSplitPane jSplitPaneV = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
private JPanel buttonPanel = new JPanel();
private JButton message = new JButton("消息记录");
private JButton sendButton = new JButton("发送");
private JButton closeButton = new JButton("关闭");
public ActionTestThree(){
this.setTitle("即时聊天软件");
this.setLayout(new BorderLayout());
this.setSize(500, 500);
fileMenu.add(sendFileItem);
fileMenu.add(viewFileItem);
editMenu.add(copyItem);
editMenu.add(pasteItem);
helpMenu.add(knowItem);
helpMenu.add(aboutItem);
jMenuBar.add(fileMenu);
jMenuBar.add(editMenu);
jMenuBar.add(helpMenu);
this.setJMenuBar(jMenuBar);
//jTextArea1.setBackground(Color.gray);
jSplitPaneV.setTopComponent(jTextArea1);
jSplitPaneV.setBottomComponent(jTextArea2);
this.add(jSplitPaneV);
buttonPanel.add(message);
buttonPanel.add(closeButton);
buttonPanel.add(sendButton);
sendButton.addMouseListener(this);//添加事件
this.add(buttonPanel,BorderLayout.SOUTH);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String args[]){
new ActionTestThree();
}
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
sendButton.setBackground(Color.blue);//点击按钮后的显示效果
}
@Override
public void mouseEntered(java.awt.event.MouseEvent e) {
sendButton.setBackground(Color.red);//将鼠标移到按钮上的显示效果
}
@Override
public void mouseExited(java.awt.event.MouseEvent e) {
sendButton.setBackground(null);//鼠标离开按钮后的显示效果
}
@Override
public void mousePressed(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import org.w3c.dom.events.MouseEvent;
public class ActionTestThree extends JFrame implements MouseListener{//实现MouseListener接口,可以给多种组件添加事件
private static final long serialVersionUID = 1L;
private JTextArea jTextArea1 = new JTextArea(20, 30) ;
private JTextArea jTextArea2 = new JTextArea();
private JMenuBar jMenuBar = new JMenuBar();
private JMenu fileMenu = new JMenu("文件");
private JMenu editMenu = new JMenu("编辑");
private JMenu helpMenu = new JMenu("帮助");
private JMenuItem sendFileItem = new JMenuItem("发送");
private JMenuItem viewFileItem = new JMenuItem("查看");
private JMenuItem copyItem = new JMenuItem("复制");
private JMenuItem pasteItem = new JMenuItem("粘贴");
private JMenuItem knowItem = new JMenuItem("用户须知");
private JMenuItem aboutItem = new JMenuItem("关于我们");
private JSplitPane jSplitPaneV = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
private JPanel buttonPanel = new JPanel();
private JButton message = new JButton("消息记录");
private JButton sendButton = new JButton("发送");
private JButton closeButton = new JButton("关闭");
public ActionTestThree(){
this.setTitle("即时聊天软件");
this.setLayout(new BorderLayout());
this.setSize(500, 500);
fileMenu.add(sendFileItem);
fileMenu.add(viewFileItem);
editMenu.add(copyItem);
editMenu.add(pasteItem);
helpMenu.add(knowItem);
helpMenu.add(aboutItem);
jMenuBar.add(fileMenu);
jMenuBar.add(editMenu);
jMenuBar.add(helpMenu);
this.setJMenuBar(jMenuBar);
//jTextArea1.setBackground(Color.gray);
jSplitPaneV.setTopComponent(jTextArea1);
jSplitPaneV.setBottomComponent(jTextArea2);
this.add(jSplitPaneV);
buttonPanel.add(message);
buttonPanel.add(closeButton);
buttonPanel.add(sendButton);
sendButton.addMouseListener(this);//添加事件
this.add(buttonPanel,BorderLayout.SOUTH);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String args[]){
new ActionTestThree();
}
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
sendButton.setBackground(Color.blue);//点击按钮后的显示效果
}
@Override
public void mouseEntered(java.awt.event.MouseEvent e) {
sendButton.setBackground(Color.red);//将鼠标移到按钮上的显示效果
}
@Override
public void mouseExited(java.awt.event.MouseEvent e) {
sendButton.setBackground(null);//鼠标离开按钮后的显示效果
}
@Override
public void mousePressed(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询