好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java中Swing类实例讲解

Swing类部分画图方法讲解

定义框架

?

1

2

3

4

5

6

7

8

JFrame jFrame= new JFrame( "标题名字" );

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//设置用户在此窗体上发起 "close" 时默认执行的操作。

//有两种选择,默认是 HIDE_ON_CLOSE即点击关闭时隐藏界面。

jFrame.setBounds( 0 , 0 , 1200 , 1200 );

//设置框架的大小

jFrame.setVisible( true );

//设置框架为可见

将背景图片加载进入程序中

?

1

2

3

4

5

6

ImageIcon image= new ImageIcon( "文件地址" );

JLabel label= new JLabel(image);

label.setBounds( 0 , 0 ,image.getIconWidth(),image.getIconHeight());

//定义标签的大小为图片大小

jFrame.getLayeredPane().add(label);

//将标签添加进入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 ));

  //设置字体的(种类,格式,大小)

  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++;

  }

  });

  //添加按钮的事件监听

jFrame.getLayeredPane().add(button);

//将按钮添加到框架中

JPanel类

?

1

2

3

4

5

JPanel panel = new JPanel();

panel.setLayout( null );

//定义布局

frame.add(panel);

//添加到面板。

文本框运用

?

1

2

3

4

5

6

7

8

//文本框

JTextField userText = new JTextField(要求的字数限制);

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类内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_39528702/article/details/114003240

查看更多关于Java中Swing类实例讲解的详细内容...

  阅读:16次