永发信息网

如何在Java中Jlable上既显示图片又显示JtextField(以下代码错在哪里啊)

答案:1  悬赏:10  手机版
解决时间 2021-05-15 20:50
  • 提问者网友:niaiwoma
  • 2021-05-15 12:27

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

public class Ahi extends JFrame {

 private static final long serialVersionUID = 1L;
 private JPanel jContentPane = null;
 private JPanel pan = null;
 private JLabel users = null;
 private JLabel userpassword = null;
 private JTextField tfName = null;
 private JPasswordField tfPass = null;
 private JButton OK = null;
 private JButton Cancel = null;
 private JPanel back = null; 
 private JLabel lab = null;
 private ImageIcon ii= new ImageIcon(getClass().getResource("/com/qq/image/20092792056688_2.jpg"));
 
 public Ahi() {
  super();
  initialize();
 }

 
 private void initialize() {
  this.setSize(400, 300);
  this.setContentPane(getJContentPane());
  this.setTitle("JFrame");
  
 }

 
 private JPanel getJContentPane() {
  if (jContentPane == null) {
   jContentPane = new JPanel();
   jContentPane.setLayout(null);
   jContentPane.add(getPan(), null);
   jContentPane.setLayout(null);
   jContentPane.add(lab,new Integer(Integer.MIN_VALUE));
   jContentPane.add(pan);
  }
  return jContentPane;
 }

 
 private JPanel getPan() {
  if (pan == null) {
   
   pan = new JPanel();
   pan.add(back);
   pan.setLayout(null);
   pan.setOpaque(false);
   lab =new JLabel(ii);
   lab.setBounds(new Rectangle(2, 0, 400, 281));
   pan.setBounds(0, 0, 393, 264);
   pan.add(lab, null);
   
  }
  return pan;
 }

 
 private JTextField getTfName() {
  if (tfName == null) {
   tfName = new JTextField(20);
   tfName.setBounds(new Rectangle(170, 51, 150, 23));
   tfName.setOpaque(false);//设置为透明
  }
  return tfName;
 }

 
 private JPasswordField getTfPass() {
  if (tfPass == null) {
   tfPass = new JPasswordField();
   tfPass.setBounds(new Rectangle(170, 153, 150, 23));
   tfPass.setOpaque(false);
  }
  return tfPass;
 }

 
 private JButton getOK() {
  if (OK == null) {
   OK = new JButton("登陆");
   OK.setBounds(new Rectangle(94, 217, 75, 23));
   OK.setOpaque(false);
  }
  return OK;
 }

 
 private JButton getCancel() {
  if (Cancel == null) {
   Cancel = new JButton("取消");
   Cancel.setBounds(new Rectangle(203, 218, 75, 23));
  }
  return Cancel;
 }

 
 private JPanel getBack() {
  if (back == null) {
   back = new JPanel();
   back.setLayout(null);
   back.setOpaque(false);
   userpassword = new JLabel("密  码");
   userpassword.setBounds(new Rectangle(56, 146, 65, 27));
   users = new JLabel("用   户");
   users.setBounds(58, 49, 65, 27);
   //back = new JPanel();
   
   back.setBounds(new Rectangle(0, 0, 393, 264));
   back.add(users, null);
   back.add(userpassword, null);
   back.add(getTfName(), null);
   back.add(getTfPass(), null);
   back.add(getOK(), null);
   back.add(getCancel(), null);
   back.add(lab, null);
  }
  return back;
 }

}  //  @jve:decl-index=0:visual-constraint="207,-4"

最佳答案
  • 五星知识达人网友:孤独的牧羊人
  • 2021-05-15 13:31
建立一个JPanelclass ImageJPanel extends JPanel //绘制背景图片 public void paintComponent(Graphics g) } 在实例化组件的时候,将该面板添加进去:ImageJPanel ip=new ImageJPanel();ip.setBounds(0,0,this.getWidth(),this.getHeight());//设置背景图片的长和宽和窗体大小一致this.add(ip);//添加到面板中
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯