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() );