如何在Java中使用图像图标删除jlabel并将其替换?

时间:2019-02-20 14:41:29

标签: java netbeans jlabel imageicon

我正在尝试创建一个银行系统,并且不想让它变得无聊,我们尝试在jlabel的程序中添加一些图像图标,当我尝试运行代码并首先犯错时,成功删除了测试器标签,但问题是当尝试次数等于4时,Label pinwrongtester似乎并没有移除,而是与我输入4次pin时编程的Label混合在一起,希望大家能帮助我解决这个错误

   public void Option()
{
    //option title
   JLabel label = new JLabel("Choose Options");
   label.setFont(new Font("Consolas",Font.BOLD,20));

   JButton choose = new JButton();
   choose.add(label);
   choose.setBounds(200,300,200,40);
   choose.setBackground(Color.white);
   choose.setEnabled(false);


   // Buttons Option 
   JButton Balance = new JButton("Balance");
   JButton Withdraw= new JButton("Withdraw");
   JButton Send = new JButton("Transfer");
   JButton Exit = new JButton("Exit");


    //Set Bounds
    Balance.setBounds(50,350,200,40);
    Withdraw.setBounds(50,450,200,40);
    Send.setBounds(350,350,200,40);
    Exit.setBounds(350,450,200,40);

    //addActionListener
    Balance.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           pinbalance();
       }
   });
    Withdraw.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           pinwithdraw();
       }
   });
    Send.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           pintransferfunds();
       }
   });

   //add buttons and labels
   p.add(choose);
   p.add(Balance);
   p.add(Withdraw);
   p.add(Send);
   p.add(Exit);
}
JPanel enterpinbg;
JFrame enterpinframe;
String account_name;
String balance;
String pindb;

JLabel tester;
JLabel pinwrongtester;
JLabel pinwrongertester;
JLabel balancetester;

JFrame balanceframe;
JPanel balancepanel;

ImageIcon enteryourpin;
ImageIcon pinwrong;
ImageIcon pinwronger;
ImageIcon balanceicon;

private LinkedList<String> picturetrans;


JTextField inputpin;
boolean police = false;
int x =0;

public void pinbalance()
{

 //PIN FRAME
    enterpinframe = new JFrame("Enter Pin");
    enterpinframe.setSize(WIDTH,HEIGHT);
    enterpinframe.setVisible(true);
    enterpinframe.setResizable(false);
    enterpinframe.setLocationRelativeTo(null);
    enterpinframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



     //PIN PANEL  
    enterpinbg = new JPanel();
    enterpinbg.setSize(WIDTH,HEIGHT);
    enterpinbg.setBackground(Color.orange);
    enterpinbg.setVisible(true);
    enterpinbg.setLayout(null);
    enterpinframe.add(enterpinbg);

  inputpin = new JTextField();
     inputpin.setFont(new Font("Consolas",Font.BOLD,20));
   inputpin.setForeground(Color.gray);

       //Setbounds
    inputpin.setBounds(250,600,100,40);


      //tester + image
     enteryourpin = new ImageIcon(getClass().getResource("Enter yout      PIN.png"));
     tester = new JLabel(enteryourpin);
      tester.setBounds(50,50,500,500);




    //enterpin Add
     enterpinbg.add(tester);
    enterpinbg.add(inputpin);
     this.dispose();


    inputpin.addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override
        public void keyPressed(KeyEvent e) {

            if(e.getKeyCode()==(KeyEvent.VK_ENTER))

            {
            String pinx = inputpin.getText();
            Connection con= null;
            Statement stmt = null;
                try
                {
                    Class.forName(JDBC_DRIVER1);
                    Class.forName(JDBC_DRIVER2);
                    con = DriverManager.getConnection(URL,USER,PASS);
                    stmt=con.createStatement();
                    String sql ="SELECT*FROM TBL_BANK WHERE PIN ='"+pinx+"' ";
                    ResultSet rs = stmt.executeQuery(sql);
                        while(rs.next())
                        {
                        pindb = rs.getString("PIN");
                        account_name = rs.getString("ACCOUNT_NAME");
                        balance = rs.getString("BALANCE");    
                        }
                        rs.close();

                }
                catch(Exception f)
                {
                    System.out.println(f);
                }

                //if Login Success and Failed

                if(pinx.equals(pindb))
                { 
                  // Balance will show up

                 }

                else if(pinx!=(pindb))
                {

                    enterpinbg.remove(tester); 
                    x++;
                    System.out.println("x");
                    inputpin.setText("");


                    pinwrong = new ImageIcon(getClass().getResource("PIN Wrong (1_2Times).png"));
                    pinwrongtester = new JLabel(pinwrong);
                    pinwrongtester.setBounds(50,50,500,500);
                    enterpinbg.add(pinwrongtester);

                     enterpinbg.repaint();

                    if(x>=4 )
                      {
                        // this doesnt quite work please help me fix it
                       enterpinbg.remove(pinwrongtester);
                       enterpinbg.repaint();


                       }


                }



            }
        }

        @Override
        public void keyReleased(KeyEvent e) {

        }
    });

0 个答案:

没有答案