import java.awt.AWTEvent; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Graphics; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class MainUi extends JPanel implements ActionListener { protected JTextField id, psw; protected JButton ok, register, exit; protected JLabel idl, pswl, imgl; /** * 因为原来你原来就是实例变量而且不是private修饰的 * 所以我没有改成createGUI方法里的局部变量 * 因为下面要用所以image加了static修饰 */ protected static JLabel imgpanel; protected static ImageIcon image; public MainUi() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); id = new JTextField(60); id.setActionCommand("ID"); idl = new JLabel("ID :"); idl.setLabelFor(id); psw = new JTextField(60); psw.setActionCommand("password"); pswl = new JLabel("PassWord: "); pswl.setLabelFor(psw); ok = new JButton("OK"); register = new JButton("REGISTER"); exit = new JButton("EXIT"); JPanel login = new JPanel(); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); login.setLayout(gridbag); // imgl.setBounds(0, 0, imgIcon.getIconWidth(), imgIcon.getIconHeight()); // login.setOpaque(false); JLabel[] labels = { idl, pswl }; JTextField[] textFields = { id, psw }; addLabelTextRows(labels, textFields, gridbag, login); c.gridwidth = GridBagConstraints.REMAINDER; // last c.anchor = GridBagConstraints.WEST; c.weightx = 1.0; login.setBorder(BorderFactory.createCompoundBorder(BorderFactory .createTitledBorder("Text Fields"), BorderFactory .createEmptyBorder(5, 5, 5, 5))); add(login, BorderLayout.CENTER); } private void addLabelTextRows(JLabel[] labels, JTextField[] textFields, GridBagLayout gridbag, Container container) { GridBagConstraints c = new GridBagConstraints(); c.anchor = GridBagConstraints.EAST; int numLabels = labels.length; for (int i = 0; i < numLabels; i++) { c.gridwidth = GridBagConstraints.RELATIVE; // next-to-last c.fill = GridBagConstraints.NONE; // reset to defau container.add(labels[i], c); c.gridwidth = GridBagConstraints.REMAINDER; // end row c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; container.add(textFields[i], c); } } @Override public void actionPerformed(ActionEvent arg0) { } /** * 这里重写了JPanel的paintComponent方法,目的是把图片画上去 */ protected void paintComponent(Graphics g) { if (ui != null) { Graphics scratchGraphics = (g == null) ? null : g.create(); try { ui.update(scratchGraphics, imgpanel); } finally { scratchGraphics.dispose(); } } } public static void createGUI() { JFrame frame = new JFrame("Plants and Zombies"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); image = new ImageIcon("D://java/PZ/image/bg.jpg"); imgpanel = new JLabel(image); frame.getLayeredPane().add(imgpanel, new Integer(Integer.MIN_VALUE)); imgpanel.setBounds(0,0,image.getIconWidth(),image.getIconHeight()); //这里不要漏掉 MainUi mainUi=new MainUi(); frame.getContentPane().add(mainUi); ((JPanel)frame.getContentPane()).setOpaque(false); //frame.pack(); 既然下面设置了尺寸干嘛还要pack一下? frame.setSize(800, 600); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { UIManager.put("swing.boldMetal", Boolean.FALSE); createGUI(); } }); } }