swing那点事

xiaoxiao2021-02-28  27

swing是一个扩张的awt,说白了也就是对awt里面的功能进行了一些封装。

1把窗体放在你现在电脑的位置

Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension screenSize = toolkit.getScreenSize(); jFrame.setLocation((int)(screenSize.getWidth()-width)/2, (int)(screenSize.getHeight()-height)/2);

width:这个窗体的宽度,height:这个窗体的高度

2给窗体添加一张图片做为背景图

URL resource =Thread.currentThread().getContextClassLoader() .getResource("qqww.jpg"); String path= resource.getPath(); BackgroundPanel bgp = new BackgroundPanel(new ImageIcon(path).getImage() ) ; bgp.setBounds(0, 0, 600, 400); jFrame.getContentPane().add(bgp); class BackgroundPanel extends JPanel{ Image image ; public BackgroundPanel(Image image){ this.image = image; this.setOpaque(true); } @Override protected void paintComponent(Graphics g) { // TODO Auto-generated method stub super.paintComponent(g); g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this); } }

写一个Jpanel子类BackgroundPanel重写paintComponent方法来显示图片。

转载请注明原文地址: https://www.6miu.com/read-1000119.html

最新回复(0)