Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.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上的jButton_Java_Swing_Focus_Jbutton_Netbeans 7 - Fatal编程技术网

Java 自动对焦jpanel上的jButton

Java 自动对焦jpanel上的jButton,java,swing,focus,jbutton,netbeans-7,Java,Swing,Focus,Jbutton,Netbeans 7,我在JFrame上加载了JPanel。JPanel包含4个JButton。我试图做的事情是在第一个按钮上自动获得焦点,这样我就可以用键盘在两个按钮之间切换 我尝试了jButton1.requestFocusInWindow()jpanel构造函数中的代码,但它仍然不起作用。有什么我遗漏的吗?我还能做什么 编辑: 该项目包含3个java文件,其代码如下: NewJFrame.java /* * To change this template, choose Tools | Templates

我在
JFrame
上加载了
JPanel
。JPanel包含4个JButton。我试图做的事情是在第一个按钮上自动获得焦点,这样我就可以用键盘在两个按钮之间切换

我尝试了
jButton1.requestFocusInWindow()jpanel构造函数中的代码,但它仍然不起作用。有什么我遗漏的吗?我还能做什么

编辑: 该项目包含3个java文件,其代码如下:

NewJFrame.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sampleui;

import java.awt.Container;
import java.awt.Toolkit;
import javax.swing.JRootPane;
import javax.swing.UIManager;

/**
 *
 * @author Administrator
 */
public class NewJFrame extends javax.swing.JFrame {

static Container container;

/**
 * Creates new form NewJFrame
 */
public NewJFrame() {
    initComponents();
    rootPane= getRootPane();
    container=getContentPane();
    setSize(Toolkit.getDefaultToolkit().getScreenSize());
    setVisible(true);

}

/**
 * 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() {

    jMenuItem1 = new javax.swing.JMenuItem();
    jPanel1 = new javax.swing.JPanel();
    jLabel2 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    jLabel1 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jPanel2 = new javax.swing.JPanel();
    jLabel7 = new javax.swing.JLabel();
    jLabel8 = new javax.swing.JLabel();
    jLabel9 = new javax.swing.JLabel();
    jDesktopPane1 = new javax.swing.JDesktopPane();

    jMenuItem1.setText("jMenuItem1");

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("This is the Title");

    jPanel1.setBackground(new java.awt.Color(204, 204, 204));
    jPanel1.setLayout(new java.awt.BorderLayout());

    jLabel2.setForeground(new java.awt.Color(255, 255, 255));
    jPanel1.add(jLabel2, java.awt.BorderLayout.PAGE_START);

    jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    jLabel5.setForeground(new java.awt.Color(51, 51, 51));
    jLabel5.setText("dfgdfgdfg");
    jPanel1.add(jLabel5, java.awt.BorderLayout.LINE_START);

    jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    jLabel6.setText("dfgdfgdfg");
    jPanel1.add(jLabel6, java.awt.BorderLayout.LINE_END);

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText("dfgdfgfdgfdgfdg");
    jPanel1.add(jLabel1, java.awt.BorderLayout.CENTER);

    jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel3.setText("jLabel3");
    jPanel1.add(jLabel3, java.awt.BorderLayout.PAGE_END);

    getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

    jPanel2.setBackground(new java.awt.Color(204, 204, 204));
    jPanel2.setLayout(new java.awt.BorderLayout());

    jLabel7.setText("jLabel7");
    jPanel2.add(jLabel7, java.awt.BorderLayout.LINE_START);

    jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
    jLabel8.setText("jLabel8");
    jPanel2.add(jLabel8, java.awt.BorderLayout.LINE_END);

    jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel9.setText("jLabel9");
    jPanel2.add(jLabel9, java.awt.BorderLayout.CENTER);

    getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

    jDesktopPane1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            jDesktopPane1MouseClicked(evt);
        }
    });
    getContentPane().add(jDesktopPane1, java.awt.BorderLayout.CENTER);

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

private void jDesktopPane1MouseClicked(java.awt.event.MouseEvent evt) {                                           
    // TODO add your handling code here:

    container=getContentPane();
    setSize(Toolkit.getDefaultToolkit().getScreenSize());
    setVisible(true);

    //callCompanyOption();

    container.setVisible(false);
    jDesktopPane1.setVisible(false);
    NewJPanel p=new NewJPanel();
    //p.setBounds(40, 30, 1200, 786);
    container.add(p);
    container.setVisible(true);

}                                          


/**
 * @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
        }
    });
}



// Variables declaration - do not modify                     
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
// End of variables declaration                   
}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装样品;
导入java.awt.Container;
导入java.awt.Toolkit;
导入javax.swing.JRootPane;
导入javax.swing.UIManager;
/**
*
*@作者管理员
*/
公共类NewJFrame扩展了javax.swing.JFrame{
静态容器;
/**
*创建新表单NewJFrame
*/
公共NewJFrame(){
初始化组件();
rootPane=getRootPane();
container=getContentPane();
setSize(Toolkit.getDefaultToolkit().getScreenSize());
setVisible(真);
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
jMenuItem1=newjavax.swing.JMenuItem();
jPanel1=newjavax.swing.JPanel();
jLabel2=newjavax.swing.JLabel();
jLabel5=newjavax.swing.JLabel();
jLabel6=newjavax.swing.JLabel();
jLabel1=newjavax.swing.JLabel();
jLabel3=newjavax.swing.JLabel();
jPanel2=newjavax.swing.JPanel();
jLabel7=newjavax.swing.JLabel();
jLabel8=newjavax.swing.JLabel();
jLabel9=newjavax.swing.JLabel();
jDesktopPane1=newjavax.swing.JDesktopPane();
setText(“jMenuItem1”);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle(“这是标题”);
setBackground(新java.awt.Color(204204204204));
setLayout(新java.awt.BorderLayout());
setForeground(新java.awt.Color(255、255、255));
jPanel1.add(jLabel2,java.awt.BorderLayout.PAGE_START);
jLabel5.setFont(新的java.awt.Font(“Tahoma”,1,14));//NOI18N
jLabel5.set前台(新java.awt.Color(51,51,51));
jLabel5.setText(“dfgdfgdfg”);
jPanel1.add(jLabel5、java.awt.BorderLayout.LINE_START);
jLabel6.setFont(新的java.awt.Font(“Tahoma”,1,14));//NOI18N
jLabel6.setText(“dfgdfgdfg”);
jPanel1.add(jLabel6,java.awt.BorderLayout.LINE_END);
jLabel1.setFont(新的java.awt.Font(“Tahoma”,1,14));//NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText(“dfgdfgfdgfdgfdg”);
add(jLabel1,java.awt.BorderLayout.CENTER);
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel3.setText(“jLabel3”);
jPanel1.add(jLabel3,java.awt.BorderLayout.PAGE_END);
getContentPane().add(jPanel1,java.awt.BorderLayout.PAGE_START);
setBackground(新java.awt.Color(204204204204));
setLayout(新java.awt.BorderLayout());
jLabel7.setText(“jLabel7”);
jPanel2.add(jLabel7,java.awt.BorderLayout.LINE_START);
jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.training);
jLabel8.setText(“jLabel8”);
jPanel2.add(jLabel8,java.awt.BorderLayout.LINE_END);
jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel9.setText(“jLabel9”);
add(jLabel9,java.awt.BorderLayout.CENTER);
getContentPane().add(jPanel2,java.awt.BorderLayout.PAGE_END);
jDesktopPane1.addMouseListener(新java.awt.event.MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent evt){
jDesktopPane1MouseClicked(evt);
}
});
getContentPane().add(jDesktopPane1,java.awt.BorderLayout.CENTER);
包装();
}//                         
私有void jDesktopPane1MouseClicked(java.awt.event.MouseEvent evt){
//TODO在此处添加您的处理代码:
container=getContentPane();
setSize(Toolkit.getDefaultToolkit().getScreenSize());
setVisible(真);
//callCompanyOption();
container.setVisible(false);
jDesktopPane1.setVisible(false);
NewJPanel p=newnewjpanel();
//p、 立根(40,30,1200,786);
容器。添加(p);
container.setVisible(true);
}                                          
/**
*@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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sampleui;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.KeyEvent;

/**
 *
 * @author Administrator
 */
public class NewJPanel extends javax.swing.JPanel {


/**
 * Creates new form NewJPanel
 */
public NewJPanel() {
    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() {

    jPanel2 = new javax.swing.JPanel();
    jPanel1 = new javax.swing.JPanel();
    jButton2 = new javax.swing.JButton();
    jXMonthView1 = new org.jdesktop.swingx.calendar.JXMonthView();

    setLayout(new java.awt.GridBagLayout());

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );

    add(jPanel2, new java.awt.GridBagConstraints());

    jButton2.setText("OK");
    jButton2.setNextFocusableComponent(jButton2);
    jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            jButton2MouseClicked(evt);
        }
    });
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });
    jButton2.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            jButton2KeyPressed(evt);
        }
    });

    jXMonthView1.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            jXMonthView1KeyPressed(evt);
        }
    });

    javax.swing.GroupLayout jXMonthView1Layout = new javax.swing.GroupLayout(jXMonthView1);
    jXMonthView1.setLayout(jXMonthView1Layout);
    jXMonthView1Layout.setHorizontalGroup(
        jXMonthView1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );
    jXMonthView1Layout.setVerticalGroup(
        jXMonthView1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 160, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(64, 64, 64)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                .addComponent(jXMonthView1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap(56, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(19, 19, 19)
            .addComponent(jXMonthView1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );

    add(jPanel1, new java.awt.GridBagConstraints());
}// </editor-fold>

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

            jPanel1.setVisible(false);
            NewJFrame.container.remove(NewJPanel.this);
            NewJPanel1 p=new NewJPanel1();
            NewJFrame.container.add(p);


}                                     

private void jButton2KeyPressed(java.awt.event.KeyEvent evt) {                                    
    // TODO add your handling code here:
    int key=evt.getKeyCode();
    if(key==KeyEvent.VK_ENTER)
    {
        System.out.println("Enter Pressed");
        jPanel1.setVisible(false);
        NewJFrame.container.remove(NewJPanel.this);
        NewJPanel1 p=new NewJPanel1();
        NewJFrame.container.add(p);
    }
}                                   

private void jXMonthView1KeyPressed(java.awt.event.KeyEvent evt) {                                        
    // TODO add your handling code here:
    jButton2.requestFocus();
}                                       

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    jPanel1.setVisible(false);
        NewJFrame.container.remove(NewJPanel.this);
        NewJPanel1 p=new NewJPanel1();
        NewJFrame.container.add(p);
}


// Variables declaration - do not modify
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private org.jdesktop.swingx.calendar.JXMonthView jXMonthView1;
// End of variables declaration
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 package sampleui;

 import java.awt.Window;
 import java.awt.event.KeyEvent;
 import javax.swing.JRootPane;

 /**
 *
 * @author Administrator
 */
 public class NewJPanel1 extends javax.swing.JPanel {


//=new NewJFrame();
/**
 * Creates new form NewJPanel1
 */
public NewJPanel1() {
    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() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jButton5 = new javax.swing.JButton();

    setRequestFocusEnabled(false);
    addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            formKeyPressed(evt);
        }
    });
    setLayout(new java.awt.GridBagLayout());

    jPanel1.setNextFocusableComponent(jButton5);
    jPanel1.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            jPanel1KeyPressed(evt);
        }
        public void keyReleased(java.awt.event.KeyEvent evt) {
            jPanel1KeyReleased(evt);
        }
    });

    jButton1.setText("back");
    jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            jButton1MouseClicked(evt);
        }
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            jButton1MouseEntered(evt);
        }
    });
    jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            jButton1KeyPressed(evt);
        }
    });

    jButton2.setText("jButton2");
    jButton2.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            jButton2KeyPressed(evt);
        }
    });

    jButton3.setText("jButton3");
    jButton3.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            jButton3KeyPressed(evt);
        }
    });

    jButton4.setText("jButton4");
    jButton4.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            jButton4KeyPressed(evt);
        }
    });

    jButton5.setText("jButton5");
    jButton5.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    jButton5.setFocusCycleRoot(true);
    jButton5.setFocusTraversalPolicyProvider(true);
    jButton5.setInheritsPopupMenu(true);
    jButton5.setNextFocusableComponent(jButton4);
    jButton5.setVerifyInputWhenFocusTarget(false);
    jButton5.requestFocusInWindow();
    jButton5.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton5ActionPerformed(evt);
        }
    });
    jButton5.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            jButton5KeyPressed(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(41, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(38, 38, 38))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jButton5)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jButton4)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jButton3)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jButton2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jButton1)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    jButton5.getAccessibleContext().setAccessibleParent(this);

    add(jPanel1, new java.awt.GridBagConstraints());
    //jButton1.requestFocusInWindow();
    //NewJFrame.getRootPane().setDefaultButton(jButton1);
    //jButton1.requestFocus();
}// </editor-fold>





private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    // TODO add your handling code here:
    jPanel1.setVisible(false);
    NewJFrame.container.remove(NewJPanel1.this);
    NewJPanel p=new NewJPanel();
    NewJFrame.container.add(p);
}                                     

private void jPanel1KeyPressed(java.awt.event.KeyEvent evt) {                                   
    // TODO add your handling code here:

    System.out.println("Key Pressed");

}                                  

private void jButton5KeyPressed(java.awt.event.KeyEvent evt) {                                    
    // TODO add your handling code here:
    int key=evt.getKeyCode();
    if(key==KeyEvent.VK_ESCAPE)
    {
        System.out.println("Escape Pressed");
        jPanel1.setVisible(false);
            NewJFrame.container.remove(NewJPanel1.this);
            NewJPanel p=new NewJPanel();
            NewJFrame.container.add(p);
    }
}                                   

private void jButton4KeyPressed(java.awt.event.KeyEvent evt) {                                    
    // TODO add your handling code here:
    int key=evt.getKeyCode();
    if(key==KeyEvent.VK_ESCAPE)
    {
        System.out.println("Escape Pressed");
        jPanel1.setVisible(false);
            NewJFrame.container.remove(NewJPanel1.this);
            NewJPanel p=new NewJPanel();
            NewJFrame.container.add(p);
    }
}                                   

private void jButton3KeyPressed(java.awt.event.KeyEvent evt) {                                    
    // TODO add your handling code here:
    int key=evt.getKeyCode();
    if(key==KeyEvent.VK_ESCAPE)
    {
        System.out.println("Escape Pressed");
        jPanel1.setVisible(false);
            NewJFrame.container.remove(NewJPanel1.this);
            NewJPanel p=new NewJPanel();
            NewJFrame.container.add(p);
    }
}                                   

private void jButton2KeyPressed(java.awt.event.KeyEvent evt) {                                    
    // TODO add your handling code here:
    int key=evt.getKeyCode();
    if(key==KeyEvent.VK_ESCAPE)
    {
        System.out.println("Escape Pressed");
        jPanel1.setVisible(false);
            NewJFrame.container.remove(NewJPanel1.this);
            NewJPanel p=new NewJPanel();
            NewJFrame.container.add(p);
    }
}                                   

private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {                                    
    // TODO add your handling code here:
    int key=evt.getKeyCode();
    if(key==KeyEvent.VK_ESCAPE||key==KeyEvent.VK_ENTER)
    {
            jPanel1.setVisible(false);
            NewJFrame.container.remove(NewJPanel1.this);
            NewJPanel p=new NewJPanel();
            NewJFrame.container.add(p);
    }
}                                   

private void jButton1MouseEntered(java.awt.event.MouseEvent evt) {                                      
    // TODO add your handling code here:

}                                     

private void formKeyPressed(java.awt.event.KeyEvent evt) {                                
    // TODO add your handling code here:
    System.out.println("key pressed");
}                               

private void jPanel1KeyReleased(java.awt.event.KeyEvent evt) {                                    
    // TODO add your handling code here:
}                                   

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
}                                        


// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
public static javax.swing.JPanel jPanel1;
// End of variables declaration
public NewJFrame() {
    initComponents();
    rootPane= getRootPane();
    container=getContentPane();
    setSize(Toolkit.getDefaultToolkit().getScreenSize());
    setVisible(true);

    //added code:
    JButton jbutton5 = //get from NewJPanel1
    getRootPane().setDefaultButton(jbutton5);
    jbutton5.requestFocus();
}
public JButton getDefaultButton () {
    return jButton5;
}
jButton1.requestFocus();
public NewJFrame() {
    initComponents();
    //some methods ommitted
    setVisible(true);

    //change to NewJPanel1

    //request focus after displaying the NewJPanel1
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    jPanel1.setVisible(false);
    NewJFrame.container.remove(NewJPanel.this);
    NewJPanel1 p=new NewJPanel1();
    NewJFrame.container.add(p);

    p.initFocus();
}
public void initFocus() {
    jButton5.requestFocus();
}
public NewJPanel1() {
    initComponents();
}
public NewJPanel1() {
    initComponents();
    addComponentListener(new ComponentListener() {

        @Override
        public void componentResized(ComponentEvent ce) {}

        @Override
        public void componentMoved(ComponentEvent ce) {}

        @Override
        public void componentShown(ComponentEvent ce) {
            jButton1.requestFocus();
        }

        @Override
        public void componentHidden(ComponentEvent ce) {}
    });
}
import java.awt.AWTKeyStroke;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.util.Set;
import java.util.HashSet;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class FocusFrame extends JFrame
{
    JButton lowerButton;
    JButton upperButton;
    JPanel centerPanel;

    public void prepareAndShowGUI()
    {
        centerPanel = new FocusPanel();
        lowerButton = new JButton("Lower Button");
        upperButton = new JButton("Click here to hide/show center Panel");
        Container container = getContentPane();
        container.add(centerPanel,BorderLayout.CENTER);
        container.add(lowerButton,BorderLayout.SOUTH);
        container.add(upperButton,BorderLayout.NORTH);
        upperButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                centerPanel.setVisible(!centerPanel.isVisible());
            }
        });
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String[] st)
    {
        SwingUtilities.invokeLater( new Runnable()
        {
            @Override
            public void run()
            {
                FocusFrame fFrame = new FocusFrame();
                fFrame.prepareAndShowGUI();
            }
        });
    }
}

class FocusPanel extends JPanel implements ComponentListener
{
    int SIZE = 10;
    JButton[] buttons;
    public FocusPanel()
    {
        createAndAttachGUI();
    }
    private void createAndAttachGUI()
    {
        buttons = new JButton[SIZE];
        setLayout(new GridLayout(5,5));
        for (int i = 0 ; i < SIZE ; i++ )
        {
            buttons[i] = new JButton("Button " + (i+1));
            buttons[i].setFocusTraversalKeysEnabled(true);
            add(buttons[i]);
        }
        //For Forward Focus Traversal
        Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>(this.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
        set.add(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0));
        setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,set);
        //For Backward Focus Traversal
        set = new HashSet<AWTKeyStroke>(this.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
        set.add(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0));
        setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,set);
        addComponentListener(this);
    }
    @Override
    public void componentHidden(ComponentEvent e)
    {}
    @Override
    public void componentMoved(ComponentEvent e)
    {
        buttons[0].requestFocus();
    }
    @Override
    public void componentResized(ComponentEvent e)
    {}
    @Override
    public void componentShown(ComponentEvent e)
    {
        buttons[0].requestFocus();
    }
}