package com.xingzhong.ore;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class GlassJframeTest extends JFrame implements ItemListener {
private JFrame jf = new JFrame("Welcome to Century");// 初始化一个带标题窗体
// JLabel实例化
private JLabel jlGrassId = new JLabel("glass_id:");// 初始化一个JLabel
private JLabel jlGrade = new JLabel("grade:");// 初始化一个JLabel
private JLabel jlBatch = new JLabel("batch_id:");// 初始化一个JLabel
private JLabel jlBatchSize = new JLabel("batch_size:");// 初始化一个JLabel
private JLabel jlLotId = new JLabel("lot_id:");// 初始化一个JLabel
private JLabel byGlassIdJLabel = new JLabel("byGlassId");
private JLabel byBatchIdJLabel = new JLabel("byBatchId");
private JLabel byLotIdJLabel = new JLabel("byLotId");
private JRadioButton byGlassId = new JRadioButton();
private JRadioButton byBatchId = new JRadioButton();
private JRadioButton byLotId = new JRadioButton();
private JTabbedPane addTab = new JTabbedPane(JTabbedPane.TOP);
private JTabbedPane queryTab = new JTabbedPane(JTabbedPane.TOP);
// JButton实例化
private JButton jbAdd = new JButton("add");// 初始化一个JButton
private JButton jbQuery = new JButton("query");// 初始化一个JButton
// private JButton jbUpdate = new JButton("update");// 初始化一个JButton
// JTextField实例化
private JTextField tfGrassId = new JTextField();
private JTextField tfGrade = new JTextField();
String gradeList[] = { "G1", "G2", "G3", "G4", "G5", "G6" };
private JComboBox jcb = new JComboBox(gradeList);
private JTextField tfBatch = new JTextField();
private JTextField tfBatchSize = new JTextField();
private JTextField tfLotId = new JTextField();
private JTable table = null;
private DefaultTableModel tableModel = null;
String[] titles = { "glass_id", "grade", "batch_id", "batch_size", "lot_id" };
Object[][] glassInfo = { { "ww", "ss", "ss", "11", "ss" },
{ "ss", "ss", "ss", "ss", "ss" }, { "ss", "ss", "ss", "ss", "ss" },
{ "ss", "ss", "ss", "ss", "ss" } };
public GlassJframeTest() {
jbAdd.addActionListener(new ActionListener() {
// add event
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getSource() == jbAdd) {
// reset glass id
//
String grassId = tfGrassId.getText();
String grade = jcb.getSelectedItem().toString();
String batchId = tfBatch.getText();
String batchSize = tfBatchSize.getText();
String lotId = tfLotId.getText();
Glass glass = new Glass();
glass.setGlassId(grassId);
glass.setGrade(grade);
Batch bt = new Batch(batchId, Integer.parseInt(batchSize));
glass.setBatch(bt);
Lot lot = new Lot(lotId, bt);
glass.setLot(lot);
glass.addGlass();// add glass
System.out.println("grassId=" + grassId + ",grade=" + grade
+ ",batchId=" + batchId + ",batchSize=" + batchSize
+ ",lotId=" + lotId);
}
}
});
// query event
jbQuery.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getSource() == jbQuery) {
tableModel = new DefaultTableModel(glassInfo, titles);
table = new JTable(tableModel);
JScrollPane scr = new JScrollPane(table);
JPanel toolBar = new JPanel();
// jf.setLayout()
table.setBounds(10, 250, 400, 100);
jf.add(table);
Glass glass = new Glass();
String grassId = tfGrassId.getText();
glass.queryGlass(grassId);
System.out.println("jbQuery");
}
}
});
// 增加tab event
addTab.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
// System.out.println("mouseReleased");
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
// if(arg0.getSource()==addTab){
//
// }
// System.out.println("mousePressed");
//
//
// jlGrassId.setBounds(5, 30, 100, 15);//
// tfGrassId.setBounds(70, 30, 100, 20);//
//
// jlGrassId.setVisible(true);
//
// // label
// jlGrade.setVisible(true);
// jlBatch.setVisible(true);
// jlBatchSize.setVisible(true);
// jlLotId.setVisible(true);
//
// // text
// tfGrassId.setVisible(true);
// tfGrade.setVisible(true);
// jcb.setVisible(true);
// tfBatch.setVisible(true);
// tfBatchSize.setVisible(true);
// tfLotId.setVisible(true);
// tfLotId.setVisible(true);
// jbAdd.setVisible(true);
// jbQuery.setVisible(false);
// // jTable.setVisible(false);
//
// // by
// byGlassId.setVisible(false);
// byGlassIdJLabel.setVisible(false);
// byBatchId.setVisible(false);
// byBatchIdJLabel.setVisible(false);
// byLotId.setVisible(false);
// byLotIdJLabel.setVisible(false);
addJFrameLayout();
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
// System.out.println("mouseExited");
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
// System.out.println("mouseEntered");
}
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
// System.out.println("mouseClicked");
}
});
// 查询event
queryTab.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
// System.out.println("mouseReleased");
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
//
// jf.add(byGlassId);
// jf.add(byBatchId);
// jf.add(byLotId);
//
// jf.add(byGlassIdJLabel);
// jf.add(byBatchIdJLabel);
// jf.add(byLotIdJLabel);
// byGlassIdJLabel.setBounds(5, 30, 100, 15);//
// byGlassId.setBounds(70, 30, 100, 20);//
// by
// byGlassId.setVisible(true);
// byGlassIdJLabel.setVisible(true);
// byBatchId.setVisible(true);
// byBatchIdJLabel.setVisible(true);
// byLotId.setVisible(true);
// byLotIdJLabel.setVisible(true);
//
// // byBatchId.setSelected(true);
// byGlassIdJLabel.setVisible(true);
// jlGrassId.setVisible(false);
// tfGrassId.setVisible(false);
//
// System.out.println("mousePressed");
// jlGrade.setVisible(false);
// jlBatch.setVisible(false);
// jlBatchSize.setVisible(false);
// jlLotId.setVisible(false);
//
// tfGrade.setVisible(false);
// jcb.setVisible(false);
// tfBatch.setVisible(false);
// tfBatchSize.setVisible(false);
// tfLotId.setVisible(false);
// tfLotId.setVisible(false);
// jbAdd.setVisible(false);
// jbQuery.setVisible(true);
// // jTable.setVisible(true);
//
// // reset jlGrassId位置
// jlGrassId.setBounds(5, 60, 100, 15);//
// tfGrassId.setBounds(60, 60, 100, 20);//
// jlGrassId.setVisible(true);
// tfGrassId.setVisible(true);
queryJFrameLayout();
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
// System.out.println("mouseExited");
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
// System.out.println("mouseEntered");
}
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
// System.out.println("mouseClicked");
}
});
// by glass event
// byGlassId.addMouseListener(new MouseListener() {
//
// public void mouseReleased(MouseEvent arg0) {
// // TODO Auto-generated method stub
//
// }
//
// public void mousePressed(MouseEvent arg0) {
// // TODO Auto-generated method stub
//
//
//
// }
//
// public void mouseExited(MouseEvent arg0) {
// // TODO Auto-generated method stub
//
// }
//
// public void mouseEntered(MouseEvent arg0) {
// // TODO Auto-generated method stub
//
// }
//
// public void mouseClicked(MouseEvent arg0) {
// // TODO Auto-generated method stub
// if (byGlassId.isSelected()) {
// System.out.println("by glass id");
// jlGrassId.setBounds(5, 60, 100, 15);//
// tfGrassId.setBounds(60, 60, 100, 20);//
// jlGrassId.setVisible(true);
// tfGrassId.setVisible(true);
// jlBatch.setVisible(false);
// tfGrade.setVisible(false);
// jlLotId.setVisible(false);
// tfLotId.setVisible(false);
// }
// }
// });
//
// // byBatchId event
// byBatchId.addMouseListener(new MouseListener() {
//
// public void mouseReleased(MouseEvent arg0) {
// // TODO Auto-generated method stub
//
// }
//
// public void mousePressed(MouseEvent arg0) {
// // TODO Auto-generated method stub
//
// }
//
// public void mouseExited(MouseEvent arg0) {
// // TODO Auto-generated method stub
//
// }
//
// public void mouseEntered(MouseEvent arg0) {
// // TODO Auto-generated method stub
//
// }
//
// public void mouseClicked(MouseEvent arg0) {
// // TODO Auto-generated method stub
// if (byBatchId.isSelected()) {
// System.out.println("by batch id");
// jlBatch.setBounds(5, 60, 100, 15);//
// tfBatch.setBounds(60, 60, 100, 20);//
// jlBatch.setVisible(true);
// tfBatch.setVisible(true);
// jlGrassId.setVisible(false);
// tfGrassId.setVisible(false);
// jlLotId.setVisible(false);
// tfLotId.setVisible(false);
// }
// }
// });
//
// // byLotID event
// byLotId.addMouseListener(new MouseListener() {
//
// public void mouseReleased(MouseEvent arg0) {
// // TODO Auto-generated method stub
//
// }
//
// public void mousePressed(MouseEvent arg0) {
// // TODO Auto-generated method stub
//
// }
//
// public void mouseExited(MouseEvent arg0) {
// // TODO Auto-generated method stub
//
// }
//
// public void mouseEntered(MouseEvent arg0) {
// // TODO Auto-generated method stub
//
// }
//
// public void mouseClicked(MouseEvent arg0) {
// // TODO Auto-generated method stub
// if (byLotId.isSelected()) {
// System.out.println("by lot id");
// jlLotId.setBounds(5, 60, 100, 15);//
// tfLotId.setBounds(60, 60, 100, 20);//
// jlLotId.setVisible(true);
// tfLotId.setVisible(true);
// jlGrassId.setVisible(false);
// tfGrassId.setVisible(false);
// jlBatch.setVisible(false);
// tfBatch.setVisible(false);
//
// }
// }
// });
jf.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(1);
}
});
jf.setLayout(null);// 使用绝对定位
// add addtab
addTab.addTab("增加", new JLabel());
addTab.setBounds(0, 0, 50, 25);//
jf.add(addTab);
// add querytab
queryTab.addTab("查询", new JLabel());
queryTab.setBounds(50, 0, 50, 25);//
jf.add(queryTab);
// // 设置glass_id textbox 位置于大小
// jlGrassId.setBounds(5, 30, 100, 15);//
// tfGrassId.setBounds(70, 30, 100, 20);//
// jf.add(jlGrassId);// 向容器增加JLabel组件
// jf.add(tfGrassId);// 向容器增加JButton组件
//
// // 设置grade textbox 位置于大小
// jlGrade.setBounds(200, 30, 100, 15);//
// jcb.setBounds(250, 30, 100, 20);//
// jf.add(jlGrade);// 向容器增加JLabel组件
// jf.add(jcb);// 向容器增加JButton组件
//
// // 设置batch textbox 位置于大小
// jlBatch.setBounds(5, 70, 100, 15);//
// tfBatch.setBounds(70, 70, 100, 20);//
// jf.add(jlBatch);// 向容器增加JLabel组件
// jf.add(tfBatch);// 向容器增加JButton组件
//
// // 设置batchsize textbox 位置于大小
// jlBatchSize.setBounds(200, 70, 100, 15);//
// tfBatchSize.setBounds(270, 70, 100, 20);//
// jf.add(jlBatchSize);// 向容器增加JLabel组件
// jf.add(tfBatchSize);// 向容器增加JButton组件
//
// // 设置lotId textbox 位置于大小
// jlLotId.setBounds(5, 110, 100, 15);//
// tfLotId.setBounds(70, 110, 100, 20);//
// jf.add(jlLotId);// 向容器增加JLabel组件
// jf.add(tfLotId);// 向容器增加JButton组件
//
// // 设置button 位置于大小
// jbAdd.setBounds(320, 200, 70, 25);//
// jbQuery.setBounds(320, 60, 70, 25);//
// // jbUpdate.setBounds(320, 200, 75, 25);//
// jf.add(jbAdd);// 向容器增加JLabel组件
// jf.add(jbQuery);// 向容器增加JButton组件
// jbQuery.setVisible(false);
// // jf.add(jbUpdate);// 向容器增加JButton组件
//
// // 查询
// jf.add(byGlassId);
// jf.add(byBatchId);
// jf.add(byLotId);
// ButtonGroup bg = new ButtonGroup();
// bg.add(this.byGlassId);
// bg.add(this.byBatchId);
// bg.add(this.byLotId);
// // byGlassId.setSelected(true);
//
// jf.add(byGlassIdJLabel);
// jf.add(byBatchIdJLabel);
// jf.add(byLotIdJLabel);
// // by glass id
// byGlassId.setBounds(5, 30, 16, 15);//
// byGlassIdJLabel.setBounds(25, 30, 100, 15);//
//
// // by batchid
// byBatchId.setBounds(100, 30, 16, 15);//
// byBatchIdJLabel.setBounds(120, 30, 100, 15);//
//
// // by lot id
// byLotId.setBounds(200, 30, 16, 15);//
// byLotIdJLabel.setBounds(220, 30, 100, 15);//
// // byGlassId.
// byGlassId.setVisible(false);
// byGlassIdJLabel.setVisible(false);
// byBatchId.setVisible(false);
// byBatchIdJLabel.setVisible(false);
// byLotId.setVisible(false);
// byLotIdJLabel.setVisible(false);
//
//
// //add by glassId bathchId LotId event
// byGlassId.addItemListener(this);
// byBatchId.addItemListener(this);
// byLotId.addItemListener(this);
initJFrameAdd();
addJFrameLayout();
}
public static void main(String[] args) {
new GlassJframeTest();
}
public void itemStateChanged(ItemEvent evt) {
// TODO Auto-generated method stub
if (evt.getStateChange() == evt.SELECTED) {
if (evt.getSource() == byGlassId) {
jlGrassId.setBounds(5, 60, 100, 15);//
tfGrassId.setBounds(60, 60, 100, 20);//
jlGrassId.setVisible(true);
tfGrassId.setVisible(true);
jlBatch.setVisible(false);
tfGrade.setVisible(false);
jlLotId.setVisible(false);
tfLotId.setVisible(false);
// jbQuery.setVisible(true);
}
if (evt.getSource() == byBatchId) {
jlBatch.setBounds(5, 60, 100, 15);//
tfBatch.setBounds(60, 60, 100, 20);//
jlBatch.setVisible(true);
tfBatch.setVisible(true);
jlGrassId.setVisible(false);
tfGrassId.setVisible(false);
jlLotId.setVisible(false);
tfLotId.setVisible(false);
// jbQuery.setVisible(true);
}
if (evt.getSource() == byLotId) {
jlLotId.setBounds(5, 60, 100, 15);//
tfLotId.setBounds(60, 60, 100, 20);//
jlLotId.setVisible(true);
tfLotId.setVisible(true);
jlGrassId.setVisible(false);
tfGrassId.setVisible(false);
jlBatch.setVisible(false);
tfBatch.setVisible(false);
}
}
System.out.println("aa");
}
public void queryJFrameLayout() {
byGlassId.setVisible(true);
byGlassIdJLabel.setVisible(true);
byBatchId.setVisible(true);
byBatchIdJLabel.setVisible(true);
byLotId.setVisible(true);
byLotIdJLabel.setVisible(true);
// byBatchId.setSelected(true);
byGlassIdJLabel.setVisible(true);
jlGrassId.setVisible(false);
tfGrassId.setVisible(false);
System.out.println("mousePressed");
jlGrade.setVisible(false);
jlBatch.setVisible(false);
jlBatchSize.setVisible(false);
jlLotId.setVisible(false);
tfGrade.setVisible(false);
jcb.setVisible(false);
tfBatch.setVisible(false);
tfBatchSize.setVisible(false);
tfLotId.setVisible(false);
tfLotId.setVisible(false);
jbAdd.setVisible(false);
jbQuery.setVisible(true);
// jTable.setVisible(true);
// reset jlGrassId位置
jlGrassId.setBounds(5, 60, 100, 15);//
tfGrassId.setBounds(60, 60, 100, 20);//
jlGrassId.setVisible(true);
tfGrassId.setVisible(true);
byGlassId.setSelected(true);
}
public void initJFrameAdd() {
// 设置glass_id textbox 位置于大小
// jlGrassId.setBounds(5, 30, 100, 15);//
// tfGrassId.setBounds(70, 30, 100, 20);//
jf.add(jlGrassId);// 向容器增加JLabel组件
jf.add(tfGrassId);// 向容器增加JButton组件
// 设置grade textbox 位置于大小
// jlGrade.setBounds(200, 30, 100, 15);//
// jcb.setBounds(250, 30, 100, 20);//
jf.add(jlGrade);// 向容器增加JLabel组件
jf.add(jcb);// 向容器增加JButton组件
// // 设置batch textbox 位置于大小
// jlBatch.setBounds(5, 70, 100, 15);//
// tfBatch.setBounds(70, 70, 100, 20);//
jf.add(jlBatch);// 向容器增加JLabel组件
jf.add(tfBatch);// 向容器增加JButton组件
// 设置batchsize textbox 位置于大小
jlBatchSize.setBounds(200, 70, 100, 15);//
tfBatchSize.setBounds(270, 70, 100, 20);//
jf.add(jlBatchSize);// 向容器增加JLabel组件
jf.add(tfBatchSize);// 向容器增加JButton组件
// // 设置lotId textbox 位置于大小
// jlLotId.setBounds(5, 110, 100, 15);//
// tfLotId.setBounds(70, 110, 100, 20);//
jf.add(jlLotId);// 向容器增加JLabel组件
jf.add(tfLotId);// 向容器增加JButton组件
// // 设置button 位置于大小
// jbAdd.setBounds(320, 200, 70, 25);//
// jbQuery.setBounds(320, 60, 70, 25);//
// jbUpdate.setBounds(320, 200, 75, 25);//
jf.add(jbAdd);// 向容器增加JLabel组件
jf.add(jbQuery);// 向容器增加JButton组件
jbQuery.setVisible(false);
// jf.add(jbUpdate);// 向容器增加JButton组件
// 查询
jf.add(byGlassId);
jf.add(byBatchId);
jf.add(byLotId);
ButtonGroup bg = new ButtonGroup();
bg.add(this.byGlassId);
bg.add(this.byBatchId);
bg.add(this.byLotId);
// byGlassId.setSelected(true);
jf.add(byGlassIdJLabel);
jf.add(byBatchIdJLabel);
jf.add(byLotIdJLabel);
// by glass id
byGlassId.setBounds(5, 30, 16, 15);//
byGlassIdJLabel.setBounds(25, 30, 100, 15);//
// by batchid
byBatchId.setBounds(100, 30, 16, 15);//
byBatchIdJLabel.setBounds(120, 30, 100, 15);//
// by lot id
byLotId.setBounds(200, 30, 16, 15);//
byLotIdJLabel.setBounds(220, 30, 100, 15);//
// byGlassId.
byGlassId.setVisible(false);
byGlassIdJLabel.setVisible(false);
byBatchId.setVisible(false);
byBatchIdJLabel.setVisible(false);
byLotId.setVisible(false);
byLotIdJLabel.setVisible(false);
// add by glassId bathchId LotId event
byGlassId.addItemListener(this);
byBatchId.addItemListener(this);
byLotId.addItemListener(this);
jf.setSize(500, 350);// 设置窗体大小
jf.setResizable(false);// 设置不能最大化
jf.setBackground(Color.RED);// 设置窗体背景颜色
jf.setLocation(300, 200);// 设置窗体显示位置
// jf.pack();
jf.setVisible(true);// 设置窗体是可见的
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addJFrameLayout() {
System.out.println("mousePressed");
//
// jlGrassId.setBounds(5, 30, 100, 15);//
// tfGrassId.setBounds(70, 30, 100, 20);//
//
// jlGrassId.setVisible(true);
//
// // label
// jlGrade.setVisible(true);
// jlBatch.setVisible(true);
// jlBatchSize.setVisible(true);
// jlLotId.setVisible(true);
//
// // text
// tfGrassId.setVisible(true);
// tfGrade.setVisible(true);
// jcb.setVisible(true);
// tfBatch.setVisible(true);
// tfBatchSize.setVisible(true);
// tfLotId.setVisible(true);
// tfLotId.setVisible(true);
// jbAdd.setVisible(true);
// jbQuery.setVisible(false);
// // jTable.setVisible(false);
//
// // by
// byGlassId.setVisible(false);
// byGlassIdJLabel.setVisible(false);
// byBatchId.setVisible(false);
// byBatchIdJLabel.setVisible(false);
// byLotId.setVisible(false);
// byLotIdJLabel.setVisible(false);
//laybel glassid 位置于大小
jlGrassId.setBounds(5, 30, 100, 15);//
tfGrassId.setBounds(70, 30, 100, 20);//
//grade 位置于大小
jlGrade.setBounds(200, 30, 100, 15);//
jcb.setBounds(250, 30, 100, 20);//
// 设置batch textbox 位置于大小
jlBatch.setBounds(5, 70, 100, 15);//
tfBatch.setBounds(70, 70, 100, 20);//
// 设置lotId textbox 位置于大小
jlLotId.setBounds(5, 110, 100, 15);//
tfLotId.setBounds(70, 110, 100, 20);//
// 设置button 位置于大小
jbAdd.setBounds(320, 200, 70, 25);//
jbQuery.setBounds(320, 60, 70, 25);//
jlGrassId.setVisible(true);
tfGrassId.setVisible(true);
jlGrade.setVisible(true);
jcb.setVisible(true);
jlBatch.setVisible(true);
tfBatch.setVisible(true);
jlLotId.setVisible(true);
tfLotId.setVisible(true);
jbAdd.setVisible(true);
//byGlassId.setVisible(true);
// // by glass id
// byGlassId.setBounds(5, 30, 16, 15);//
// byGlassIdJLabel.setBounds(25, 30, 100, 15);//
//
// // by batchid
// byBatchId.setBounds(100, 30, 16, 15);//
// byBatchIdJLabel.setBounds(120, 30, 100, 15);//
//
// // by lot id
// byLotId.setBounds(200, 30, 16, 15);//
// byLotIdJLabel.setBounds(220, 30, 100, 15);//
// byGlassId.
byGlassId.setVisible(false);
byGlassIdJLabel.setVisible(false);
byBatchId.setVisible(false);
byBatchIdJLabel.setVisible(false);
byLotId.setVisible(false);
byLotIdJLabel.setVisible(false);
jbQuery.setVisible(false);
}
}