Swing類部分畫圖方法講解
定義框架
1
2
3
4
5
6
7
8
|
JFrame jFrame= new JFrame( "標(biāo)題名字" ); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設(shè)置用戶在此窗體上發(fā)起 "close" 時(shí)默認(rèn)執(zhí)行的操作。 //有兩種選擇,默認(rèn)是 HIDE_ON_CLOSE即點(diǎn)擊關(guān)閉時(shí)隱藏界面。 jFrame.setBounds( 0 , 0 , 1200 , 1200 ); //設(shè)置框架的大小 jFrame.setVisible( true ); //設(shè)置框架為可見 |
將背景圖片加載進(jìn)入程序中
1
2
3
4
5
6
|
ImageIcon image= new ImageIcon( "文件地址" ); JLabel label= new JLabel(image); label.setBounds( 0 , 0 ,image.getIconWidth(),image.getIconHeight()); //定義標(biāo)簽的大小為圖片大小 jFrame.getLayeredPane().add(label); //將標(biāo)簽添加進(jìn)入JFrame框架中 |
添加按鈕到框架中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
JButton button= new JButton( "hello" ); button.setFont( new Font( "宋體" ,Font.PLAIN, 70 )); //設(shè)置字體的(種類,格式,大小) button.setForeground(Color.red); //設(shè)置字體的顏色 button.setBackground(Color.GREEN); //設(shè)置按鈕框的顏色 button.setBounds( 400 , 0 , 400 , 400 ); //設(shè)置按鈕的大小 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類
1
2
3
4
5
|
JPanel panel = new JPanel(); panel.setLayout( null ); //定義布局 frame.add(panel); //添加到面板。 |
文本框運(yùn)用
1
2
3
4
5
6
7
8
|
//文本框 JTextField userText = new JTextField(要求的字?jǐn)?shù)限制); userText.setBounds( 100 , 20 , 165 , 25 ); panel.add(userText); //密碼文本框(里面內(nèi)容為*) JPasswordField passwordText = new JPasswordField( 20 ); passwordText.setBounds( 100 , 50 , 165 , 25 ); panel.add(passwordText); |
到此這篇關(guān)于Java中Swing類實(shí)例講解的文章就介紹到這了,更多相關(guān)Java中Swing類內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_39528702/article/details/114003240