java - 背景图像隐藏所有 GUI 设计组件

标签 java image swing background

我正在开发 GUI 应用程序,我使用 swing 组件来设计 GUI。我想为我的表单设置背景图像,但是当我设置图像时,它与用于设计 GUI 的所有组件重叠。

首先我的没有背景图片的表单如下图所示,

enter image description here

我的代码是,

登录.java

public class Login extends JFrame{
    public static JFrame myFrame;
    public LoginPanel loginPanel;

    public Login() throws IOException
    {
        initilize();
    }

    public void initilize()throws IOException {

        myFrame = new JFrame("Message"){ 
            private Image backgroundImage = ImageIO.read(new File("D:/Sky.jpg"));
            public void paint( Graphics g ) { 
                super.paint(g);
                g.drawImage(backgroundImage, 0, 0, null);
            }
        };
        myFrame.setLayout(new BorderLayout());

        loginPanel = new LoginPanel();

        //Panel
        Container c = myFrame.getContentPane();

        c.add(loginPanel, BorderLayout.WEST);
        myFrame.setSize(300, 150);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
        myFrame.setLocationRelativeTo(null);

    } 

    public static void main(String[] arg) {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                try {
                    new Login();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

登录面板.java

public class LoginPanel extends JPanel implements ActionListener {
    public JLabel user_no=null;
    public JLabel password=null;
    public JButton btn_login = null;
    public JButton btn_newUser = null;
    public JTextField usernameField=null;
    public JPasswordField passwordField=null;
    public static String userNo;
    public ArrayList msgList = new ArrayList();

    public LoginPanel()
    {
        initilize();
        initConnection();
    }

    private void initilize() {

        Dimension size = getPreferredSize();
        size.width = 285;
        size.height = 150;
        setPreferredSize(size);

        setBorder(BorderFactory.createTitledBorder(null, "Login Details", TitledBorder.CENTER, TitledBorder.TOP));

        user_no = new JLabel("User No : ");

        password = new JLabel("Password : ");

        usernameField = new JTextField(14);

        passwordField = new JPasswordField(14);

        btn_login = new JButton("Login");

        btn_newUser = new JButton("New User");

        setLayout(new GridBagLayout());

        GridBagConstraints gc = new GridBagConstraints();

        //// First column /////////////////////////

        gc.anchor = GridBagConstraints.LINE_START;

        gc.gridx = 0;
        gc.gridy = 0;
        add(user_no, gc);

        gc.gridx = 0;
        gc.gridy = 1;
        add(password, gc);

        //// Second column
        gc.anchor = GridBagConstraints.LINE_START;

        gc.gridx = 1;
        gc.gridy = 0;
        add(usernameField, gc);

        gc.gridx = 1;
        gc.gridy = 1;
        add(passwordField, gc);

        // Final row
        gc.anchor = GridBagConstraints.FIRST_LINE_START;
        gc.gridx = 1;
        gc.gridy = 2;
        add(btn_login, gc);

        gc.anchor = GridBagConstraints.FIRST_LINE_END;
        gc.gridx = 1;
        gc.gridy = 2;
        add(btn_newUser, gc);

    }

    private void initConnection() {
        btn_login.addActionListener(this);
        btn_newUser.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource().equals(btn_login))
        {
            userNo = usernameField.getText();
            String userPwd = passwordField.getText();
            System.out.println(userNo+" "+userPwd);
            Connection con = ConnectionImpl.getConnection();

            try
            {
                PreparedStatement pstmt;

                String sql = "SELECT sender_no,pwd FROM tb_login where sender_no ='"+userNo+"'"; 

                pstmt= con.prepareStatement(sql);

                ResultSet rs = pstmt.executeQuery();

                if(rs.next())
                {
                    if(userNo.equalsIgnoreCase(rs.getString(1))&&userPwd.equalsIgnoreCase(rs.getString(2)))
                    {
                        System.out.println("Successfull login");

                        String sql2 = "Select msg from tb_msg where sender_no='"+userNo+"'";

                        pstmt= con.prepareStatement(sql2);

                        ResultSet rs2 = pstmt.executeQuery();

                        while(rs2.next())
                        {
                            msgList.add(rs2.getString(1));
                        }
                        System.out.println("msgList = "+msgList.size());

                        Login.myFrame.dispose();
                        new AddMessage(userNo,msgList);
                    }
                }
            }
            catch (Exception exp) {
                exp.printStackTrace();
            }
        }

        if(e.getSource().equals(btn_newUser))
        {
            Login.myFrame.dispose();
            new NewUser();
        }
    }

}

设置背景图片后是这样的

enter image description here

它隐藏了JButtonJLabel 的组件。

最佳答案

代码需要更接近于此:

Login panel

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import javax.swing.border.TitledBorder;

public class Login extends JFrame{
    public static JFrame myFrame;
    public LoginPanel loginPanel;

    public Login() throws IOException
    {
        initilize();
    }

    public void initilize()throws IOException {
        BufferedImage backgroundImage = new BufferedImage(400,200,BufferedImage.TYPE_INT_RGB);
        myFrame = new JFrame("Message");
        myFrame.setLayout(new BorderLayout());

        loginPanel = new LoginPanel(backgroundImage);

        //Panel
        Container c = myFrame.getContentPane();

        c.add(loginPanel, BorderLayout.WEST);
        //myFrame.setSize(300, 150);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.pack();
        myFrame.setVisible(true);
        myFrame.setLocationRelativeTo(null);

    } 

    public static void main(String[] arg) {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                try {
                    new Login();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

class LoginPanel extends JPanel implements ActionListener {
    public JLabel user_no=null;
    public JLabel password=null;
    public JButton btn_login = null;
    public JButton btn_newUser = null;
    public JTextField usernameField=null;
    public JPasswordField passwordField=null;
    public static String userNo;
    public ArrayList msgList = new ArrayList();

    private BufferedImage backgroundImage;

    public LoginPanel(BufferedImage backgroundImage)
    {
        this.backgroundImage = backgroundImage;
        initilize();
        initConnection();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
    }

    private void initilize() {

        Dimension size = getPreferredSize();
        size.width = 285;
        size.height = 150;
        setPreferredSize(size);

        setBorder(BorderFactory.createTitledBorder(null, "Login Details", TitledBorder.CENTER, TitledBorder.TOP));

        user_no = new JLabel("User No : ");
        password = new JLabel("Password : ");
        usernameField = new JTextField(14);
        passwordField = new JPasswordField(14);
        btn_login = new JButton("Login");
        btn_newUser = new JButton("New User");
        setLayout(new GridBagLayout());

        GridBagConstraints gc = new GridBagConstraints();

        //// First column /////////////////////////

        gc.anchor = GridBagConstraints.LINE_START;

        gc.gridx = 0;
        gc.gridy = 0;
        add(user_no, gc);

        gc.gridx = 0;
        gc.gridy = 1;
        add(password, gc);

        //// Second column
        gc.anchor = GridBagConstraints.LINE_START;

        gc.gridx = 1;
        gc.gridy = 0;
        add(usernameField, gc);

        gc.gridx = 1;
        gc.gridy = 1;
        add(passwordField, gc);

        // Final row
        gc.anchor = GridBagConstraints.FIRST_LINE_START;
        gc.gridx = 1;
        gc.gridy = 2;
        add(btn_login, gc);

        gc.anchor = GridBagConstraints.FIRST_LINE_END;
        gc.gridx = 1;
        gc.gridy = 2;
        add(btn_newUser, gc);

    }

    private void initConnection() {
        btn_login.addActionListener(this);
        btn_newUser.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource().equals(btn_login))
        {
            userNo = usernameField.getText();
            String userPwd = passwordField.getText();
            System.out.println(userNo+" "+userPwd);
            Connection con = null;

            try
            {
                PreparedStatement pstmt;
                String sql = "SELECT sender_no,pwd FROM tb_login where sender_no ='"+userNo+"'"; 
                pstmt= con.prepareStatement(sql);
                ResultSet rs = pstmt.executeQuery();
                if(rs.next())
                {
                    if(userNo.equalsIgnoreCase(rs.getString(1))&&userPwd.equalsIgnoreCase(rs.getString(2)))
                    {
                        System.out.println("Successfull login");
                        String sql2 = "Select msg from tb_msg where sender_no='"+userNo+"'";
                        pstmt= con.prepareStatement(sql2);
                        ResultSet rs2 = pstmt.executeQuery();

                        while(rs2.next())
                        {
                            msgList.add(rs2.getString(1));
                        }
                        System.out.println("msgList = "+msgList.size());

                        Login.myFrame.dispose();
                        //new AddMessage(userNo,msgList);
                    }
                }
            }
            catch (Exception exp) {
                exp.printStackTrace();
            }
        }

        if(e.getSource().equals(btn_newUser))
        {
            Login.myFrame.dispose();
            //new NewUser();
        }
    }
}

更多提示

  1. 为了尽快获得更好的帮助,请发布 SSCCE .
  2. 不要扩展框架,只使用一个实例,但是..
  3. 在这种情况下根本不使用框架,而是在 JDialogJOptionPane 中显示登录。

关于java - 背景图像隐藏所有 GUI 设计组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14979647/

相关文章:

java - 位图大小与质量 - 内存不足异常

javascript - 当图像位于同一域时,Canvas 元素未呈现为 PNG(安全错误)

java - 如何在单元格边界内显示图像

java - MVC 模式 : which is better? 为 View 或 Controller 创建和引用其他?

java - 使用 BoxLayout 时 JPanel 不会调整大小

java - 设置 JProgressBar

java - 在现有项目结构上设置gradle

java - 我正在努力创建和理解简单射击游戏程序的特定方法

java - ArrayList 的标准偏差

java - 依赖 Jar 未引用其自己的 application.properties