王朝网络
分享
 
 
 

XB Tool

王朝java/jsp·作者佚名  2006-01-09
宽屏版  字体: |||超大  

package gemini.tool;

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.util.ArrayList;

import javax.swing.BoxLayout;

import javax.swing.DefaultListModel;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JComponent;

import javax.swing.JDialog;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JList;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextField;

import javax.swing.ListSelectionModel;

import javax.swing.event.ListSelectionEvent;

import javax.swing.event.ListSelectionListener;

/**

* @author Leif.Wang Last Modified:2004-10-13

*

*/

public class XbTool

extends JPanel

implements ActionListener, ListSelectionListener {

private static final String NEW_LINE = "\n";

private static final String SPLIT = " | ";

private static final String CAPTION = "XB tool";

private static final String ATTACK = "主动攻击";

private static final String GANG = "群体";

private static final String POISON = "中毒";

private static final String DESTROY = "破坏盔甲";

private static final int POS_X = 250;

private static final int POS_Y = 180;

private static final int LIST_WIDTH = 200;

private static final int LIST_HEIGHT = 300;

private static final int FRAME_WIDTH = 500;

private static final int FRAME_HEIGHT = 500;

private JButton openButton, saveButton;

private JCheckBox attackBox, gangBox, poisonBox, destroyBox;

private JFileChooser fc;

private ArrayList monsters;

private JList list;

private DefaultListModel listModel;

private JTextField levelField;

private JLabel filePathLabel;

public XbTool() {

super(new BorderLayout());

//Create a file chooser

fc = new JFileChooser();

//Create the open button. We use the image from the JLF

//Graphics Repository (but we extracted it from the jar).

openButton = new JButton("Open a File...");

openButton.addActionListener(this);

//Create the save button. We use the image from the JLF

//Graphics Repository (but we extracted it from the jar).

saveButton = new JButton("Save a File...");

saveButton.addActionListener(this);

//For layout purposes, put the buttons in a separate panel

JPanel buttonPanel = new JPanel(); //use FlowLayout

buttonPanel.add(openButton);

buttonPanel.add(saveButton);

// Creates a list model

listModel = new DefaultListModel();

listModel.addElement("ID" + SPLIT + "Name");

// Creates a list

list = new JList(listModel);

list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

list.setLayoutOrientation(JList.VERTICAL);

list.setVisibleRowCount(-1);

list.addListSelectionListener(this);

// Creates a scroll pane for the list

JScrollPane listScroller = new JScrollPane(list);

listScroller.setPreferredSize(new Dimension(LIST_WIDTH, LIST_HEIGHT));

// Creates text field for level

levelField = new JTextField();

levelField.addActionListener(this);

// Creates check boxes

attackBox = new JCheckBox(ATTACK);

attackBox.setSelected(false);

attackBox.addActionListener(this);

gangBox = new JCheckBox(GANG);

gangBox.setSelected(false);

gangBox.addActionListener(this);

poisonBox = new JCheckBox(POISON);

poisonBox.setSelected(false);

poisonBox.addActionListener(this);

destroyBox = new JCheckBox(DESTROY);

destroyBox.setSelected(false);

destroyBox.addActionListener(this);

filePathLabel = new JLabel("Please open a cfg file!");

// Creates a panel for the check boxes

JPanel checkPanel = new JPanel();

checkPanel.setLayout(new BoxLayout(checkPanel, BoxLayout.PAGE_AXIS));

checkPanel.add(filePathLabel);

checkPanel.add(levelField);

checkPanel.add(attackBox);

checkPanel.add(gangBox);

checkPanel.add(poisonBox);

checkPanel.add(destroyBox);

//Add the buttons and the log to this panel.

add(buttonPanel, BorderLayout.PAGE_START);

add(listScroller, BorderLayout.WEST);

add(checkPanel, BorderLayout.CENTER);

}

/**

* Handles event for buttons

*/

public void actionPerformed(ActionEvent e) {

Object source = e.getSource();

if (source == openButton) {

if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {

File file = fc.getSelectedFile();

filePathLabel.setText(file.getPath());

// Gets monsters from file

monsters = (ArrayList) Monster.loadMonstersFromFile(file);

for (int i = 0; i < monsters.size(); i++) {

Monster monster = (Monster) monsters.get(i);

listModel.addElement(monster.getId() + SPLIT + monster.getName());

}

}

} else if (source == saveButton) {

if (monsters != null

&& fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {

File file = fc.getSelectedFile();

Monster.saveMonstersIntoFile(monsters, file);

}

} else {

if (list.getSelectedIndex() < 1)

return;

// Gets the monster

Monster monster = (Monster) monsters.get(list.getSelectedIndex() - 1);

// Sets the properites

if (source == attackBox) {

monster.setAttack(attackBox.isSelected());

} else if (source == gangBox) {

monster.setGang(gangBox.isSelected());

} else if (source == poisonBox) {

monster.setPoison(poisonBox.isSelected());

} else if (source == destroyBox) {

monster.setDestroy(destroyBox.isSelected());

} else if (source == levelField) {

try {

monster.setLevel(Integer.parseInt(levelField.getText()));

} catch (NumberFormatException e1) {

JOptionPane.showMessageDialog(

null,

"Please input a number!",

"Information",

JOptionPane.INFORMATION_MESSAGE);

}

}

}

}

/**

* Handles event for list

*/

public void valueChanged(ListSelectionEvent e) {

int index = list.getSelectedIndex();

if (index == 0)

return;

Monster monster = (Monster) monsters.get(index - 1);

levelField.setText(monster.getLevel() + "");

attackBox.setSelected(monster.isAttack());

gangBox.setSelected(monster.isGang());

poisonBox.setSelected(monster.isPoison());

destroyBox.setSelected(monster.isDestroy());

}

/**

* Create the GUI and show it. For thread safety, this method should be

* invoked from the event-dispatching thread.

*/

private static void createAndShowGUI() {

//Make sure we have nice window decorations.

JFrame.setDefaultLookAndFeelDecorated(true);

JDialog.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.

JFrame frame = new JFrame(CAPTION);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocation(POS_X, POS_Y);

//Create and set up the content pane.

JComponent newContentPane = new XbTool();

newContentPane.setOpaque(true); //content panes must be opaque

frame.setContentPane(newContentPane);

//Display the window.

frame.pack();

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);

frame.setVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

////////////////////////////////////////////////////////////////////////////

package gemini.tool;

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collection;

/**

* @author Leif.Wang

* Last Modified:2004-10-13

*

*/

public class Monster {

private static final String SPLIT = "\t,";

private static final String REG_SPLIT = "\\,";

private int id;

private String name;

private int level;

private boolean attack;

private boolean gang;

private boolean poison;

private boolean destroy;

public void setDestroy(boolean destroy) {

this.destroy = destroy;

}

public boolean isDestroy() {

return destroy;

}

public void setPoison(boolean poison) {

this.poison = poison;

}

public boolean isPoison() {

return poison;

}

public void setGang(boolean gang) {

this.gang = gang;

}

public boolean isGang() {

return gang;

}

public void setAttack(boolean attack) {

this.attack = attack;

}

public boolean isAttack() {

return attack;

}

public void setLevel(int level) {

this.level = level;

}

public int getLevel() {

return level;

}

public String getName() {

return name;

}

public int getId() {

return id;

}

/**

* Default constructor

*/

public Monster() {

id = 0;

name = "";

setLevel(0);

setAttack(false);

setGang(false);

setPoison(false);

setDestroy(false);

}

/**

* Constructor with parameters

*/

public Monster(int id, String name, int level, boolean attack, boolean gang,

boolean poison, boolean destroy) {

this.id = id;

this.name = name;

this.setLevel(level);

this.setAttack(attack);

this.setGang(gang);

this.setPoison(poison);

this.setDestroy(destroy);

}

/**

* Overides Objects toString method to display monster information

*

* @see java.lang.Object#toString()

*/

public String toString() {

return "$" + getId() + SPLIT

+ getName() + SPLIT

+ getLevel() + SPLIT

+ (isAttack() ? "1" : "0") + SPLIT

+ (isGang() ? "1" : "0") + SPLIT

+ (isPoison() ? "1" : "0") + SPLIT

+ (isDestroy() ? "1" : "0")

+ "\t;";

}

/**

* Gets monsters from file

*/

public static Collection loadMonstersFromFile(File file) {

try {

ArrayList monsters = new ArrayList();

BufferedReader buffer = new BufferedReader(new FileReader(file));

String line = buffer.readLine();

Monster monster = null;

while (line != null) {

String[] parameters = line.split(REG_SPLIT);

monster = new Monster(

Integer.parseInt(parameters[0].trim().substring(1)),

parameters[1].trim(),

Integer.parseInt(parameters[2].trim()),

"1".equals(parameters[3].trim()),

"1".equals(parameters[4].trim()),

"1".equals(parameters[5].trim()),

"1".equals(parameters[6].charAt(0) + "")

);

monsters.add(monster);

line = buffer.readLine();

}

return monsters;

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

/**

* Save the monsters information into file

*/

public static void saveMonstersIntoFile(ArrayList monsters, File file) {

DataOutputStream out = null;

try {

out = new DataOutputStream(new FileOutputStream(file));

for (int i = 0; i < monsters.size(); i++) {

out.writeBytes(monsters.get(i).toString() + "\n");

}

out.flush();

out.close();

} catch (Exception e) {

try {

out.close();

} catch (IOException e1) {

e1.printStackTrace();

}

e.printStackTrace();

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有