大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
public static void main ( String args[])
成都创新互联公司服务项目包括金山网站建设、金山网站制作、金山网页制作以及金山网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,金山网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到金山省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
{
new MenuTest().setVisible(true);
}
}
不要写在另一个方法内。
另外,是这个名称的
PopupMenu pop;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.datatransfer.*;
class MyMenuBar extends MenuBar{
public MyMenuBar(Frame parent){
parent.setMenuBar(this);
}
public void addMenus(String [] menus){
for(int i=0;imenus.length;i++)
add(new Menu(menus[i]));
}
public void addMenuItems(int menuNumber,String[] items){
for(int i=0;iitems.length;i++){
if(items[i]!=null)
getMenu(menuNumber).add(new MenuItem(items[i]));
else getMenu(menuNumber).addSeparator();
}
}
public void addActionListener(ActionListener al){
for(int i=0;igetMenuCount();i++)
for(int j=0;jgetMenu(i).getItemCount();j++)
getMenu(i).getItem(j).addActionListener(al);
}
}
class MyFile{
private FileDialog fDlg;
public MyFile(Frame parent){
fDlg=new FileDialog(parent,"",FileDialog.LOAD);
}
private String getPath(){
return fDlg.getDirectory()+"\\"+fDlg.getFile();
}
public String getData() throws IOException{
fDlg.setTitle("打开");
fDlg.setMode(FileDialog.LOAD);
fDlg.setVisible(true);
BufferedReader br=new BufferedReader(new FileReader(getPath()));
StringBuffer sb=new StringBuffer();
String aline;
while((aline=br.readLine())!=null)
sb.append(aline+'\n');
br.close();
return sb.toString();
}
public void setData(String data) throws IOException{
fDlg.setTitle("保存");
fDlg.setMode(FileDialog.SAVE);
fDlg.setVisible(true);
BufferedWriter bw=new BufferedWriter(new FileWriter(getPath()));
bw.write(data);
bw.close();
}
}
class MyClipboard{
private Clipboard cb;
public MyClipboard(){
cb=Toolkit.getDefaultToolkit().getSystemClipboard();
}
public void setData(String data){
cb.setContents(new StringSelection(data),null);
}
public String getData(){
Transferable content=cb.getContents(null);
try{
return (String) content.getTransferData(DataFlavor.stringFlavor);
//DataFlavor.stringFlavor会将剪贴板中的字符串转换成Unicode码形式的String对象。
//DataFlavor类是与存储在剪贴板上的数据的形式有关的类。
}catch(Exception ue){}
return null;
}
}
class MyFindDialog extends Dialog implements ActionListener{
private Label lFind=new Label("查找字符串");
private Label lReplace=new Label("替换字符串");
private TextField tFind=new TextField(10);
private TextField tReplace=new TextField(10);
private Button bFind=new Button("查找");
private Button bReplace=new Button("替换");
private TextArea ta;
public MyFindDialog(Frame owner,TextArea ta){
super(owner,"查找",false);
this.ta=ta;
setLayout(null);
lFind.setBounds(10,30,80,20);
lReplace.setBounds(10,70,80,20);
tFind.setBounds(90,30,90,20);
tReplace.setBounds(90,70,90,20);
bFind.setBounds(190,30,80,20);
bReplace.setBounds(190,70,80,20);
add(lFind);
add(tFind);
add(bFind);
add(lReplace);
add(tReplace);
add(bReplace);
setResizable(false);
bFind.addActionListener(this);
bReplace.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyFindDialog.this.dispose();
}
});
}//构造函数结束
public void showFind(){
setTitle("查找");
setSize(280,60);
setVisible(true);
}
public void showReplace(){
setTitle("查找替换");
setSize(280,110);
setVisible(true);
}
private void find(){
String text=ta.getText();
String str=tFind.getText();
int end=text.length();
int len=str.length();
int start=ta.getSelectionEnd();
if(start==end) start=0;
for(;start=end-len;start++){
if(text.substring(start,start+len).equals(str)){
ta.setSelectionStart(start);
ta.setSelectionEnd(start+len);
return;
}
}
//若找不到待查字符串,则将光标置于末尾
ta.setSelectionStart(end);
ta.setSelectionEnd(end);
}
public Button getBFind() {
return bFind;
}
private void replace(){
String str=tReplace.getText();
if(ta.getSelectedText().equals(tFind.getText()))
ta.replaceRange(str,ta.getSelectionStart(),ta.getSelectionEnd());
else find();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bFind)
find();
else if(e.getSource()==bReplace)
replace();
}
}
public class MyMemo extends Frame implements ActionListener{
private TextArea editor=new TextArea(); //可编辑的TextArea
private MyFile mf=new MyFile(this);//MyFile对象
private MyClipboard cb=new MyClipboard();
private MyFindDialog findDlg=new MyFindDialog(this,editor);
public MyMemo(String title){ //构造函数
super(title);
MyMenuBar mb=new MyMenuBar(this);
//添加需要的菜单及菜单项
mb.addMenus(new String[]{"文件","编辑","查找","帮助"});
mb.addMenuItems(0,new String[]{"新建","打开","保存",null,"全选"});
mb.addMenuItems(1,new String[]{"剪贴","复制","粘贴","清除",null,"全选"});
mb.addMenuItems(2,new String[]{"查找",null,"查找替换"});
mb.addMenuItems(3,new String[]{"我的记事本信息"});
add(editor); //为菜单项注册动作时间监听器
mb.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyMemo.this.dispose();
}
}); //分号不能忘了
} //构造函数完
public void actionPerformed(ActionEvent e){
String selected=e.getActionCommand(); //获取菜单项标题
if(selected.equals("新建"))
editor.setText("");
else if(selected.equals("打开")){
try{
editor.setText(mf.getData());
}catch(IOException ie){}
}
else if(selected.equals("保存")){
try{
mf.setData(editor.getText());
}catch(IOException ie){}
}
else if(selected.equals("退出")){
dispose();
}
else if(selected.equals("剪贴")){
//将选中的字符串复制到剪贴板中并清除字符串
cb.setData(editor.getSelectedText());
editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd());
}
else if(selected.equals("复制")){
cb.setData(editor.getSelectedText());
}
else if(selected.equals("粘贴")){
String str=cb.getData();
editor.replaceRange(str,editor.getSelectionStart(),editor.getSelectionEnd());
//粘贴在光标位置
}
else if(selected.equals("清除")){
editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd());
}
else if(selected.equals("全选")){
editor.setSelectionStart(0);
editor.setSelectionEnd(editor.getText().length());
}
else if(selected.equals("查找")){
findDlg.showFind();
}
else if(selected.equals("查找替换")){
findDlg.showReplace();
}
}
public static void main(String[] args){
MyMemo memo=new MyMemo("记事本");
memo.setSize(650,450);
memo.setVisible(true);
}
}
参考这个程序吧。有不懂的可以找我。呵呵
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyMenu extends JFrame{
JMenuBar jmbar=new JMenuBar();
JMenu jmenu=new JMenu("颜色");
JMenuItem jmt1=new JMenuItem("红色"),
jmt2=new JMenuItem("黄色"),
jmt3=new JMenuItem("蓝色");
JPanel jp=new JPanel();
MyMenu(){
setTitle("菜单测试");
setSize(400,300);
setJMenuBar(jmbar);
jmbar.add(jmenu);
jmenu.add(jmt1);
jmenu.add(jmt2);
jmenu.add(jmt3);
add(jp);
jmt1.addActionListener(new MenuAction(this));
jmt2.addActionListener(new MenuAction(this));
jmt3.addActionListener(new MenuAction(this));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new MyMenu();
}
}
class MenuAction implements ActionListener{
MyMenu m;
MenuAction(MyMenu m){
this.m=m;
}
public void actionPerformed(ActionEvent e){
String color=e.getActionCommand();
if(color=="红色")m.jp.setBackground(Color.red);
else if(color=="黄色")m.jp.setBackground(Color.yellow);
else if(color=="蓝色")m.jp.setBackground(Color.blue);
}
}
不知道你要什么事件代码,我写了个比较简单的你看适合不。
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class Test_11 extends Frame implements ActionListener{ /**
* @param args
*/
MenuBar mb = null;
Menu m = null;
MenuItem mi = null; public Test_11() {
this.setBounds(100, 100, 400, 300);
this.setVisible(true);
mb = new MenuBar();
m = new Menu("文件");
mi = new MenuItem("打开");
mi.setShortcut(new MenuShortcut(KeyEvent.VK_O));
m.add(mi);
mb.add(m);
setMenuBar(mb);
mi.addActionListener(this);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public static void main(String[] args) {
// TODO Auto-generated method stub
new Test_11();
} @Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}}
import javax.swing.*;
import java.awt.*;
public class MenuBarOne extends JFrame
{
JPanel jp=new JPanel();
JMenuBar menubar=new JMenuBar();
JMenu menu1=new JMenu("菜单");
JMenu menu2=new JMenu("菜单");
JMenu menu3=new JMenu("菜单");
JMenuItem item1=new JMenuItem("菜单项");
JMenuItem item2=new JMenuItem("菜单项");
JMenuItem item3=new JMenuItem("菜单项");
JMenuItem item4=new JMenuItem("菜单项");
JMenuItem item5=new JMenuItem("菜单项");
JMenuItem item6=new JMenuItem("菜单项");
public MenuBarOne()
{
jp.setLayout(new BorderLayout());
menu1.add(item1);
menu1.add(item2);
menu1.add(item3);
menu2.add(item4);
menu2.add(item5);
menu3.add(item6);
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
jp.add(menubar,BorderLayout.NORTH);
this.add(jp);
this.setTitle("菜单示意");
this.setBounds(100,100,280,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new MenuBarOne();
}
}
很简单,就是个菜单框架.
希望有用吧
package mm;
import java.io.*;
import java.awt.*;
import javax.swing.*;
public class HelloWorld {
public static void main(String[] args){
JFrame w=new JFrame("SKH");
Container con=w.getContentPane();
//Color
con.setBackground(Color.pink);
//Location
w.setBounds(60, 100, 388, 308);
//可见性
w.setVisible(true);
//单击右上角容器的行为
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建菜单项
JMenuBar menubar=new JMenuBar();
JMenu menu,submenu;
JMenuItem a,b;
menu=new JMenu("菜单");
submenu=new JMenu("子菜单");
a=new JMenuItem("菜单项1");
b=new JMenuItem("菜单项2");
menu.add(a);
//添加分割符;
menu.addSeparator();
menu.add(b);
menu.addSeparator();
menu.add(submenu);
submenu.add(new JMenuItem("QWQ"));
menubar.add(menu);
w.setJMenuBar(menubar);
}
}