swing 一句话经验

来源:互联网 发布:淘宝被限制交易怎么办 编辑:程序博客网 时间:2024/05/21 16:54
1.JFrame对象一般可以直接向里面添加数据,但是在设置背景颜色时得用:Container f = new JFrame(“test”).getContentPane():
2.pack()是设置窗体大小自适应其内组件,setSize()为设置其显示的大小。
3.默认关闭窗体JFrame时,是隐藏。要想真退出。请加入setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JFrame.EXIT_ON_CLOSE是一整形常量,值为3。
4.加载GIF图片:
MediaTracker tracker = new MediaTracker(this);
Image img = new ImageIcon (getClass().getResource(“test.gif”)).getImage():
Tracker.addImage(img,0):
try{
        tracker.waitForAll();
    }catch(Exception e)
{}
此后就可以对完整的gif进行操作了。
5.光标移动到组件上给出提示:setToolTipText(”this is tip”);
6.JScrollPane可以通过setVerticalScrollBarPolicy(int policy)
和setHorizontalScrollBarPolicy(int policy)来选择是否显示滚动条。

7.弹出消息框:JOptionPane.showMessageDialog(btn,new String (“test”));
  8. 文本输入框右对齐:SetHorizontalAlignment(JTextField.RIGHT);
9.  读取JAR包内资源,应该使用:getClass().getResource();
10. 容器布局为空后——setLayout(null),其内组件应setBounds();
11.内存回收:if(Runtime.getRuntime().freeMemory()<Runtime.getRuntime().totalMemory()/3)
{
    System.gc();
}
12.  JFrame 窗口不可最大化:setResizable(false);
13. 应用程序和applet小程序共存:直接把applet往jframe里面加就成应用程序。
14. 让窗口居中(屏幕)显示:setLocationRelativeTo(null):
或者用:
Toolkit kit = Toolkit.getDefaultToolkit():
Dimension screenSize = kit.getScreenSize();
f.setLocation((screenSize.Width-f.getWidth())/2, (screenSize.Height-f.getHeight())/2);

15. 选择皮肤感官:
try{
UIManager.setLookAndFeel ( "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
            SwingUtilities.updateComponentTreeUI(f);
}

16. 密码框可设setEchoChar(‘#’);或者密码时 pwd = new String(password.getPassword());

17. 用JlayeredPane添加组件时候可以设置深度。

18. 区别单双击事件:
if(e.getClickCount ==1)
{}else if(e.getClickCount ==2){}

19. JAVA2D 中设置虚线:
float [] dash = {2.0f,4.0f};
g2.setStroke(
new BasicStroke(2.0f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL,2.0f,dash,2.0f));

20. JAVA2D 中设置字体笔画大小:
Font cf = g.getFont();
Font newf = new Font(cf.getName(),cf.getStyle(),cf.getSize()+20);
g.setFont(newf);
 
原创粉丝点击