2011-06-14 1 views
0

jApplet을 확장하는 클래스와 jpanel을 확장하는 3 개의 클래스가 있습니다. 자, JButton에서 특정 Jpanel로 가져온 작업에 대해 이러한 jPanels을 전환하려고합니다.Java JApplet - 외부 클래스에서 생성 된 JPanels 전환 시도

문제점 : init() 메소드에서 애플릿 자체에서 볼 수 있도록 설정 한 애플릿의 첫 번째 패널을로드/표시 할 수 있습니다. 하지만, jPanel 폼을 클릭하여 동일한 작업을 시도하면 작동하지 않습니다. Me 패널이 닫히지 만 추가 된 새 패널이 표시되지 않습니다. 아래 코드의

코드 문맥 :

  1. 응용 프로그램. java는 애플릿 클래스입니다.
  2. User_Login. 자바 (1)
  3. Reset_Password.java (2) USER_LOGIN의 JPanel의에서 암호 변경 버튼의 클릭에

는, 내가 Reset_Password의 인 JPanel를 호출 할 JPanel의 클래스 인 JPanel의 클래스입니다.

코드는 다음과 같습니다. 어떤 도움은 매우

Application.java

에게

을 감사
package business_intelligence; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 


/** 
* 
* @author lokesh 
*/ 
public class Application extends JApplet { 

int x = 0; 

Container content_pane = getContentPane(); 

User_Login obj = new User_Login(); 
Reset_Password obj1 = new Reset_Password(); 



public void init() { 

    setVisible(true); 
    setSize(1200, 1000); 
    content_pane.add(obj); 
    content_pane.add(obj1); 
    content_pane.add(obj2); 
    add(obj1); 
    add(obj2); 
    add(obj); 
    obj.setVisible(true); 
    obj1.setVisible(false); 
    obj2.setVisible(false); 
} 

public void Change() 
{ 
    content_pane.removeAll(); 
    content_pane.add(obj1); 
    add(obj1); 
    obj1.setVisible(true); 
} 
public void Login() 
{ 
    content_pane.removeAll(); 
    content_pane.add(obj); 
    add(obj); 
    obj.setVisible(true); 
} 
} 

User_Login.java

package business_intelligence; 
import javax.swing.*; 
import java.awt.*; 
import javax.swing.JApplet.*; 
import java.awt.event.*; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyListener; 
/** 
* 
* @author lokesh 
*/ 
public class User_Login extends JPanel implements ActionListener { 
FlowLayout f; 
JLabel l1; 
JLabel l2, l3, h1 ; 
JButton b1; 
JButton b2,b3,b4; 
Font fo1 = new Font("Verdana",Font.BOLD,25); 
Font fo2 = new Font("Verdana",Font.PLAIN,15); 
JTextField tf1 = new JTextField(); 
JPasswordField tp1 = new JPasswordField(); 
Color bg = new Color (255,255,204); 

public User_Login() 
{ 

    setVisible(true); 
    setSize(1200, 1000); 
    setBackground(bg); 
    setLayout(null); 
    h1 = new JLabel("Welcome to Business Intelligence Application -- Login"); 
h1.setFont(fo1); 
h1.setBounds(100, 02, 800, 50); 
l1 = new JLabel("User-name"); 
l1.setFont(fo2); 
l2 = new JLabel("Password"); 
l2.setFont(fo2); 
l3 = new JLabel(); 
l3.setFont(fo2); 
l3.setForeground(Color.RED); 
b1 = new JButton("Submit"); 
b2 = new JButton("Close"); 
b3 = new JButton("Reset Password"); 
b4 = new JButton("New User Request"); 
add(h1); 
l1.setBounds(100, 100, 100, 40); 
add(l1); 
l2.setBounds(100, 150, 100, 40); 
add (l2); 
add(b1); 
add(b2); 
tf1.setBounds(300, 100, 200, 40); 
add(tf1); 
tp1.setBounds(300, 150, 200, 40); 
add(tp1); 
b1.setBounds(25, 260, 150, 50); 
add(b1); 
b2.setBounds(200, 260, 150, 50); 
add(b2); 
l3.setBounds(100, 200, 400, 50); 
add(l3); 
b3.setBounds(375, 260, 150, 50); 
add(b3); 
b4.setBounds(550, 260, 160, 50); 
add(b4); 
b1.addActionListener(this); 
b2.addActionListener(this); 
b3.addActionListener(this); 
b4.addActionListener(this); 

} 

public void verifyLogin() 
{ 

    String uid = tf1.getText(); 
    char ch[] = tp1.getPassword(); 
    String pwd = String.valueOf(ch); 

    Connect obj1 = new Connect(); 
    obj1.connect(); 
    int r = obj1.verifyLogin(uid, pwd); 
    if (r ==1) 
    { 
     setVisible(false); 
     Application obj = new Application(); 
     obj.Dashboard(); 

    } 
    else 
    { 
     l3.setText("Incorrect User-name or Password. Try again."); 
    } 
} 

public void actionPerformed(java.awt.event.ActionEvent evt) 
{ 
    Object ob = evt.getSource(); 
    if(ob == b1) 
     verifyLogin(); 
    else if (ob == b2) 
      System.exit(0); 
    else if (ob == b3) 
    { 
     setVisible(false); 
     Application obj = new Application(); 
     obj.Change(); 
    } 

} 

} 

Reset_Password.java

package business_intelligence; 

import java.awt.event.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionListener; 

//mport java.awt.event.*; 
/** 
* 
* @author lokesh 
*/ 
public class Reset_Password extends JPanel implements ActionListener { 

String email; 
String pwd; 
String cpwd; 
JLabel l1,l2, l3,l4, h1 ; 
JButton b1; 
JButton b2; 
Font fo3 = new Font("Verdana",Font.BOLD,25); 
Font fo4 = new Font("Verdana",Font.PLAIN,15); 
JTextField tf1 = new JTextField(); 
JPasswordField p1 = new JPasswordField(); 
JPasswordField p2 = new JPasswordField(); 
int r; 

Color bg = new Color (255,255,204); 



public Reset_Password() 
{ 
    setSize(1200, 1000); 
    setBackground(bg); 
    setLayout(null); 
    h1 = new JLabel("Welcome to Business Intelligence Application -- Change Password"); 
    h1.setFont(fo3); 
    h1.setBounds(100, 02, 1000, 50); 
    add(h1); 
    l1 = new JLabel("E-mail"); 
    l1.setFont(fo4); 
    l2 = new JLabel("New Password"); 
    l2.setFont(fo4); 
    l3 = new JLabel("Confirm Password"); 
    l3.setFont(fo4); 
    l4 = new JLabel(""); 
    l4.setFont(fo4); 
    l4.setForeground(Color.RED); 
    b1 = new JButton("Submit"); 
    b2 = new JButton("Cancel"); 
    l1.setBounds(100, 100, 100, 40); 
    add(l1); 
    l2.setBounds(100, 150, 100, 40); 
    add (l2); 
    add(b1); 
    add(b2); 
    tf1.setBounds(300, 100, 200, 40); 
    add(tf1); 
    p1.setBounds(300, 150, 200, 40); 
    add(p1); 
    p2.setBounds(300, 200, 200, 40); 
    add(p2); 
    l3.setBounds(100, 200, 400, 50); 
    add(l3); 
    l4.setBounds(100, 250, 400, 50); 
    add(l4); 
    b1.setBounds(100, 300, 150, 50); 
    add(b1); 
    b2.setBounds(350, 300, 150, 50); 
    add(b2); 
    b1.addActionListener(this); 
    b2.addActionListener(this); 
} 


@Override 
public void actionPerformed (java.awt.event.ActionEvent e)  
{           

    Object ob = e.getSource(); 
    if(ob == b1)  
     resetPassword(); 
    if (ob == b2) 
    { 

      Application obj = new Application(); 
      obj.Login(); 
    } 

} 

    // TODO add your handling code here           

/*private void ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
    //setVisible(false); 

    if (isVisible()== true) 
     setVisible(false); 

    this.remove(this); 
    Business_Intelligence obj = new Business_Intelligence(); 
    obj.activated(); 

} */  



public void resetPassword() 
{ 

    email = tf1.getText(); 
    char ch[] = p1.getPassword(); 
    pwd = String.valueOf(ch); 
    char ch1[] = p2.getPassword(); 
    cpwd = String.valueOf(ch1); 
    if (ch.length >= 8) 
    { 

     if(pwd.equals(cpwd)) 
     { 
     Connect obj = new Connect(); 
     obj.connect(); 
     int r = obj.verifyEmail(email); 
     if (r==1) 
      { 
       obj.connect(); 
       obj.updat(email, pwd); 
       l4.setVisible(true); 
       l4.setText("password changed");    
       p1.setText(null); 
       p2.setText(null); 
       tf1.setText(null); 

      } 
     if (r==0) 
      { 
       l4.setVisible(true); 
       l4.setText("incorrect e-mail id"); 
      } 
     } 
     if(!pwd.equals(cpwd)) 
     { 
     //jLabel5.setForeground(Color.RED); 
     l4.setVisible(true); 
     l4.setText("password and confirm password did not match");  
     } 
    } 
    if (ch.length < 8) 
    { 
    //jLabel5.setForeground(Color.RED); 
    l4.setVisible(true); 
    l4.setText("password should be a minimum of 8 characters"); 
    } 
} 
} 

답변

1

대신 직접 애플릿의 ContentPane에 다음 퍼팅, 당신 해당 ContentPane에 CardLayout을 넣고 visib를 변경해야합니다. 그 CardLayout 패널은 훨씬 간단한 작업입니다.

편집

OK, 이제 당신이 다음, 당신의 ContentPane에 CardLayout을 넣어 그것을 다른 모든 패널을 추가 할 생각 해보자. 이를 위해

, 당신은 단순히 같은 당신의 초기화 방법을 변경할 수 있습니다 : 사용자가 로그인 할 때, 그리고

public void init() { 

    setVisible(true); 
    setSize(1200, 1000); 

    // Shamelessely borrowed from http://download.oracle.com/javase/tutorial/uiswing/layout/card.html 
    // which you SHOULD definitevely read 
    content_pane.setLayout(new CardLayout()); 

    // First item will stay visible, as long as it is not changed 
    content_pane.add(obj, "user_login"); 
    content_pane.add(obj1, "user_panel"); 
    content_pane.add(obj2, "reset_login"); 

} 

을, 당신은 활성 패널을 변경해야 할, 같은

로 변경 방법을 변경
public void Login() 
{ 
    CardLayout card = (CardLayout) content_pane.getLayout(); 
    card.show(content_pane, "user_panel"); 
} 
+0

빠른 답변을 보내 주셔서 감사합니다. 빠른 답변을 보내 주시면 감사하겠습니다. – Lokesh

+0

@Lokesh 잘하면 인터넷에 CardLayout 사용에 관한 내용이 전혀 없으며 [StackOverflow] (http://stackoverflow.com/search?q=java+cardlayout)도 없습니다. – Riduidel

0
japplet.setSize (panel.getWidth(), panel.getHeight()); 
japplet.setContentPane (panel); 
panel.revalidate(); 
panel.repaint(); 
japplet.revalidate(); 
japplet.repaint(); 
revalidate() must be called first prior repaint(); 
source : http://stackoverflow.com/questions/10119069/repaint-jpanel-doesnt-work-in-japplet