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);
}}
希望有用吧
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);
}
}
//核心代码
String[] sg= {"苹果","香蕉","樱桃","山楂"};
JComboBoxString jcb = new JComboBoxString(sg);
完整代码
import java.awt.*;
import javax.swing.*;
public class MFrame extends JFrame{
public MFrame() {
String[] sg= {"苹果","香蕉","樱桃","山楂"};
JComboBoxString jcb = new JComboBoxString(sg);
add(jcb);
setLayout(new FlowLayout());//把JFrame设置成流式布局
setTitle("测试");//设置窗口标题
setSize(220, 100);//设置窗口大小
setLocationRelativeTo(null);//设置窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置点击关闭退出jvm虚拟机
setVisible(true);//设置窗口可见
}
public static void main(String[] args) {
new MFrame();//创建窗口
}
}
#include iostream
#include string //C++磁盘操作头文件
#include istream
#include fstream
#include iomanip//自定义输出宽度
using namespace std;
struct Book //结构类型
{
string bookname;//书名
string author;//作者名
Book *next;
};
class BookList
{
public:
BookList(){first=new Book;first-next=NULL;}
int NewBook();
void SOldBook();
void testread();
void testwrite();
void DisplayBook();
~BookList();
private:
Book *first;
int j;
};
int BookList::NewBook()
{
Book *s;s=new Book; //为插入的新书申请结点
Book *q;q=new Book;
Book *p;p=new Book;
cout"输入新书书名:";
cins-bookname;
cout"输入作者:";
cins-author;
p=first;q=first; //建立链表
if(first-next==NULL)//如果原链表为空,把新书的结点插入到链表中
{
first-next=s; //头插法
s-next=NULL;
}
else{
while(p-next-booknames-bookname)//按书名排序插入
{
p=p-next;
if(p-next==NULL)break;
}
s-next=p-next;
p-next=s;
}
return 1;
}
void BookList::SOldBook() //查询
{
Book *s;s=new Book;
Book *p;p=new Book;
cout"输入你要查询的书名:";
cins-bookname;
p=first-next;
while(p-bookname!=s-bookname)
{
p=p-next;//接着查找下一个结点
if(p==NULL)break;
}
if(p==NULL)
cout"你所查询的书没找到!"endl;
else
cout"找到:《"p-bookname"》 "p-authorendl;
}
void BookList::testread()
{
ifstream in;j=0;//读出文件流
in.open("BookList.txt",ios::in);//以读的形式打开原文件
if(!in)
{
cerr"错误!不能打开文件!";
exit(1);
}
Book *q;q=new Book;
Book *p;p=new Book;
p=first;
cout"从BookList.txt中读出已有图书如下:"endl;
while(!in.eof())//判断文件是否为空
{
inq-booknameq-author;//从文件里面读到链表
coutq-bookname" "q-authorendl;
j++;
p-next=q;
p=q;
q-next=new Book;
q=q-next;
}
p-next=NULL;
cout"共有图书"j"本"endlendl;
in.close();
}
void BookList::DisplayBook()
{
j=0;
cout"本馆现有书籍有:"endl;
Book *p;p=new Book;
p=first-next;
while(p!=NULL) //输出链表中的元素
{
coutp-bookname" "p-authorendl;
p=p-next;
j++;
}
cout"共有图书"j"本"endl;
}
void BookList::testwrite()
{
ofstream out; //写入文件流
out.open("BookList.txt",ios::out);
if(!out)
{
cerr"错误!不能打开文件!";
exit(1);
}
Book *p;p=new Book;
for(p=first-next;p!=NULL;p=p-next)
{
outp-bookname'\t'p-authorendl; //把链表中的元素写到文件中
}
out.close();
}
BookList::~BookList() //析构函数
{
Book *p;p=new Book;
Book *q;q=new Book;
p=first-next;
while(p)
{
q=p;
p=p-next;
delete q;
}
}
void main()
{
char s;
int i,k,count=0;
BookList L; //定义对象
L.testread(); //把文件中的内容读到链表中
while(1)
{
char *menu[]={"输入新书","查询旧书","显示所有图书","退出",""}; //字符串指针数组
for(i=0;menu[i][0]!='\0';i++)
couti+1"、 "menu[i]endl;
cink;
switch(k)
{
case 1:count=L.NewBook();break;
case 2:L.SOldBook();break;
case 3:L.DisplayBook();break;
default:if(k!=4)cout"输入错误!"endl;break;
}
if(k==4)
{ if(count)
{ cout"修改后的BookList.txt未保存,是否保存?(y/n):";
cins;
if(s=='y')
L.testwrite();
}
break;
}
}
}