1、窗口风格 、
try { for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (Exception e) { JOptionPane.showMessageDialog(null, "未找到新皮肤, 请升级 JDK 到 6.0 update 10"); }
放到显示窗口前面即还没显示窗口的时候就应该应用他比如在 main()方法里面先加上这 段代码再 new frame() frame()是类的构造方法里面是实现组件和窗口显示的功能
2、组件随意拖动 、
classDragPicListener extends MouseInputAdapter { Point point = new Point(0, 0); /** * 当鼠标拖动时触发该事件。记录下鼠标按下(开始拖动)的位置。 */ public void mouseDragged(MouseEvent e) { // 转换坐标
系统 Point newPoint = SwingUtilities.convertPoint(label, e .getPoint(), label.getParent()); // 设置标签的新位置 label.setLocation(label.getX()
+ (newPoint.x - point.x), label.getY() + (newPoint.y - point.y)); // 更改坐标点 point = newPoint; } /** * 当鼠标按下时触发该事件。记录下鼠标按下(开始拖动)的位置。 */ public void mousePressed(MouseEvent e) { // 得到当前坐标点 point = SwingUtilities.convertPoint(label, e.getPoint(), label.getParent()); } }
3、居中 、
Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包 Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸 intscreenWidth = screenSize.width/2; // 获取屏幕的宽 intscreenHeight = screenSize.height/2; // 获取屏幕的高 int height = this.getHeight(); int width = this.getWidth(); setLocation((screenWidth-width)/2, (screenHeight-height)/2);
4、设置窗体背景图片 、
background = new ImageIcon("body1r.gif");//背景图片 JLabel label = new JLabel(background);//把背景图片显示在一个标签里面 label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());//把标签的大小位 置设置为图片刚好填充整个面板 imagePanel = (JPanel)this.getContentPane();//把内容窗格转化为 JPanel,否则不能用方法 setOpaque()来使内容窗格透明 imagePanel.setOpaque(false);
imagePanel.setLayout(new FlowLayout());//内容窗格默认的布局管理器为 BorderLayout imagePanel.add(new JButton("测试按钮")); this.getLayeredPane().setLayout(null); this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));//把背景图片添加到分层窗 格的最底层作为背景
5、组件规则移动 、
if(e.getSource()==button1) { Rectangle rectangle=button3.getBounds(); int x=(int)rectangle.getX(); int y=(int)rectangle.getY(); button3.setLocation(x-5, y); }
点击 button1
button3 左移五格
6、文件路径名字问题(I/O 流方面) 、文件路径名字
问题( 流方面)
1、String file=chooser.getSelectedFile().getAbsolutePath();被选择的文件的路径(有文件名) 2、String filePath=file.toString();返回此抽象路径名的路径名字符串 3、 String filename=filePath.su
bstring(filePath.lastIndexOf("\\")+1);sbustring 返回 filePath 的子字 符串 这个子字符串返回\在 filePath 中最右边出现处的索引例子 E:\ABC\AC.jar 应用上面的 3 之后 filename 就是 AC.jar 如果不加 1 就是\AC.jar