我想實(shí)現(xiàn)的功能是 鼠標(biāo)右擊會(huì)出現(xiàn)《復(fù)制》《粘貼》,然后左上角菜單點(diǎn)擊可以實(shí)現(xiàn)出 打開(kāi)一個(gè)文本文件。最終實(shí)現(xiàn)的是可以對(duì)比兩段源程序的相似度import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.InputEvent;import java.awt.event.MouseEvent;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import javax.swing.*;import java.util.*;public class text2 { public static void main(String args[]) { new WindowFlow("程序相似度"); }}class WindowFlow extends JFrame{ JTextField field1,field2; JTextArea text1,text2; JButton controlButton; ? ? ? ? ? ? ? ? ? ? ? //按鈕 JMenuBar menubar; JMenu menu; JLabel one,two; JPopupMenu menu1; FileDialog ?op; JMenuItem copy,cut,paste; WindowFlow(String s){ setTitle(s); setBounds(200,200,730,460); setVisible(true); menu1=new JPopupMenu(); copy=new JMenuItem("復(fù)制"); cut=new JMenuItem("剪切"); paste=new JMenuItem("粘貼"); field1=new JTextField(6); field2=new JTextField(6); text1=new JTextArea(12,22); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//文本區(qū)1 text2=new JTextArea(12,22); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//文本區(qū)2 menubar=new JMenuBar(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //菜單條 menu=new JMenu("打開(kāi)文件"); ? ? ? ? ? ? ? ? ? ? ? ? ? ?//菜單 controlButton=new JButton("計(jì)算"); ? ? ? ? ? ? ? //按鈕 JPanel panel=new JPanel(); ? ? ? ? ? ? ? ? ? ? ?//中間容器 final TextField txtField = new TextField(50); panel.add(text1); panel.add(text2); panel.add(field1); op = new FileDialog(this, "打開(kāi)文件", FileDialog.LOAD); ? ? //新建對(duì)話框 ?? panel.add(controlButton); menu.addActionListener(this); ? ? ? ? ? ? ? ? ? ?//設(shè)置按鈕點(diǎn)擊監(jiān)聽(tīng)事件 ? panel.add(field2); menubar.add(menu); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //菜單添加到菜單條 setJMenuBar(menubar);? menu1.add(copy); menu1.add(cut); menu1.add(paste); add(panel,BorderLayout.CENTER); add(new JScrollPane(text1),BorderLayout.WEST); add(new JScrollPane(text2),BorderLayout.EAST); validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e){ if(e.getSource()==copy) text1.copy(); else if(e.getSource()==cut) text1.cut(); else if(e.getSource()==paste) text1.paste(); else if(e.getSource()==menu){ String str; op.setVisible(true); try{ File f1=new File(op.getDirectory(),op.getFile()); FileReader fr=new FileReader(f1); BufferedReader br=new BufferedReader(fr); text1.setText(""); while((str=br.readLine())!=null) text1.append(str+'\n'); fr.close(); } catch (Exception e1){ e1.printStackTrace(); } } } }
java窗體菜單監(jiān)視器問(wèn)題
Acap
2016-06-29 18:40:47