JComponents没有出现有图片背景?
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage; import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.*;public class login implements ActionListener{ JTextField gusername; JTextField gpassword; static String username; static String password; void logini() throws IOException { JFrame window = new JFrame("Login"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setSize(300, 250); window.setResizable(false); window.setVisible(true); JPanel mainp = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); window.add(mainp); BufferedImage myPicture = ImageIO.read(new File("c:\\bgd.png")); JLabel picLabel = new JLabel(new ImageIcon( myPicture )); mainp.add(picLabel, c); c.gridx = 0; c.gridy = 1; gusername = new JTextField(); gusername.setText("Username"); mainp.add(gusername, c); c.gridx = 0; c.gridy = 2; gpassword = new JTextField(); gpassword.setText(" password "); mainp.add(gpassword, c); c.gridx = 0; c.gridy = 3; JButton login = new JButton("Login"); mainp.add(login, c); login.addActionListener(this); login.setActionCommand("ok"); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equalsIgnoreCase("ok")){ try { this.username = (gusername.getText()); this.password = (gpassword.getText()); System.out.println("0"); } catch(NumberFormatException ex){ System.out.println("ERROR: Could not preform function: 7424"); } } }}
相关分类