Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用多个外部jpanel的带netbeans的CardLayout_Java_Swing_Netbeans_Cardlayout_Gui Builder - Fatal编程技术网

Java 使用多个外部jpanel的带netbeans的CardLayout

Java 使用多个外部jpanel的带netbeans的CardLayout,java,swing,netbeans,cardlayout,gui-builder,Java,Swing,Netbeans,Cardlayout,Gui Builder,我想在JFrame表单中使用CardLayout交换JPanel 这是JFrame表单的代码: public class Login extends javax.swing.JFrame { /** * Creates new form Login */ public Login() { initComponents(); } /** * This method is called from within the constructor to initialize the f

我想在JFrame表单中使用CardLayout交换JPanel 这是JFrame表单的代码:

public class Login extends javax.swing.JFrame {

/**
 * Creates new form Login
 */

public Login() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Login().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
// End of variables declaration                   
公共类登录扩展了javax.swing.JFrame{
/**
*创建新表单登录
*/
公共登录(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
// //
私有组件(){
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,400,短。最大值)
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,300,短。最大值)
);
包装();
}//                         
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新登录().setVisible(true);
}
});
}
//变量声明-不修改
//变量结束声明
}

JFrame启动时要加载的第一个JPanel是:

public class LoginPanel extends javax.swing.JPanel {
private Font original;

/**
 * Creates new form LoginPanel
 */



public LoginPanel() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    Email = new javax.swing.JLabel();
    emailField = new javax.swing.JTextField();
    login = new javax.swing.JButton();
    password = new javax.swing.JLabel();
    passwordField = new javax.swing.JPasswordField();
    register = new javax.swing.JLabel();
    background = new javax.swing.JLabel();

    setLayout(null);

    Email.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    Email.setText("Email address");
    add(Email);
    Email.setBounds(155, 115, 90, 14);

    emailField.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            emailFieldMouseEntered(evt);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            emailFieldMouseExited(evt);
        }
    });
    add(emailField);
    emailField.setBounds(115, 135, 170, 20);

    login.setText("Login");
    add(login);
    login.setBounds(156, 240, 90, 23);

    password.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    password.setText("Password");
    add(password);
    password.setBounds(155, 170, 90, 14);
    add(passwordField);
    passwordField.setBounds(115, 191, 170, 20);

    register.setForeground(new java.awt.Color(0, 0, 255));
    register.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    register.setText("Don't have an account? Register!");
    register.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            registerMouseClicked(evt);
        }
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            registerMouseEntered(evt);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            registerMouseExited(evt);
        }
    });
    add(register);
    register.setBounds(115, 220, 170, 14);

    background.setIcon(new javax.swing.ImageIcon("C:\\Users\\Matteo\\Desktop\\Netbeans Project\\Social_Network\\background.png")); // NOI18N
    background.setText("jLabel1");
    add(background);
    background.setBounds(0, 0, 400, 300);
}// </editor-fold>                        

private void registerMouseClicked(java.awt.event.MouseEvent evt) {                                      

}                                     

private void registerMouseEntered(java.awt.event.MouseEvent evt) {                                      
    original = register.getFont();
    Map attributes = original.getAttributes();
    attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
    register.setFont(original.deriveFont(attributes));
    Cursor cursor = Cursor.getDefaultCursor();
    cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
    setCursor(cursor);
}                                     

private void registerMouseExited(java.awt.event.MouseEvent evt) {                                     
    register.setFont(original);
    Cursor cursor = Cursor.getDefaultCursor();
    setCursor(cursor);
}                                    

private void emailFieldMouseEntered(java.awt.event.MouseEvent evt) {                                        
    Cursor cursor = Cursor.getDefaultCursor();
    cursor = Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR);
    setCursor(cursor);
}                                       

private void emailFieldMouseExited(java.awt.event.MouseEvent evt) {                                       
   Cursor cursor = Cursor.getDefaultCursor();
   setCursor(cursor);
}                                      


// Variables declaration - do not modify                     
private javax.swing.JLabel Email;
private javax.swing.JLabel background;
private javax.swing.JTextField emailField;
private javax.swing.JButton login;
private javax.swing.JLabel password;
private javax.swing.JPasswordField passwordField;
private javax.swing.JLabel register;
// End of variables declaration                   
公共类LoginPanel扩展了javax.swing.JPanel{
私人字体原件;
/**
*创建新表单LoginPanel
*/
公共登录面板(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
Email=newjavax.swing.JLabel();
emailField=newjavax.swing.JTextField();
login=newjavax.swing.JButton();
password=newjavax.swing.JLabel();
passwordField=newjavax.swing.JPasswordField();
register=newjavax.swing.JLabel();
background=newjavax.swing.JLabel();
setLayout(空);
setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
Email.setText(“电子邮件地址”);
添加(电子邮件);
电子邮件。挫折(155、115、90、14);
addMouseListener(新java.awt.event.MouseAdapter(){
public void mouseenterned(java.awt.event.MouseEvent evt){
emailFieldMouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt){
emailFieldMouseExited(evt);
}
});
添加(电子邮件字段);
emailField.setBounds(11513517020);
login.setText(“login”);
添加(登录);
login.setBounds(156、240、90、23);
setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
password.setText(“密码”);
添加(密码);
密码.setBounds(155170,90,14);
添加(密码字段);
passwordField.setBounds(11519117020);
register.set前台(新java.awt.Color(0,0255));
register.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
register.setText(“没有帐户?注册!”);
register.addMouseListener(新java.awt.event.MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent evt){
注册经理(evt);
}
public void mouseenterned(java.awt.event.MouseEvent evt){
登记入户(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt){
注册房屋退出(evt);
}
});
添加(注册);
登记册.立根(115、220、170、14);
background.setIcon(new javax.swing.ImageIcon(“C:\\Users\\Matteo\\Desktop\\Netbeans Project\\Social\u Network\\background.png”);//NOI18N
setText(“jLabel1”);
添加(背景);
背景.立根(0,0,400,300);
}//                         
私有无效注册表鼠标单击(java.awt.event.MouseEvent evt){
}                                     
私有无效注册表mouseentered(java.awt.event.MouseEvent evt){
original=register.getFont();
Map attributes=original.getAttributes();
attributes.put(texttribute.UNDERLINE,texttribute.UNDERLINE_ON);
register.setFont(original.deriveFont(属性));
Cursor Cursor=Cursor.getDefaultCursor();
cursor=cursor.getPredefinedCursor(cursor.HAND\u cursor);
设置游标(游标);
}                                     
私有无效注册表已退出(java.awt.event.MouseEvent
public class RegisterPanel extends javax.swing.JPanel {

/**
 * Creates new form RegisterPanel
 */
public RegisterPanel() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jTextField1 = new javax.swing.JTextField();
    jTextField2 = new javax.swing.JTextField();
    jTextField4 = new javax.swing.JTextField();
    jPasswordField1 = new javax.swing.JPasswordField();
    register = new javax.swing.JButton();
    cancel = new javax.swing.JButton();
    Name = new javax.swing.JLabel();
    LastName = new javax.swing.JLabel();
    Email = new javax.swing.JLabel();
    Password = new javax.swing.JLabel();
    icon = new javax.swing.JLabel();
    jPasswordField2 = new javax.swing.JPasswordField();
    repeatPassword = new javax.swing.JLabel();
    jLabel1 = new javax.swing.JLabel();

    setToolTipText("");
    setPreferredSize(new java.awt.Dimension(400, 300));
    setLayout(null);
    add(jTextField1);
    jTextField1.setBounds(110, 60, 240, 20);
    add(jTextField2);
    jTextField2.setBounds(110, 100, 240, 20);
    add(jTextField4);
    jTextField4.setBounds(110, 140, 240, 20);
    add(jPasswordField1);
    jPasswordField1.setBounds(110, 180, 240, 20);

    register.setIcon(new javax.swing.ImageIcon("C:\\Users\\Matteo\\Desktop\\Netbeans Project\\Social_Network\\cancel.png")); // NOI18N
    register.setText("Cancel");
    register.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            registerMouseClicked(evt);
        }
    });
    add(register);
    register.setBounds(240, 260, 90, 25);

    cancel.setIcon(new javax.swing.ImageIcon("C:\\Users\\Matteo\\Desktop\\Netbeans Project\\Social_Network\\confirm.png")); // NOI18N
    cancel.setText("Register");
    add(cancel);
    cancel.setBounds(130, 260, 93, 25);

    Name.setText("Name");
    add(Name);
    Name.setBounds(20, 60, 27, 14);

    LastName.setText("Last name");
    add(LastName);
    LastName.setBounds(20, 100, 60, 14);

    Email.setText("Email address");
    add(Email);
    Email.setBounds(20, 140, 70, 14);

    Password.setText("Password");
    add(Password);
    Password.setBounds(20, 180, 50, 14);

    icon.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
    icon.setIcon(new javax.swing.ImageIcon("C:\\Users\\Matteo\\Desktop\\Netbeans Project\\Social_Network\\addUser.png")); // NOI18N
    icon.setText("     Registration user");
    add(icon);
    icon.setBounds(20, 10, 180, 40);
    add(jPasswordField2);
    jPasswordField2.setBounds(110, 220, 240, 20);

    repeatPassword.setText("Repeat password");
    add(repeatPassword);
    repeatPassword.setBounds(20, 220, 90, 14);

    jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Matteo\\Desktop\\Netbeans Project\\Social_Network\\background.png")); // NOI18N
    jLabel1.setText("jLabel1");
    add(jLabel1);
    jLabel1.setBounds(0, 0, 400, 300);
}// </editor-fold>                        

private void registerMouseClicked(java.awt.event.MouseEvent evt) {                                      

}                                     


// Variables declaration - do not modify                     
private javax.swing.JLabel Email;
private javax.swing.JLabel LastName;
private javax.swing.JLabel Name;
private javax.swing.JLabel Password;
private javax.swing.JButton cancel;
private javax.swing.JLabel icon;
private javax.swing.JLabel jLabel1;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JPasswordField jPasswordField2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField4;
private javax.swing.JButton register;
private javax.swing.JLabel repeatPassword;
// End of variables declaration                   
public interface Switchable {
    void show(String card);
}

public class Login extends JFrame implements Switchable {
    LoginPanel panel = new LoginPanel(this);

    @Override
    public void show(String card) {
        CardLayout layout = (CardLayout)getLayout();
        layout.show(card);
    }
}

public class LoginPanel extends JPanel {
    private Switchable switcher;

    public LoginPanel(Switchable switcher) {
        this.switcher = switcher;
    }

    private void buttonActionPerformed(ActionEvent e) {
        switcher.show(...);
    }
}