JFrame中怎么添加背景图片,输入框的大小怎么修改

JFrame中怎么添加背景图片,输入框的大小怎么修改,第1张

import java.awt.*

import java.awt.image.*

import javax.swing.*

import javax.imageio.ImageIO

public class LoginExample {

    public static void main(final String... args) {

        EventQueue.invokeLater(new Runnable(){

                @Override public void run(){

                    try {

                        final XPanel container = new XPanel(ImageIO.read(LoginExample.class.getResource("/images/background.jpg")))

                        final JPanel panel = new JPanel()

                        final GroupLayout layout = new GroupLayout(panel)

                        container.setLayout(new BorderLayout())

                        panel.setLayout(layout)

                        panel.setOpaque(false)

                        layout.setAutoCreateGaps(true)

                        layout.setAutoCreateContainerGaps(true)

                        final JLabel name = new JLabel("用户名")

                        final JLabel pass = new JLabel("密码")

                        final JTextField name_field = new JTextField(12)

                        final JPasswordField pass_field = new JPasswordField(12)

                        name_field.setOpaque(false)

                        pass_field.setOpaque(false)

                        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup()

                        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING).

                                        addComponent(name).addComponent(pass))

                        hGroup.addGroup(layout.createParallelGroup().

                                        addComponent(name_field).addComponent(pass_field))

                        layout.setHorizontalGroup(hGroup)

                        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup()

                        vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).

                                        addComponent(name).addComponent(name_field))

                        vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).

                                        addComponent(pass).addComponent(pass_field))

                        layout.setVerticalGroup(vGroup)

                        final JFrame frame = new JFrame("Login")

                        container.add(new JLabel("**欢迎您登录**", JLabel.CENTER), BorderLayout.PAGE_START)

                        container.add(panel)

                        frame.setContentPane(container)

                        frame.pack()

                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

                        frame.setVisible(true)

                    } catch (Exception e) {

                    }

                }

            })

    }

    public static class XPanel extends JPanel {

        private BufferedImage background

        public XPanel(BufferedImage background) {

            this.background = background

        }

        @Override public void paintComponent(Graphics g) {

            BufferedImage bufimg = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_ARGB)

            Graphics2D g2 = bufimg.createGraphics()

            // restore the foreground color in case the superclass needs it

            g2.setColor(g.getColor())

            super.paintComponent(g2)

            // do an alpha composite

            Graphics2D gx = (Graphics2D) g

            gx.drawImage(background,0,0,null)

            gx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.8f))

            gx.drawImage(bufimg,0,0,null)

        }

    }

}

import javax.swing.*

import java.awt.*

/**

* @author Hardneedl

*/

final class PictureBackgroundFrame extends JFrame {

public String getTitle() {return "frame title"}

static private final Dimension size = new Dimension(600, 400)

public Dimension getPreferredSize() {return size}

public Dimension getMaximumSize() {return size}

public Dimension getMinimumSize() {return size}

public Dimension getSize() {return size}

private static final Image image = Toolkit.getDefaultToolkit().getImage("b.JPG")

private static final JComponent canvas = new JComponent(){

protected void paintComponent(Graphics g) {

super.paintComponent(g)

Graphics gg = g.create()

gg.drawImage(image,0,0,getWidth(),getHeight(),this)

gg.dispose()

}

}

PictureBackgroundFrame() throws HeadlessException {

attachListeners()

doLay()

}

private void attachListeners() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

}

private void doLay() {

Container container = getContentPane()

container.add(canvas)

pack()

setVisible(true)

}

public static void main(String[] args) {new PictureBackgroundFrame()}

}

通过以下方式设置下背景就可以了:

background = new ImageIcon("images/backImage.png")

backImage = new JLabel(background)

backImage.setBounds(0, 0, background.getIconWidth(),

background.getIconHeight())

backPanel = (JPanel) this.getContentPane()

backPanel.setOpaque(false)

this.getLayeredPane().setLayout(null)

this.getLayeredPane().add(backImage, new Integer(Integer.MIN_VALUE))

backPanel.setLayout(new BorderLayout())

这样就可以把图片放在最下面了,效果图


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/7952147.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存