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.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.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 看不惯勿喷。 如果与你要求有出入,直接追问
public static void main(String args[]) {
String menu = "北京烤鸭:189元 西芹炒肉:12.9元 酸菜鱼:69元 铁板牛柳:32元";
Scanner scanner = new Scanner(menu);// Scanner类创建scanner,将menu传递给构造方法的参数
String regex = "[^0123456789.]+";
// scanner调用useDelimiter(String regex),将regex传递给该方法的参数
scanner.useDelimiter(regex);
double sum = 0;
while (scanner.hasNext()) {
try {
double price = scanner.nextDouble();// scanner调用nextDouble()返回数字单词
sum = sum + price;
System.out.println(price);
} catch (InputMismatchException exp) {
String t = scanner.next();
}
}
System.out.println("菜单总价格:" + sum + "元");
}
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);
}}