Bản quyền thuộc về TITV.vn,
vui lòng không đăng tải lại nội dung từ trang này.
Video giải thích chi tiết
Code chi tiết
package view;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import controller.MenuExampleController;
import controller.MenuExampleMouseListener;
public class MenuExampleView extends JFrame{
private JLabel jLabel;
public JPopupMenu jPopupMenu;
public MenuExampleView() {
this.setTitle("Menu Example");
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(new BorderLayout());
// Tạo controller
MenuExampleController menuExampleController = new MenuExampleController(this);
// Tạo thanh menu
JMenuBar jMenuBar = new JMenuBar();
// Tạo 1 menu
JMenu jMenu_file = new JMenu("File");
jMenu_file.setMnemonic(KeyEvent.VK_F);
jMenu_file.setDisplayedMnemonicIndex(0);
// Tạo các menu con
JMenuItem jMenuItem_new = new JMenuItem("New", KeyEvent.VK_N);
jMenuItem_new.addActionListener(menuExampleController);
jMenuItem_new.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK));
JMenuItem jMenuItem_open = new JMenuItem("Open", KeyEvent.VK_O);
jMenuItem_open.addActionListener(menuExampleController);
jMenuItem_open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
JMenuItem jMenuItem_exit = new JMenuItem("Exit", KeyEvent.VK_X);
jMenuItem_exit.addActionListener(menuExampleController);
jMenuItem_exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_DOWN_MASK));
jMenu_file.add(jMenuItem_new);
jMenu_file.add(jMenuItem_open);
jMenu_file.addSeparator();
jMenu_file.add(jMenuItem_exit);
JMenu jMenu_help = new JMenu("Help");
jMenu_help.setMnemonic(KeyEvent.VK_L);
jMenu_help.setDisplayedMnemonicIndex(0);
JMenuItem jMenuItem_welcome = new JMenuItem("Welcome");
jMenuItem_welcome.addActionListener(menuExampleController);
jMenu_help.add(jMenuItem_welcome);
jMenuBar.add(jMenu_file);
jMenuBar.add(jMenu_help);
// Thêm thanh thanh menu vào chương trình
this.setJMenuBar(jMenuBar);
// Tạo ToolBar (Thanh công cụ)
JToolBar jToolBar = new JToolBar();
JButton jButton_Undo = new JButton("Undo");
jButton_Undo.setToolTipText("Nhấn vào đây để quay lại thao tác vừa rồi!");
jButton_Undo.addActionListener(menuExampleController);
JButton jButton_Redo = new JButton("Redo");
jButton_Redo.addActionListener(menuExampleController);
JButton jButton_Copy = new JButton("Copy");
jButton_Copy.addActionListener(menuExampleController);
JButton jButton_Cut = new JButton("Cut");
jButton_Cut.addActionListener(menuExampleController);
JButton jButton_Paste = new JButton("Paste");
jButton_Paste.addActionListener(menuExampleController);
jToolBar.add(jButton_Undo);
jToolBar.add(jButton_Redo);
jToolBar.add(jButton_Copy);
jToolBar.add(jButton_Cut);
jToolBar.add(jButton_Paste);
this.add(jToolBar, BorderLayout.NORTH);
// Menu chuột phải JPopupMenu
jPopupMenu = new JPopupMenu();
JMenu jMenuPoupFont = new JMenu("Font");
JMenuItem jMenuItemType = new JMenuItem("Type");
jMenuItemType.addActionListener(menuExampleController);
JMenuItem jMenuItemSize = new JMenuItem("Size");
jMenuItemSize.addActionListener(menuExampleController);
jMenuPoupFont.add(jMenuItemType);
jMenuPoupFont.add(jMenuItemSize);
JMenuItem jMenuItemCut = new JMenuItem("Cut");
jMenuItemCut.addActionListener(menuExampleController);
JMenuItem jMenuItemCopy = new JMenuItem("Copy");
jMenuItemCopy.addActionListener(menuExampleController);
JMenuItem jMenuItemPaste = new JMenuItem("Paste");
jMenuItemPaste.addActionListener(menuExampleController);
jPopupMenu.add(jMenuPoupFont);
jPopupMenu.addSeparator();
jPopupMenu.add(jMenuItemCut);
jPopupMenu.add(jMenuItemCopy);
jPopupMenu.add(jMenuItemPaste);
this.add(jPopupMenu);
// Thêm sự kiện phải chuột vào JLabel
MenuExampleMouseListener menuExampleMouseListener = new MenuExampleMouseListener(this);
this.addMouseListener(menuExampleMouseListener);
// Tạo label hiển thị
Font font = new Font("Arial", Font.BOLD, 50);
jLabel = new JLabel();
this.add(jLabel, BorderLayout.CENTER);
this.setVisible(true);
}
public void setTextJLabel(String s) {
this.jLabel.setText(s);
}
}
package controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import view.MenuExampleView;
public class MenuExampleController implements ActionListener{
private MenuExampleView menuExampleView;
public MenuExampleController(MenuExampleView menuExampleView) {
this.menuExampleView = menuExampleView;
}
@Override
public void actionPerformed(ActionEvent e) {
String button = e.getActionCommand();
if(button.equals("Exit")) {
System.exit(0);
}else{
this.menuExampleView.setTextJLabel("Bạn đã click: "+button);
}
}
}
package controller;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import view.MenuExampleView;
public class MenuExampleMouseListener implements MouseListener{
MenuExampleView menuExampleView;
public MenuExampleMouseListener(MenuExampleView menuExampleView) {
this.menuExampleView = menuExampleView;
}
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
this.menuExampleView.jPopupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
if (e.isPopupTrigger()) {
this.menuExampleView.jPopupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
package test;
import javax.swing.UIManager;
import view.MenuExampleView;
public class Test {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new MenuExampleView();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Không có nhận xét nào:
Đăng nhận xét