Java中Swing類實例講解
更新時間:2021年02月26日 11:10:59 作者:nightelves11
這篇文章主要介紹了Java中Swing類實例講解,文中用代碼實例講解的很清楚,有需要的同學可以研究下
Swing類部分畫圖方法講解
定義框架
JFrame jFrame=new JFrame("標題名字"); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設置用戶在此窗體上發(fā)起 "close" 時默認執(zhí)行的操作。 //有兩種選擇,默認是 HIDE_ON_CLOSE即點擊關閉時隱藏界面。 jFrame.setBounds(0,0,1200,1200); //設置框架的大小 jFrame.setVisible(true); //設置框架為可見
將背景圖片加載進入程序中
ImageIcon image=new ImageIcon("文件地址"); JLabel label=new JLabel(image); label.setBounds(0,0,image.getIconWidth(),image.getIconHeight()); //定義標簽的大小為圖片大小 jFrame.getLayeredPane().add(label); //將標簽添加進入JFrame框架中
添加按鈕到框架中
JButton button=new JButton("hello"); button.setFont(new Font("宋體",Font.PLAIN,70)); //設置字體的(種類,格式,大?。? button.setForeground(Color.red); //設置字體的顏色 button.setBackground(Color.GREEN); //設置按鈕框的顏色 button.setBounds(400,0,400,400); //設置按鈕的大小 button.addActionListener(new ActionListener() { int n=1; @Override public void actionPerformed(ActionEvent e) { System.out.println("click"+n+"次"); n++; } }); //添加按鈕的事件監(jiān)聽 jFrame.getLayeredPane().add(button); //將按鈕添加到框架中
JPanel類
JPanel panel = new JPanel(); panel.setLayout(null); //定義布局 frame.add(panel); //添加到面板。
文本框運用
//文本框 JTextField userText = new JTextField(要求的字數(shù)限制); userText.setBounds(100,20,165,25); panel.add(userText); //密碼文本框(里面內容為*) JPasswordField passwordText = new JPasswordField(20); passwordText.setBounds(100,50,165,25); panel.add(passwordText);
到此這篇關于Java中Swing類實例講解的文章就介紹到這了,更多相關Java中Swing類內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Elasticsearch查詢Range Query語法示例
這篇文章主要為大家介紹了Elasticsearch查詢Range Query語法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04