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.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) throws Exception {
StringBuffer sb = new StringBuffer();
Scanner sc = new Scanner(System.in);
for (int i = 0; i 3; i++) {
System.out.print("请输入商品名字:\r\n");
String name = sc.nextLine();
System.out.print("请输入商品价格:\r\n");
String price = sc.nextLine();
sb.append("("+name+"/"+price+")\r\n");
}
File file = new File("C:\\MyData.txt");
if(!file.exists()){
file.createNewFile();
}
OutputStream os = new FileOutputStream(file);
byte[] bytes = sb.toString().getBytes();
os.write(bytes, 0, bytes.length);
os.close();
sc.close();
}
}
public static void main ( String args[])
{
new MenuTest().setVisible(true);
}
}
不要写在另一个方法内。
另外,是这个名称的
PopupMenu pop;
package sum;
import java.util.ArrayList;
import java.util.Scanner;
public class TestDemo {
public static void main(String[] args){
ArrayListGoods arr = new ArrayListGoods();
Goods g1 = new Goods(1, "羽毛球", 237);
Goods g2 = new Goods(2, "羽毛球拍", 113);
Goods g3 = new Goods(3, "护腕", 100);
arr.add(g1);
arr.add(g2);
arr.add(g3);
System.out.println("***********************商品查询系统*********************");
System.out.println("1.羽毛球 2.羽毛球拍 3.护腕");
System.out.println("*****************************************************");
Scanner sc = new Scanner(System.in);
outer: while(true){
System.out.print("请选择商品号:");
String num = sc.next();
for(int i = 0; i arr.size(); i++){
if((arr.get(i).getId() + "").equals(num)){
System.out.println(arr.get(i).getName() + " " + arr.get(i).getPrice() + "元" );
}else if(num.equals("n")){
System.out.println("谢谢使用");
break outer;
}
}
}
}
}
class Goods{
public Goods(int id, String name, int price){
this.id = id;
this.name = name;
this.price = price;
}
private int id;
private String name;
private int price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
很辛苦写的希望能采纳
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Scanner;
public class menu {
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer buffer =new StringBuffer();
String[] num={"first","second","third"};
//变量i 为输入的次数。 如果有变动 直接修改即可。
for(int i = 0 ;i3;i++){
System.out.println("Please enter the name of the "+num[i]+" item:");
Scanner nameSc = new Scanner(System.in);
buffer.append("("+nameSc.next()+"/");
System.out.println("Please enter the "+num[i]+" item price:");
nameSc = new Scanner(System.in);
buffer.append(nameSc.next()+")");
if(i2)
buffer.append("\r\n");
}
System.out.println(buffer.toString());
try {
//1、打开流
Writer w=new FileWriter("C://menu.txt",true);
//2、写入内容
w.write(buffer.toString());
//3、关闭流
w.close();
} catch (IOException e) {
System.out.println("文件写入错误:"+e.getMessage());
}
}
}
//代码写的比较low 看不惯勿喷。 如果与你要求有出入,直接追问