1. JFrame對(duì)象一般可以直接向里面添加數(shù)據(jù),但是在設(shè)置背景顏色時(shí)得用:Container f = new JFrame(“test”).getContentPane();
2. pack()是設(shè)置窗體大小自適應(yīng)其內(nèi)組件,setSize()為設(shè)置其顯示的大小。
3. 默認(rèn)關(guān)閉窗體JFrame時(shí),是隱藏。要想真退出。請(qǐng)加入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)
{}
此后就可以對(duì)完整的gif進(jìn)行操作了。
5. 光標(biāo)移動(dòng)到組件上給出提示:setToolTipText(”this is tip”);
6. JScrollPane可以通過setVerticalScrollBarPolicy(int policy)
和setHorizontalScrollBarPolicy(int policy)來選擇是否顯示滾動(dòng)條。
7.彈出消息框:JOptionPane.showMessageDialog(btn,new String (“test”));
彈出錯(cuò)誤提示:JOptionPane.showMessageDialog(null,"錯(cuò)誤信息","錯(cuò)誤框標(biāo)題",JOptionPane.ERROR_MESSAGE);
8. 文本輸入框右對(duì)齊:SetHorizontalAlignment(JTextField.RIGHT);
9. 讀取JAR包內(nèi)資源,應(yīng)該使用:getClass().getResource();
10. 容器布局為空后——setLayout(null),其內(nèi)組件應(yīng)setBounds();
11.內(nèi)存回收:if(Runtime.getRuntime().freeMemory()<Runtime.getRuntime().totalMemory()/3)
{
System.gc();
}
12. JFrame 窗口不可最大化:setResizable(false);
13. 應(yīng)用程序和applet小程序共存:直接把a(bǔ)pplet往jframe里面加就成應(yīng)用程序。
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. 密碼框可設(shè)setEchoChar(‘#’);或者密碼時(shí) pwd = new String(password.getPassword());
17. 用JlayeredPane添加組件時(shí)候可以設(shè)置深度。
18. 區(qū)別單雙擊事件:
if(e.getClickCount ==1)
{}else if(e.getClickCount ==2){}
19. JAVA2D 中設(shè)置虛線:
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 中設(shè)置字體筆畫大小:
Font cf = g.getFont();
Font newf = new Font(cf.getName(),cf.getStyle(),cf.getSize()+20);
g.setFont(newf);
21.讓樹無圖標(biāo)標(biāo)簽
((DefaultTreeCellRenderer)tree.getCellRenderer()).setOpenIcon(null);
((DefaultTreeCellRenderer)tree.getCellRenderer()).setClosedIcon(null);
((DefaultTreeCellRenderer)tree.getCellRenderer()).setDisabledIcon(null);
((DefaultTreeCellRenderer)tree.getCellRenderer()).setLeafIcon(null);
22.豎著顯示滑塊
new JSlider(SwingConstants.VERTICAL,min,max,initialValue);
23.通過標(biāo)尺顯示滑塊
slider.setMajorTickSpacing(20);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
24.讓滑塊自動(dòng)對(duì)齊到標(biāo)尺
slider.setSnapToTicks(true);
25.滑塊事件
addChangeListener
ChangeListener -> stateChanged(ChangeEvent event)
26.滑塊標(biāo)尺特殊化
HashtMap map = new HashMap();
map.put(new Integer(0),new JLabel("A"));
slider.setLabel(map);
27.多選單選按鈕
JCheckBox 和 JRadioButton 其中單選放入ButtonGroup對(duì)象中。
判斷是否選中:isSelected()
獲取標(biāo)簽用:getText();
事件:ItemListener
方法:itemStateChanged(ItemEvent event)
28.讓樹一開始就全部展開
expandAll(tree,new TreePath(tree.getModel().getRoot()));
其中有:
public void expandAll (JTree tree,TreePath path)
{
tree.expandPath(path);
Obect parent = path.getLastPathComponent();
TreeModel model = tree.getModel()
if(model ==null)return;
if(model.isLeaf(parent)) return;
int size = model.getChildCount(parent);
for(int i = 0 ;i<size;i++)
{
Object child = model.getChild(parent,i);
expandAll(tree,path.pathByAddingChild(child));
}
}
29.樹節(jié)點(diǎn)匹配:
TreePath path = tree.getNextMatch(str,0,Positon.Bias.Forward);
if(path !=null)
{
tree.setSelectionPath(path);
}
30.獲取樹節(jié)點(diǎn)
TreeSelectionListener->valueChanged(TreeSelectionEvent e)
TreePath path = e.getPath();
String info = path.getLastPathComponent().toString();
而這里獲取到是最后那個(gè)選擇節(jié)點(diǎn),如成都。
JTree tree = (JTree)e.getSource();
TreePath tp= tree.getPathForLorLocation(e.getPoint().x,e.getPoint().y);
這個(gè)PATH是完整的節(jié)點(diǎn)名,比如:四川,成都
31.截屏程序核心:
BufferedImage fullScreenImage = robot.createScreenCapture (new Rectangle (
Toolkit.getDefaultToolkit ().getScreenSize ()));
ImageIcon icon = new ImageIcon (fullScreenImage);
利用 fullScreenImage.getSubimage(int x,int y,int w,int h)得到子圖象(其中的矩形范圍是鼠標(biāo)拖動(dòng)事件而得到)
ImageIO.write (fullScreenImage.getSubimage(int x,int y,int w,int h),"png","out.png");
界面上:1.繼承JLabel 覆蓋起paint方法;
2.JPanel 從(JPanel)Dialog.getContentPane()來,JLabel 添加進(jìn)JPanel;
3.Dialog設(shè)置為屏幕大小、不顯示邊框等飾物dialog.setUndecorated (true);(JFrame中其實(shí)也有這個(gè)方法。)
4.設(shè)置十字光標(biāo),dialog.setCursor (Cursor.getPredefinedCursor (Cursor.CROSSHAIR_CURSOR));
5.設(shè)置為模態(tài) dialog.setModal (true);
常用鏈接
留言簿(21)
隨筆分類(265)
- android(1)
- c#(.net)(1)
- C++(33)
- DB(5)
- JAVA(38)
- JSP(17)
- linux(7)
- mac(10)
- nginx(3)
- PHP(20)
- python(19)
- UML(1)
- VML(1)
- WebService(2)
- web前端(71)
- XML(4)
- 感悟生活(5)
- 正則表達(dá)式(2)
- 計(jì)算機(jī)綜合(25)
隨筆檔案(242)
- 2012年5月 (1)
- 2012年4月 (2)
- 2011年12月 (9)
- 2011年11月 (20)
- 2011年3月 (8)
- 2010年10月 (15)
- 2010年6月 (2)
- 2010年4月 (1)
- 2010年3月 (7)
- 2010年1月 (4)
- 2009年12月 (1)
- 2009年10月 (1)
- 2009年9月 (1)
- 2009年8月 (8)
- 2009年6月 (11)
- 2009年5月 (3)
- 2009年4月 (7)
- 2008年11月 (5)
- 2008年10月 (6)
- 2008年8月 (1)
- 2008年7月 (6)
- 2008年6月 (5)
- 2008年5月 (1)
- 2008年4月 (18)
- 2008年3月 (1)
- 2008年2月 (1)
- 2007年12月 (9)
- 2007年9月 (5)
- 2007年8月 (6)
- 2007年7月 (4)
- 2007年6月 (6)
- 2007年5月 (8)
- 2007年4月 (9)
- 2007年3月 (6)
- 2007年2月 (44)
相冊(cè)
JAVA網(wǎng)站
關(guān)注的Blog
搜索
積分與排名
- 積分 - 915003
- 排名 - 40
最新評(píng)論

- 1.?re: IP地址獲取當(dāng)前地理位置接口
- fdf
- --fdfs
- 2.?re: 淺談comet技術(shù)
- slowhttp攻擊會(huì)直接把server搞垮 因?yàn)橥ǔ_@種應(yīng)用場(chǎng)景的http的timeout值會(huì)比較大
- --harlan
- 3.?re: php動(dòng)態(tài)圖片驗(yàn)證碼
- 額
- --人
- 4.?re: 關(guān)于傳值和傳引用的插曲
- 大家不要看這篇文章 本來很簡(jiǎn)單的事情反而被博主繞復(fù)雜了,
- --bandit