[java]代码库
package AWT;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Label;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ListDemo1 {
public static void main(String[] args) throws FileNotFoundException,
IOException {
JFrame frame = new JFrame("英雄系统");
frame.setLayout(null);
frame.setResizable(false);
frame.setBounds(100, 100, 700, 500);
JButton bt1 = new JButton("德邦总管 ¥1");
Image i1 = ImageIO.read(new FileInputStream("./1.jpg"));
ImageIcon ic1 = new ImageIcon(i1);
bt1.setIcon(ic1);
bt1.setHorizontalTextPosition(JButton.CENTER);
bt1.setVerticalTextPosition(JButton.BOTTOM);
bt1.setBounds(20, 20, 150, 110);
frame.add(bt1);
JButton bt2 = new JButton("九尾妖狐 ¥2");
Image i2 = ImageIO.read(new FileInputStream("./2.jpg"));
ImageIcon ic2 = new ImageIcon(i2);
bt2.setIcon(ic2);
bt2.setHorizontalTextPosition(JButton.CENTER);
bt2.setVerticalTextPosition(JButton.BOTTOM);
bt2.setBounds(180, 20, 150, 110);
frame.add(bt2);
JButton bt3 = new JButton("放逐之刃 ¥3");
Image i3 = ImageIO.read(new FileInputStream("./3.jpg"));
ImageIcon ic3 = new ImageIcon(i3);
bt3.setIcon(ic3);
bt3.setHorizontalTextPosition(JButton.CENTER);
bt3.setVerticalTextPosition(JButton.BOTTOM);
bt3.setBounds(20, 160, 150, 110);
frame.add(bt3);
JButton bt4 = new JButton("探险家 ¥4");
Image i4 = ImageIO.read(new FileInputStream("./4.jpg"));
ImageIcon ic4 = new ImageIcon(i4);
bt4.setIcon(ic4);
bt4.setHorizontalTextPosition(JButton.CENTER);
bt4.setVerticalTextPosition(JButton.BOTTOM);
bt4.setBounds(180, 160, 150, 110);
frame.add(bt4);
JButton bt5 = new JButton("迅捷斥候 ¥5");
Image i5 = ImageIO.read(new FileInputStream("./5.jpg"));
ImageIcon ic5 = new ImageIcon(i5);
bt5.setIcon(ic5);
bt5.setHorizontalTextPosition(JButton.CENTER);
bt5.setVerticalTextPosition(JButton.BOTTOM);
bt5.setBounds(20, 300, 150, 110);
frame.add(bt5);
JButton bt6 = new JButton("战争之影 ¥6");
Image i6 = ImageIO.read(new FileInputStream("./6.jpg"));
ImageIcon ic6 = new ImageIcon(i6);
bt6.setIcon(ic6);
bt6.setHorizontalTextPosition(JButton.CENTER);
bt6.setVerticalTextPosition(JButton.BOTTOM);
bt6.setBounds(180, 300, 150, 110);
frame.add(bt6);
Font font = new Font("宋体", Font.BOLD, 20);
Label l1 = new Label("已点菜单:");
l1.setForeground(Color.BLACK);
l1.setFont(font);
l1.setBounds(350, 20, 120, 30);
frame.add(l1);
final List list = new List();
list.setFont(font);
list.setForeground(Color.BLUE);
list.setBounds(350, 50, 300, 300);
frame.add(list);
final Label l2 = new Label("英雄总价:0¥");
l2.setForeground(Color.black);
l2.setFont(font);
l2.setBounds(350, 360, 170, 30);
frame.add(l2);
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] items = list.getItems();
boolean flag = false;
int index = 0;
for (int i = 0; i < items.length; i++) {
if (items[i].contains("德邦总管")) {
index = i;
flag = true;
break;
}
}
if (flag) {
String item = list.getItem(index);
int x_index = item.indexOf("X");
String value = item.substring(x_index + 1);
item = item.substring(0, x_index + 1)
+ (Integer.parseInt(value) + 1);
list.remove(index);
list.add(item, index);
} else {
list.add("德邦总管 X1");
}
String value = l2.getText();
int begin = value.indexOf(":");
int end = value.indexOf("¥");
String money = value.substring(begin + 1, end);
String newValue = String.valueOf((Integer.parseInt(money) + 1));
value = value.replaceAll(money, newValue);
l2.setText(value);
}
});
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] items = list.getItems();
boolean flag = false;
int index = 0;
for (int i = 0; i < items.length; i++) {
if (items[i].contains("九尾妖狐")) {
index = i;
flag = true;
break;
}
}
if (flag) {
String item = list.getItem(index);
int x_index = item.indexOf("X");
String value = item.substring(x_index + 1);
item = item.substring(0, x_index + 1)
+ (Integer.parseInt(value) + 1);
list.remove(index);
list.add(item, index);
} else {
list.add("九尾妖狐 X1");
}
String value = l2.getText();
int begin = value.indexOf(":");
int end = value.indexOf("¥");
String money = value.substring(begin + 1, end);
String newValue = String.valueOf((Integer.parseInt(money) + 2));
value = value.replaceAll(money, newValue);
l2.setText(value);
}
});
bt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] items = list.getItems();
boolean flag = false;
int index = 0;
for (int i = 0; i < items.length; i++) {
if (items[i].contains("放逐之刃")) {
index = i;
flag = true;
break;
}
}
if (flag) {
String item = list.getItem(index);
int x_index = item.indexOf("X");
String value = item.substring(x_index + 1);
item = item.substring(0, x_index + 1)
+ (Integer.parseInt(value) + 1);
list.remove(index);
list.add(item, index);
} else {
list.add("放逐之刃 X1");
}
String value = l2.getText();
int begin = value.indexOf(":");
int end = value.indexOf("¥");
String money = value.substring(begin + 1, end);
String newValue = String.valueOf((Integer.parseInt(money) + 3));
value = value.replaceAll(money, newValue);
l2.setText(value);
}
});
bt4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] items = list.getItems();
boolean flag = false;
int index = 0;
for (int i = 0; i < items.length; i++) {
if (items[i].contains("探险家")) {
index = i;
flag = true;
break;
}
}
if (flag) {
String item = list.getItem(index);
int x_index = item.indexOf("X");
String value = item.substring(x_index + 1);
item = item.substring(0, x_index + 1)
+ (Integer.parseInt(value) + 1);
list.remove(index);
list.add(item, index);
} else {
list.add("探险家 X1");
}
String value = l2.getText();
int begin = value.indexOf(":");
int end = value.indexOf("¥");
String money = value.substring(begin + 1, end);
String newValue = String.valueOf((Integer.parseInt(money) + 4));
value = value.replaceAll(money, newValue);
l2.setText(value);
}
});
bt5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] items = list.getItems();
boolean flag = false;
int index = 0;
for (int i = 0; i < items.length; i++) {
if (items[i].contains("迅捷斥候")) {
index = i;
flag = true;
break;
}
}
if (flag) {
String item = list.getItem(index);
int x_index = item.indexOf("X");
String value = item.substring(x_index + 1);
item = item.substring(0, x_index + 1)
+ (Integer.parseInt(value) + 1);
list.remove(index);
list.add(item, index);
} else {
list.add("迅捷斥候 X1");
}
String value = l2.getText();
int begin = value.indexOf(":");
int end = value.indexOf("¥");
String money = value.substring(begin + 1, end);
String newValue = String.valueOf((Integer.parseInt(money) + 5));
value = value.replaceAll(money, newValue);
l2.setText(value);
}
});
bt6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] items = list.getItems();
boolean flag = false;
int index = 0;
for (int i = 0; i < items.length; i++) {
if (items[i].contains("战争之影")) {
index = i;
flag = true;
break;
}
}
if (flag) {
String item = list.getItem(index);
int x_index = item.indexOf("X");
String value = item.substring(x_index + 1);
item = item.substring(0, x_index + 1)
+ (Integer.parseInt(value) + 1);
list.remove(index);
list.add(item, index);
} else {
list.add("战争之影 X1");
}
String value = l2.getText();
int begin = value.indexOf(":");
int end = value.indexOf("¥");
String money = value.substring(begin + 1, end);
String newValue = String.valueOf((Integer.parseInt(money) + 6));
value = value.replaceAll(money, newValue);
l2.setText(value);
}
});
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
初级程序员
by: 云代码会员 发表于:2016-11-10 20:12:37 顶(0) | 踩(0) 回复
不错,知得学习
回复评论