travelling between frames
I am traversing b/w frames, say, Frame1, Frame2, Frame3.
I click the button on Frame1 to move to Frame2 and so on......
and return back to the previous frames upon pressing "esc" key.
Frame1::
Code:
public class Frame1 extends javax.swing.JFrame {
[COLOR="Red"] protected static Frame1 f1 = new Frame1();
protected static Frame2 f2;[/COLOR]
public Frame1() {
initComponents();
}
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jButton1)
.addContainerGap(226, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(130, 130, 130)
.addComponent(jButton1)
.addContainerGap(147, Short.MAX_VALUE))
);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
[COLOR="red"] f2 = new Frame2();
f2.setBounds(100, 100, 500, 500);
f2.setVisible(true);
setVisible(false);[/COLOR]
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
f1.setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
}
Frame2::
Code:
import java.awt.event.*;
import java.awt.*;
public class Frame2 extends javax.swing.JFrame {
protected static Frame3 f3;
[COLOR="Blue"] KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
[/COLOR]
public Frame2() {
initComponents();
manager.addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
if (e.getKeyCode() == 27) {
[COLOR="red"] Frame1.f1.setVisible(true);
setVisible(false);[/COLOR]
}
}
return false;
}
});
}
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(124, 124, 124)
.addComponent(jButton1)
.addContainerGap(203, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(104, 104, 104)
.addComponent(jButton1)
.addContainerGap(173, Short.MAX_VALUE))
);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
[COLOR="red"] f3 = new Frame3();
f3.setBounds(200, 200, 500, 500);
f3.setVisible(true);
setVisible(false);[/COLOR]
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame2().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
}
Frame3::
Code:
import java.awt.event.*;
import java.awt.*;
public class Frame3 extends javax.swing.JFrame {
[COLOR="blue"] KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManag[/COLOR]er();
public Frame3() {
initComponents();
manager.addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
if (e.getKeyCode() == 27) {
[COLOR="red"] Frame1.f2.setVisible(true);
setVisible(false);
[/COLOR] }
}
return false;
}
});
}
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();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame3().setVisible(true);
}
});
}
}
the problem is that when i traverse from the Frame3 to Frame2.. both the frames ie., Frame1 and Frame2 come to visibility..
I guess there is some problem with the design... But I can't figure it out..
please help:confused: