1.Date類:Date jhsjDate = new Date();
System.out.println(jhsjDate);//Wed Aug 10 10:42:02 CST 2011//它相當於jhsjDate.toString()
System.out.println(jhsjDate.toLocaleString());//2011-8-10 10:43:55
System.out.println(jhsjDate.getTime());//1312944122612//得到一個長整型long
Date date = new Date(jhsjDate.getTime());//對於這來說,基本無用
System.out.println(date.toLocaleString());//2011-8-10 10:42:02
String dateStr =date.toLocaleString().substring(0, 9);
System.out.println(dateStr);//2011-8-10
String dateStr1 = dateStr.toString().replace("-", "");
System.out.println(dateStr1);//2011810
2.模糊查詢包含4567
Where id like ‘%4567%’ 這是在程序中的正確寫法
Where id like ‘*4567*’ 這是在access數據庫中的正確寫法
3.在程序中查詢數據庫中的第一列寫法:set.getString(1);是1而不是0,而集合當中卻相反。
4.DefaultTableModel dtm = new DefaultTableModel();
table.getRowCount()這是顯示行的總行數
dtm.removeRow(i);這是第i-1行,行跟列都是從0開始
TableModel是根接口,AbStractTableModel是其實現的抽象類,DefaultTableModel是完全實現類
TableModel
接口指定了 JTable
用於詢問表格式數據模型的方法
TableModel myData = new MyTableModel();
JTable table = new JTable(myData);
5.TableCellEditor接口 其完全實現類是:DefaultCellEditor
此接口定義了要成爲組件(如JListBox
、JComboBox
、JTree
或JTable
)的值編輯器的任意對象需要實現的方法,table.getCellEditor()表格中此方法可以得到此接口
cell.StopCellEditing();停止單元格編輯
6.new Thread(new Runnable(){
Public void run(){
}
}
);定義了多線程
7.在model模型中聲明private Set<TbRuKuDetail> rukudetails = new HashSet<TbRuKuDetail>();
類似表連接。
8. // 設置組件位置並添加到容器中
private void setupComponet(JComponent component,int gridx, int gridy,
int gridwidth, int ipadx,boolean fill) {
final GridBagConstraints gridBagConstrains =new GridBagConstraints();
gridBagConstrains.gridx = gridx;
gridBagConstrains.gridy = gridy;
if (gridwidth > 1)
gridBagConstrains.gridwidth = gridwidth;
if (ipadx > 0)
gridBagConstrains.ipadx = ipadx;
gridBagConstrains.insets = new Insets(5, 1, 3, 1);
if (fill)
gridBagConstrains.fill = GridBagConstraints.HORIZONTAL;
getContentPane().add(component, gridBagConstrains);
9.Calendar類的介紹
Calendar c = Calendar.getInstance();
System.out.println(Calendar.YEAR);//顯示1
System.out.println(Calendar.MONTH);//2
System.out.println(Calendar.DAY_OF_MONTH);//5
System.out.println(c.get(Calendar.YEAR));//2011
System.out.println(c.get(Calendar.MONTH)+1);//8,月份一定要加1
System.out.println(c.get(Calendar.DAY_OF_MONTH));//10號
System.out.println(c.get(Calendar.DAY_OF_WEEK)-1);//3,星期一定要減1
10.Sql語句:排序語句 select sum(count) from table where id = ‘id’ order by name asc 或者是desc
11.Derby.jar數據庫javaDB驅動的程序在C:\Program Files\Sun\JavaDB\lib目錄下
12.創建表格
JTable 方法setAutoResizeMode(JTable.AUTO_RESIZE_OFF) //不自動調整列的寬度;使用滾動條
DefaultTableModel table.getModel()返回這個類,然後setColumnIdentifiers(headers);
ScrollPane
GridBagConstraints grid2 = new GridBagConstraints();
grid2.weighty=1;// 指定如何分佈額外的垂直空間。weightx
grid2.gridwidth=5; gridheight
grid2.fill= GridBagConstraints.BOTH;VERTICAL,HORIZONTAL
grid2.insets = new Insets(5,5,5,5);
grid2.gridx=0;
grid2.gridy=2;
grid2.ipax=0; 組件的最小寬度添加多大的空間 ipdy是高度
this.getContentPane().add(scrollPane,grid2);
另外GridBagConstraints還有其它靜態屬性gridBagConstraints_11.anchor = GridBagConstraints.WEST;
EAST ,NORTH,SOUTH,CENTER
13字符串格式化
String.format(“%03d”,str);將字符串格式化成三位數
14.JComboBox的setEditable(true)可以將下拉列表框設置文本框一樣,可以編輯的
Combo.setModel(new DefaultComboBoxModel(
new String[]{"升序排列", "降序排列"}));
gys.removeAllItems();移除所有項
15.在access中,一樣可以創建視圖表,點擊查詢,設計,就可以了
16.if(items.contains(item)){}這個方法很常用的,在判斷JComboBox時
17. TableColumnModel columnModel = table.getColumnModel();// 返回列模型
TableColumn tableColumn = columnModel.getColumn(0);
final DefaultCellEditor editor = new DefaultCellEditor(sp);// 構造商品組合框的編輯器
editor.setClickCountToStart(2);//雙擊兩下才開始
tableColumn.setCellEditor(editor);//將sp、tableColumn、editor連在一起
18. if(!(sp.getSelectedItem() instanceof TbSpInfo)){
return;
}這條代碼很重要,有時沒有它會出錯
19. table.getSelectedRow();// 選擇當前行
20、int rowCount =dftm.getRowCount();//清空所有行
for(int i=0;i<rowCount;i++){
dftm.removeRow(0);
}
for(Iterator iter = list.iterator();iter.hasNext();){//用vector添加每行數據到表格
Vector vector = new Vector();
List view =(List) iter.next();
vector.addAll(view);//添加視圖表格中一行的每個元素到vector
dftm.addRow(vector);//將vector添加到行中
}
21. this.setIconifiable(true);//最小化
this.setClosable(true);//關閉
this.setTitle("客戶信息查詢");
this.getContentPane().setLayout(new GridBagLayout());
new BoxLayout(jpanel,BoxLayout.X_XAIS)//按順序編排
this.setMaximizable(true);//最大化
this.setBounds(50, 50, 650, 380);//定位和定大小
22. // 按鈕事件的監聽
Action action = new openFrameAction(fName, cName, icon);自定義類,Action是接口
JButton button = new JButton(action);
button.setHideActionText(true);// 隱藏圖標按鈕旁邊的文本
button.setBorderPainted(false);// 取消圖標細小的邊框
button.setFocusPainted(false);// 取消點擊時出現的邊框焦點
button.setContentAreaFilled(false);// 取消背景內容的顏色
button.setMargin(new Insets(0, 0, 0, 0));// 設置每個圖標之間的間距,從上,左,下,右的順序出發。
button.setRolloverIcon(icon_roll);// 圖標被選中調用
button.setPressedIcon(icon_down);// 圖標被單擊調用
23. Class fClass = Class.forName("com.meilun.internalFrame."+ cName);//反射一個類回來
Constructor constructor = fClass.getConstructor(null);//反射這個類的構造方法
jf = (JInternalFrame) constructor.newInstance(null);//實例化這個構造方法
ifs.put(cName, jf);
//得到一個子窗體
private JInternalFrame getIFrame(String cName) {
JInternalFrame jf = new JInternalFrame();
if (!ifs.containsKey(cName)) {//這判斷必須的
try {
Class fClass = Class.forName("com.meilun.internalFrame."+ cName);
Constructor constructor = fClass.getConstructor(null);
jf = (JInternalFrame) constructor.newInstance(null);
ifs.put(cName, jf);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
jf = ifs.get(cName);
}
return jf;
}
@Override
public void actionPerformed(ActionEvent e) {
//調用子窗體
JInternalFrame jf = this.getIFrame(cName);
// 子窗體關閉後
jf.addInternalFrameListener(new InternalFrameAdapter() {
public void internalFrameClosed(InternalFrameEvent e) {
ifs.remove(cName);//移除在hashmap集合中的名字
}
});
//將jf添加到desktopPane中
if (jf.getDesktopPane() == null) {
desktopPane.add(jf);
jf.setVisible(true);
}
try {
jf.setSelected(true);
} catch (PropertyVetoException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}// 內部窗口選取時
}
24設置系統外觀風格
static {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
25.
// 添加背景圖片
BackPanel bp = new BackPanel();
desktopPane.add(bp);
desktopPane.setLayer(bp, -1);//這條代碼是關鍵,必須是-1,其它數字都不行,這是我自己經過多次測試發現的。
frame.getContentPane().add(desktopPane,BorderLayout.CENTER);
26. for(int i=1981,j=0;i<=date.get(Calendar.YEAR)+1;i++,j++){//for語句可以兩個變量,這是從1981到2012年的選擇
year.addItem(i);
if(i== date.get(Calendar.YEAR)){
year.setSelectedIndex(j);
}
}
27.在access中沒有convert轉換函數和substring函數,但在sql2005h中有,access的截取函數不是substring,而是mid(str,start,length),注意start是從1開始,而不是0
28.字符串中包含雙引號,System.out.println("welcome\"hacker\"");輸出:welcome"hacker"
29. sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
jhsj.setText(sdf.format(jhsjDate));
SimpleDateFormat類是專門格式化日期時間的
2011/8/14
30. String key="-0123456789"+(char)8;
if(key.indexOf(e.getKeyChar())<0){ e.consume();}
它的作用是使你的鍵盤輸入只能爲數字,
首先第一行的key是定義了鍵盤一共能輸入哪些按鍵.前面的-~9應該很好理解,(char)8就是ASCII碼的第8個char.也就是BS(退格鍵)
然後,第二行的e.getKeyChar()就是獲得當前按鍵的對應的ASCII碼的char形式.然後通過key.indexOf()方法來檢查該按鍵是否在key列表裏,如果不在的話,返回的index是-1,則-1 < 0爲true,執行e.consume();//這是相反的操作
31. pzs = new JTextField("0");//此處必須是加引號的0,否者會空指針錯誤
32.lable.setFont(new Font(null,Font.BOLD,14));這個可以設置label字體的大小,同樣可以設置其它組件的文字屬性。
32.獲得系統托盤:
SystemTray systray = SystemTray.getSystemTray();
if(systray.isSupported()){
System.out.println(systray);
}
ImageIcon icon = new ImageIcon("res\\ActionIcon\\操作員管理.png");
Image img = icon.getImage();
TrayIcon trayIcon = new TrayIcon(img,"這是操作員管理");
PopupMenu pop = new PopupMenu();
MenuItem item1 = new MenuItem("顯示窗體");
MenuItem item2 = new MenuItem("退出");
pop.add(item1);
pop.add(item2);
trayIcon.setPopupMenu(pop);
try {
systray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
33.
Final JLabel title = new JLabel();
title.setForeground(Color.RED);//設置字體顏色
34.Dao中設置ResultSet rs 使用後,必須rs.close()這樣關閉它,這是種規範。
35.icon = new ImageIcon("res/welcome.jpg");字符串也可以是res\\welcome.jpg