要求:车辆信息管理系统 型号 品牌 厂家 价格 联系人
1 车辆信息录入功能(用文件保存)
2 车辆信息浏览功能)
3 车辆信息按品牌查询
4 按价格段列出所选价格区的车辆
我们已经开始课程设计了 由于我们swing 没讲 但是课程设计需要 而且我们马上各科考试了
我还没怎么看 是在是不想自己去做了 所以没办法只能请这里的好心人帮我做一下 我参考一下
不用连接数据库 把数据保存在.txt文件中就行 (用java的输入输出流做就可以了
)界面也不用太复杂 。谢谢大家了
1 车辆信息录入功能(用文件保存)
2 车辆信息浏览功能)
3 车辆信息按品牌查询
4 按价格段列出所选价格区的车辆
我们已经开始课程设计了 由于我们swing 没讲 但是课程设计需要 而且我们马上各科考试了
我还没怎么看 是在是不想自己去做了 所以没办法只能请这里的好心人帮我做一下 我参考一下
不用连接数据库 把数据保存在.txt文件中就行 (用java的输入输出流做就可以了
)界面也不用太复杂 。谢谢大家了
思路都有,还不肯写,SWING没学用AWT做....
* Motor.java
*
* Created on 2009年7月14日, 下午2:31
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package com.motor.data;/**
* @author 9sky
*/
public class Motor {
private String type;//型号
private String brand;//品牌
private String factory;//厂家
private float price;//价格
private String linkman;//联系人 public String getType() {
return type;
} public void setType(String type) {
this.type = type;
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public String getFactory() {
return factory;
} public void setFactory(String factory) {
this.factory = factory;
} public float getPrice() {
return price;
} public void setPrice(float price) {
this.price = price;
} public String getLinkman() {
return linkman;
} public void setLinkman(String linkman) {
this.linkman = linkman;
}
}/*
* Manager.java
*
* Created on 2009年7月14日, 下午2:37
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package com.motor.data;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;/**
* @author 9sky
*/
public class Manager {
private static Manager instance= new Manager();
private Manager() {
}
public static Manager getInstance(){
return instance;
}
private List<Motor> list = Collections.synchronizedList(new ArrayList<Motor>());
public void add(Motor motor){
list.add(motor);
}
public void saveTo(File file) throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
for(Iterator<Motor> itr = list.iterator();itr.hasNext();){
Motor m = itr.next();
bw.append(m.getType()).append('\t').append(m.getBrand()).append('\t');
bw.append(m.getFactory()).append('\t').append(""+m.getPrice()).append('\t').append(m.getLinkman());
bw.newLine();
}
bw.flush();
bw.close();
}
public void loadFrom(File file) throws IOException{
BufferedReader br= new BufferedReader(new FileReader(file));
String line = null;
list.clear();
while((line=br.readLine())!=null){
String sub [] = line.split("\\t");
if(sub.length<5)continue;
Motor m = new Motor();
m.setType(sub[0]);m.setBrand(sub[1]);m.setFactory(sub[2]);
m.setPrice(Float.parseFloat(sub[3]));m.setLinkman(sub[4]);
add(m);
}
br.close();
}
public Set<String> getAllBrands(){
Set<String> types = new HashSet<String>();
for(Iterator<Motor> itr = list.iterator();itr.hasNext();){
types.add(itr.next().getBrand());
}
return types;
}
public List<Motor> queryByBrand(String brand){
List<Motor> result = new ArrayList<Motor>();
for(Iterator<Motor> itr = list.iterator();itr.hasNext();){
Motor m = itr.next();
if(brand.equals(m.getBrand()))result.add(m);
}
return result;
}
public List<Motor> queryByPrivceSegment(float start,float end){
List<Motor> result = new ArrayList<Motor>();
for(Iterator<Motor> itr = list.iterator();itr.hasNext();){
Motor m = itr.next();
if(start<=m.getPrice() && m.getPrice()<=end)result.add(m);
}
return result;
}
public int getMotorCount(){
return list.size();
}
public List<Motor> getAllMotors(){
return list;
}
}
* MotorTableModel.java
*
* Created on 2009年7月14日, 下午3:21
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package com.motor.swing.models;import com.motor.data.Manager;
import com.motor.data.Motor;
import java.util.List;
import javax.swing.table.AbstractTableModel;/**
* @author 9sky
*/
public class MotorTableModel extends AbstractTableModel{
final int ColumnCount = 4;
final String [] ColumnNames = new String[]{"型号","品牌","厂家","价格","联系人"};//型号 品牌 厂家 价格 联系人
private List<Motor> model;
public MotorTableModel(List<Motor> model){
this.model = model;
}
public int getRowCount() {
return model.size();
} public int getColumnCount() {
return ColumnCount;
} public Object getValueAt(int rowIndex, int columnIndex) {
switch(columnIndex){
case 0:
return model.get(rowIndex).getType();
case 1:
return model.get(rowIndex).getBrand();
case 2:
return model.get(rowIndex).getFactory();
case 3:
return model.get(rowIndex).getPrice();
case 4:
return model.get(rowIndex).getLinkman();
default:
return "查无此列信息" ;
}
}
public String getColumnName(int column) {
if(column<0 || column>ColumnNames.length)return "未知列";
return ColumnNames[column];
}
}/*
* PriceSegmentComboBoxModel.java
*
* Created on 2009年7月14日, 下午6:18
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package com.motor.swing.models;import javax.swing.DefaultComboBoxModel;/**
* @author 9sky
*/
public class PriceSegmentComboBoxModel extends DefaultComboBoxModel{
public static final float [] Segments = new float []{0,100,200,500};
public PriceSegmentComboBoxModel() {
super(getPriceSegments());
} private static String [] getPriceSegments() {
String segs [] = new String [Segments.length];
for(int i=0;i<segs.length-1;i++){
segs[i]=Segments[i]+" ~ "+Segments[i+1];
}
segs[segs.length-1] = "大于 "+Segments[Segments.length-1];
return segs;
}
public static float getBottomPrice(int index){
if(index>=Segments.length)return Float.MAX_VALUE;
return Segments[index];
}
public static float getTopPrice(int index){
if(index>=Segments.length-1)return Float.MAX_VALUE;
return Segments[index+1];
}
}/*
* TypeComboBoxModel.java
*
* Created on 2009年7月14日, 下午5:01
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package com.motor.swing.models;import com.motor.data.Manager;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import javax.swing.AbstractListModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.MutableComboBoxModel;/**
* @author 9sky
*/
public class TypeComboBoxModel extends DefaultComboBoxModel {
/** Creates a new instance of TypeComboBoxModel */
public TypeComboBoxModel() {
super(new Vector<String>(Manager.getInstance().getAllBrands()));
}
}
* MainFrame.java(1/3)
*
* Created on 2009年7月14日, 下午2:57
*/
package com.motor.swing;import com.motor.data.Manager;
import com.motor.data.Motor;
import com.motor.swing.models.MotorTableModel;
import com.motor.swing.models.PriceSegmentComboBoxModel;
import com.motor.swing.models.TypeComboBoxModel;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.table.TableModel;/**
* @author 9sky
*/
public class MainFrame extends javax.swing.JFrame {
public MainFrame() {
initComponents();
}
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">
private void initComponents() {
jFileChooser1 = new javax.swing.JFileChooser();
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jTextField5 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
jTable2 = new javax.swing.JTable();
jPanel3 = new javax.swing.JPanel();
jComboBox1 = new javax.swing.JComboBox();
jLabel1 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jPanel4 = new javax.swing.JPanel();
jSeparator2 = new javax.swing.JSeparator();
jLabel2 = new javax.swing.JLabel();
jComboBox2 = new javax.swing.JComboBox();
jScrollPane3 = new javax.swing.JScrollPane();
jTable3 = new javax.swing.JTable();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem(); jFileChooser1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jFileChooser1ActionPerformed(evt);
}
}); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jTabbedPane1StateChanged(evt);
}
}); jLabel3.setText("\u578b\u53f7\uff1a"); jLabel4.setText("\u54c1\u724c\uff1a"); jLabel5.setText("\u5382\u5bb6\uff1a"); jLabel6.setText("\u4ef7\u683c\uff1a"); jLabel7.setText("\u8054\u7cfb\u4eba\uff1a"); jButton1.setText("\u4fdd\u5b58");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
}); jLabel8.setText("*"); jLabel9.setText("*"); jLabel10.setText("*"); org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap(82, Short.MAX_VALUE)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel3)
.add(jLabel4)
.add(jLabel5)
.add(jLabel6))
.add(jLabel7))
.add(16, 16, 16)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jTextField5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 117, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jPanel1Layout.createSequentialGroup()
.add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 59, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel10))
.add(jPanel1Layout.createSequentialGroup()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(org.jdesktop.layout.GroupLayout.LEADING, jTextField2)
.add(org.jdesktop.layout.GroupLayout.LEADING, jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel9)
.add(jLabel8)))
.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 67, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 188, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
.addContainerGap(74, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(28, 28, 28)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel3)
.add(jLabel8))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel4)
.add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel9))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel5)
.add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel6)
.add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel10))
.add(8, 8, 8)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jTextField5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel7))
.add(36, 36, 36)
.add(jButton1)
.addContainerGap(32, Short.MAX_VALUE))
);
jTabbedPane1.addTab("\u5f55\u5165", jPanel1);
* MainFrame.java(2/3)
*/ jTable2.setModel(new MotorTableModel(Manager.getInstance().getAllMotors()));
jScrollPane2.setViewportView(jTable2); jTabbedPane1.addTab("\u6d4f\u89c8", jScrollPane2); jComboBox1.setModel(new TypeComboBoxModel());
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
}); jLabel1.setText("\u9009\u62e9\u7c7b\u578b\uff1a"); jTable1.setModel(getDefaultBrandTableModel());
jScrollPane1.setViewportView(jTable1); org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
.add(jPanel3Layout.createSequentialGroup()
.add(35, 35, 35)
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 71, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(238, Short.MAX_VALUE))
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1)
.add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))
);
jTabbedPane1.addTab("\u6309\u54c1\u724c\u67e5\u8be2", jPanel3); jLabel2.setText("\u9009\u62e9\u4ef7\u683c\u6bb5\uff1a"); jComboBox2.setModel(new PriceSegmentComboBoxModel());
jComboBox2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox2ActionPerformed(evt);
}
}); jTable3.setModel(getDefaultPriceSegmentTableModel());
jScrollPane3.setViewportView(jTable3); org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel4Layout.createSequentialGroup()
.add(29, 29, 29)
.add(jLabel2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jComboBox2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 80, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(223, Short.MAX_VALUE))
.add(jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
.add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel2)
.add(jComboBox2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))
);
jTabbedPane1.addTab("\u6309\u4ef7\u683c\u6bb5\u5217\u51fa", jPanel4); jMenu1.setText("\u6587\u4ef6");
jMenuItem1.setText("\u6253\u5f00");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
}); jMenu1.add(jMenuItem1); jMenuItem2.setText("\u4fdd\u5b58");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
}); jMenu1.add(jMenuItem2); jMenuBar1.add(jMenu1); setJMenuBar(jMenuBar1); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
jTable3.setModel(getDefaultPriceSegmentTableModel());
}
private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {
JTabbedPane tabbedPane = (JTabbedPane)evt.getSource();
int index = tabbedPane.getSelectedIndex();
switch(index){
case 0:
jTextField1.setText("");jTextField2.setText("");jTextField3.setText("");
jTextField4.setText("");jTextField5.setText("");
return;
case 1:
jTable2.setModel(new MotorTableModel(Manager.getInstance().getAllMotors()));
return;
case 2:
jComboBox1.setModel(new TypeComboBoxModel());
jTable1.setModel(getDefaultBrandTableModel());
return;
case 3:
jTable3.setModel(getDefaultPriceSegmentTableModel());
return;
default :
JOptionPane.showMessageDialog(this,"选项卡初始化失败");
}
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
jFileChooser1.showOpenDialog(this);
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
File file = jFileChooser1.getSelectedFile();
if(file==null || !file.exists()){
jFileChooser1.showSaveDialog(this);
return;
}
try {
Manager.getInstance().saveTo(file);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,ex.getMessage());
}
}
private void jFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) {
File file = jFileChooser1.getSelectedFile();
if(file==null || !file.exists()){
JOptionPane.showMessageDialog(this,"文件并不存在");
}
try {
if(jFileChooser1.getDialogType()==JFileChooser.OPEN_DIALOG){
Manager.getInstance().loadFrom(file);
}
if(jFileChooser1.getDialogType()==JFileChooser.SAVE_DIALOG){
Manager.getInstance().saveTo(file);
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,ex.getMessage());
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String type = jTextField1.getText();//型号
String brand = jTextField2.getText();//品牌
String factory = jTextField3.getText();//厂家
String price = jTextField4.getText();//价格
String linkman = jTextField5.getText();//联系人
if(type==null || brand==null || price==null)return;
Motor motor = new Motor();
motor.setBrand(brand);motor.setFactory(factory);
motor.setLinkman(linkman);motor.setPrice(Float.parseFloat(price));
motor.setType(type);
Manager.getInstance().add(motor);
jTextField1.setText("");jTextField2.setText("");jTextField3.setText("");
jTextField4.setText("");jTextField5.setText("");
}
* MainFrame.java(3/3)
*/
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
String type = jComboBox1.getSelectedItem().toString();
jTable1.setModel(new MotorTableModel(Manager.getInstance().queryByBrand(type)));
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
// 变量声明 - 不进行修改
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
private javax.swing.JFileChooser jFileChooser1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTable jTable2;
private javax.swing.JTable jTable3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
// 变量声明结束
/**
* 获得按品牌的查询结果
*/
private TableModel getDefaultBrandTableModel(){
String type ="";
if(jComboBox1.getItemCount()>0){
type = jComboBox1.getItemAt(0).toString();
}
return new MotorTableModel(Manager.getInstance().queryByBrand(type));
}
/**
* 获得价格段的查询结果
*/
private TableModel getDefaultPriceSegmentTableModel(){
int index = jComboBox2.getSelectedIndex();
float start = PriceSegmentComboBoxModel.getBottomPrice(index);
float end = PriceSegmentComboBoxModel.getTopPrice(index);
return new MotorTableModel(Manager.getInstance().queryByPrivceSegment(start,end));
}
}
谢谢。
我感觉这个比swing和awt简单吧……网上source一把一把的。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;public class Carsystem extends JFrame implements ActionListener {
JTabbedPane jtp;
JPanel jp1, jp2, jp3, jp4;
private JTextField jtf1, jtf2, jtf3, jtf4, jtf5, jtf6, jtf7;
private JButton jb1, jb2, jb3, jb4;
JTable jt;
JScrollPane jsp;
String[][] b;
String[] head = { "型号", "品牌", "厂家", "价格", "联系人" }; public Carsystem() {
super("车辆管理系统");
jtp = new JTabbedPane();
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp1.setLayout(null);
jp2.setLayout(null);
jp3.setLayout(null);
jp4 = new JPanel();
jp4.setLayout(null);
jtp.addTab("信息录入", jp1);
jtp.addTab("车辆浏览", jp2);
jtp.addTab("品牌查询", jp3);
jtp.addTab("价格查询", jp4);
jtf1 = new JTextField(8);
jtf2 = new JTextField(8);
jtf3 = new JTextField(8);
jtf4 = new JTextField(8);
jtf5 = new JTextField(8);
jtf6 = new JTextField(8);
jtf7 = new JTextField(8);
jb1 = new JButton("录入");
jb1.addActionListener(this);
jp1.setLayout(new GridLayout(6, 1));
jp1.add(jtf1);
jp1.add(jtf2);
jp1.add(jtf3);
jp1.add(jtf4);
jp1.add(jtf5);
jp1.add(jb1); this.add(jtp);
jb2 = new JButton("查询所有");
jb2.addActionListener(this);
jp2.setLayout(null);
jb2.setBounds(0, 0, 100, 30);
jp2.add(jb2);
jb3 = new JButton("品牌查询");
jb3.addActionListener(this);
jp3.setLayout(null);
jtf6.setBounds(0, 0, 100, 30);
jb3.setBounds(120, 0, 100, 30);
jp3.add(jb3);
jp3.add(jtf6); jb4 = new JButton("价格查询");
jb4.addActionListener(this);
jp4.setLayout(null);
jtf7.setBounds(0, 0, 100, 30);
jb4.setBounds(120, 0, 100, 30);
jp4.add(jb4);
jp4.add(jtf7);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 400);
this.setVisible(true);
} public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
new Carsystem();
} public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jb1) {
System.out.println("fds");
try {
createCheckFile("d:/car.txt", jtf1.getText() + "\t"
+ jtf2.getText() + "\t" + jtf3.getText() + "\t"
+ jtf4.getText() + "\t" + jtf5.getText() + "\t");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else if (e.getSource() == jb2) {
String text = "";
int count = 0;
try {
text = readCheckFile("d:/car.txt");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String[] str = text.split(" ");
for (int i = 0; i < str.length; i++) {
count++;
}
count = count / 5;
b = new String[count][5];
int x = 0, y = 0;
for (int i = 0; i < str.length; i++) {
b[x][y] = str[i];
y++;
if (y == 5) {
y = 0;
x++;
}
}
// //////////////////////创建JTable
jt = new JTable();
jsp = new JScrollPane(jt);
jsp.setBounds(0, 40, 400, 350);
jp2.add(jsp);
jt.setModel(new DefaultTableModel(b, head)); } else if (e.getSource() == jb3) {
String text = "";
int count = 0;
try {
text = readCheckFile("d:/car.txt");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String[] str = text.split(" ");
for (int i = 0; i < str.length; i++) {
count++;
}
count = count / 5;
b = new String[count][5];
int x = 0, y = 0;
for (int i = 0; i < str.length; i++) {
b[x][y] = str[i];
y++;
if (y == 5) {
y = 0;
x++;
}
}
int once = 0;
String[][] c = new String[count][5];
for (int i = 0; i < count; i++) {
if (b[i][1].startsWith(jtf6.getText().trim())) { for (int j = 0; j < 5; j++) {
c[once][j] = b[i][j];
}
once++;
}
}
// //////////////////////创建JTable
jt = new JTable();
jsp = new JScrollPane(jt);
jsp.setBounds(0, 40, 400, 350);
jp3.add(jsp);
jt.setModel(new DefaultTableModel(c, head));
} else if (e.getSource() == jb4) {
System.out.println("jb4");
String text = "";
int count = 0;
try {
text = readCheckFile("d:/car.txt");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String[] str = text.split(" ");
for (int i = 0; i < str.length; i++) {
count++;
}
count = count / 5;
b = new String[count][5];
int x = 0, y = 0;
for (int i = 0; i < str.length; i++) {
b[x][y] = str[i];
y++;
if (y == 5) {
y = 0;
x++;
}
}
int once = 0;
String[][] c = new String[count][5];
for (int i = 0; i < count; i++) {
if (Integer.parseInt(b[i][3]) == Integer.parseInt(jtf7
.getText().trim())) { for (int j = 0; j < 5; j++) {
c[once][j] = b[i][j];
}
once++;
}
}
// //////////////////////创建JTable
jt = new JTable();
jsp = new JScrollPane(jt);
jsp.setBounds(0, 40, 400, 350);
jp4.add(jsp);
jt.setModel(new DefaultTableModel(c, head));
}
} // 以下为网上COPY的文件写入的方法
public static void createCheckFile(String filePath, String detail)
throws Exception {
try {
File file = new File(filePath);
FileWriter filewriter = new FileWriter(file, true); // false表示不追加内容
filewriter.write(detail);
filewriter.close();
} catch (IOException e) {
throw e;
} catch (Exception ex) {
throw ex;
}
} public static String readCheckFile(String filePath) throws Exception {
BufferedReader bufread;
String read, readStr;
readStr = "";
try {
File file = new File(filePath);
FileReader fileread = new FileReader(file);
bufread = new BufferedReader(fileread);
while ((read = bufread.readLine()) != null) {
readStr = readStr + read;
}
} catch (Exception d) {
System.out.println(d.getMessage());
}
return readStr; // 返回从文本文件中读取内容
}}
把这个包也发上来被 org.jdesktop