1、FRAME怎麼實現全螢幕
首先要知道螢幕視窗的大小,然後將視窗的位置和大小調整到螢幕視窗的相同大小就ok了。兩種方法可實現。
Ⅰ:
程式碼如下:
JFrame frame = new JFrame("Test Full Screen JFrame");
frame .setUndecorated(true);
frame .getGraphicsConfiguration().getDevice().setFullScreenWindow(frame );
frame .setVisible(true);
Ⅱ:
JFrame frame = new JFrame("Test Full Screen JFrame");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle bounds = new Rectangle(screenSize);
frame .setBounds(bounds);
frame .setVisible(true);
2、swing之frame在運行時自動視窗最大化
import javax.swing.*;
Frame frame = new Frame();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
3、啟動顯示畫面置中
AstCenterBootstrap astCenter = new AstCenterBootstrap("AstCenter系統");
astCenter.pack();
Dimension local_size = new Dimension(300, 200);
astCenter.setSize(local_size);
astCenter.setLocationRelativeTo(null);
astCenter.setVisible(true);
4、swing 顯示模態視窗
public class SubDialog extends JDialog {
public SubDialog(JFrame f)
{
super(f,true);
this.setSize(200,300);
this.setTitle("dialog");
this.setLocationRelativeTo(null);
}
public SubDialog()
{
}
}
5、子視窗向父視窗傳遞值、從控制項取值
方法一:
父子視窗之間通過一個資料對象聯絡,該對象屬於父視窗,子視窗初始化時儲存該對象引用
在子視窗的事件處理對象的函數裡,先重子視窗控制項取值,再賦予該資料對象的參數
jDialog.fatherData.lostRate = Integer.valueOf(jDialog.jTextField.getText());
6、視窗控制項排列
7、彈出提示對話方塊
JOptionPane
8、枚舉中STRING的轉換, ENUM.VALUEOF
public enum StrList {
APPLE,
PEAR,
ORANGE
}
switch(StrList.valueOf(cmd))
{
case APPLE:
break;
case PEAR:
break;
default:
break;
}
9、設定關閉視窗,進程即結束
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
10、菜單快速鍵設定及事件觸發
newItem.setAccelerator(KeyStroke.getKeyStroke('N',CTRL_DOWN_MASK));
11、開啟檔案及過濾器
開啟檔案類JFileChooser
過濾器需要自己編寫,繼承FileFilter
package WindowsPak;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.*;
public final class CapFileFilter extends FileFilter {
private String extention;
private String desc;
public CapFileFilter( String extention, String description )
{
super();
this.extention = extention;
this.desc = description;
}
public boolean accept( File f )
{
if( null != f )
{
if ( f.isDirectory() )
return true;
String ext = getExtention( f );
if ( null != f && this.extention.equals(ext) )
{
return true;
}
}
return false;
}
public String getDescription( )
{
return this.desc;
}
private String getExtention( File f )
{
if ( null != f )
{
String file_name = f.getName();
int i = file_name.lastIndexOf( '.' );
return file_name.substring( i + 1 ).toLowerCase();
}
return null;
}
}
12、JTABLE資料重新整理
方法1:
主要通過設定MODEL來重新整理顯示資料
//模式申明
TableModel dataModel = new AbstractTableModel() {
// These methods always need to be implemented.
public int getColumnCount() { return column_names.length; }
public int getRowCount() { return data.length;}
public Object getValueAt(int row, int col) {return data[row][col];}
// The default implementations of these methods in
// AbstractTableModel would work, but we can refine them.
public String getColumnName(int column) {return column_names[column];}
public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
public boolean isCellEditable(int row, int col) {return true;}
public void setValueAt(Object aValue, int row, int column) {
System.out.println("Setting value to: " + aValue);
data[row][column] = aValue;
}
}; /*
//給表設定模式
//申請表
table_view = new JTable( dataModel );
table_view.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
*/
packet_table.setModel( dataModel );
//更新JTABLE
jtable.updateUI();
方法2:
重新整理表格式資料時使用如下:
modol.getDataVector().removeAllElements();
modol.fireTableDataChanged();
14、GridBagLayout布局3格兩列
GridBagLayout gridbag=new GridBagLayout();
container.setLayout( gridbag );
//this.setLayout( g_layout );
//a. 表單表部分
tab_pane = addPanel();
tab_pane.add( "Packet List", addJScrollPane() );
//設定表單表tab位置
GridBagConstraints gridBagConstraints_a = new java.awt.GridBagConstraints( 0, 0, 3, 1, 1, 1, GridBagConstraints.BASELINE, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);
//GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints( tab_pane, gridBagConstraints_a );
container.add( tab_pane );
15、JTREE節點選中事件
//添加滑鼠左鍵選中事件
rtp_tree.addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int selRow = rtp_tree.getRowForLocation(e.getX(), e.getY()); // 返回節點所在的行,-1表示滑鼠定位不在顯示的單元邊界內
TreePath selPath = rtp_tree.getPathForLocation(e.getX(), e.getY()); // 返回指定節點的樹路徑
boolean condition = true ;
condition = condition && (selRow != - 1 ); // 如果選中
// condition = condition && (e.getButton() == 1); // 左鍵 e.getButton() == 1 右鍵 e.getButton() == 3
condition = condition && (e.getClickCount() == 1 ); // 單擊
// 如果是左鍵點擊
if (condition == true && (e.getButton() == 1 )) {
Object[] nodes = selPath.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes[nodes.length - 1 ];
JOptionPane.showMessageDialog(null , node.toString());
}
}
});
16、提示對話方塊用JOptionPane類
17、編輯樹JTREE,使用DefaultCellEditor
public class CapTreeCellEditorListener extends DefaultCellEditor {
public JTree edit_tree = null;
public CapTreeCellEditorListener( JTextField textField )
{
super( textField );
this.setClickCountToStart(2);
}
public boolean stopCellEditing()
{
TreePath tree_path = edit_tree.getSelectionPath();
String new_value = getCellEditorValue().toString();
edit_tree.getModel().valueForPathChanged( tree_path, new_value );
System.out.println( "INFO:" + new_value );
//JOptionPane.showMessageDialog(null , new_value );
fireEditingCanceled();
return true;
}
public void cancelCellEditing() {
TreePath tree_path = edit_tree.getSelectionPath();
String new_value = getCellEditorValue().toString();
edit_tree.getModel().valueForPathChanged( tree_path, new_value );
System.out.println( "INFO:" + new_value );
fireEditingCanceled();
}
}
//
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) rtp_tree.getCellRenderer();
JTextField textField = new JTextField();
CapTreeCellEditorListener editor = new CapTreeCellEditorListener( textField );
editor.edit_tree = rtp_tree;
rtp_tree.setCellEditor( editor );
18. JTABLE選定行事件
public void valueChanged(ListSelectionEvent e1){
if ( 0 < dataTable.getSelectedRow() )
{
int index = (Integer) dataTable.getValueAt( dataTable.getSelectedRow(), 0 ); //行+列
mFrame.update_tree( index );
}
}
ListSelectionModel selectMode = packet_table.getSelectionModel();
CapTableListSelectionListener capListListener = new CapTableListSelectionListener( this, packet_table );
selectMode.addListSelectionListener( capListListener );
selectMode.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
19. JTREE預設全部展開
//展開樹
private void expandAll(JTree tree, TreePath parent, boolean expand) {
TreeNode node = (TreeNode) parent.getLastPathComponent();
if (node.getChildCount() > 0) {
for (Enumeration e = node.children(); e.hasMoreElements();) {
TreeNode n = (TreeNode) e.nextElement();
TreePath path = parent.pathByAddingChild(n);
expandAll(tree, path, expand);
}
}
if (expand) {
tree.expandPath(parent);
} else {
tree.collapsePath(parent);
}
}
20. JTABLE設定一次只選一行
selectMode.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
21. 更新樹的節點值
((DefaultTreeModel)rtp_tree.getModel()).valueForPathChanged( dst_path, "Destination Mac:" + selected_packet.get_dst_mac() );