Open new screen or page on button click (java-eclipse) - java

I have a home page with title and a few buttons I cannot get a new window to open when i click on the button. Here is the code i have for the home page aswell as the class with next screen i am attempting to open trimed for what seems relevant. The NewTicketWindow class is also attached it is plain at the moment. Any help is appreciated.
public class Home
{
private JFrame frame;
JInternalFrame internalFrame;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
Home window = new Home();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Home()
{
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize()
{
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel title1 = new JLabel("City of Murphy");
JLabel title2 = new JLabel("Traffic Ticket Input System");
JButton newTicketButton = new JButton("New Ticket");
newTicketButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
JButton payTicketButton = new JButton("Make a Payment");
JButton reportButtton = new JButton("Ticket Report");
JButton exitButton = new JButton("Exit");
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
}
second class (the screen i want to open upon newticket button being pressed
public class NewTicketWindow extends JFrame
{
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
NewTicketWindow frame = new NewTicketWindow();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public NewTicketWindow()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblEnterNewTicket = new JLabel("Enter New Ticket Information");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
}

just add these lines into your action performed code -
NewTicketWindow frame = new NewTicketWindow();
frame.setVisible(true);

The ActionListener of newTicketButton should create the new frame by calling the constructor of NewTicketWindow (same thing you are doing in the main of NewTicketWindow):
JButton newTicketButton = new JButton("New Ticket");
newTicketButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
NewTicketWindow newTicketWindow = new NewTicketWindow();
newTicketWindow.setVisible(true);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
Also you need to add the newTicketButton to the home window:
frame.add(newTicketButton);

Related

The second JFrame is blank and very small

I'm new to Swing and I'm having trouble replacing an existing JFrame. I initialize the first JFrame without a problem.
class GererAdgerent :
public class GererAdherent extends JFrame {
private JPanel contentPane;
static GererAdherent frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new GererAdherent();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GererAdherent() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnAjouterAdherent = new JButton("Ajouter Adherent");
btnAjouterAdherent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
AjouterAdherent ajouterAdherent = new AjouterAdherent();
ajouterAdherent.setVisible(true);
}
});
btnAjouterAdherent.setBounds(104, 34, 130, 23);
contentPane.add(btnAjouterAdherent);
}
}
But once I try to initialize a different JFRame, I get a blank JFrame without all of it's components (It is created properly when I use AjouterAdherent's main to initialize)
class AjouterAdherent :
public class AjouterAdherent extends JFrame {
JFrame frame;
JTextField txtNom;
static Properties p=new Properties();
static BibliothequeDAORemote proxy;
public static void main(String[] args) throws NamingException {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AjouterAdherent window = new AjouterAdherent();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public AjouterAdherent() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(SystemColor.menu);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextPane txtpnAjouterClient = new JTextPane();
txtpnAjouterClient.setFont(new Font("Tahoma", Font.BOLD, 11));
txtpnAjouterClient.setBackground(SystemColor.menu);
txtpnAjouterClient.setEnabled(false);
txtpnAjouterClient.setEditable(false);
txtpnAjouterClient.setForeground(Color.black);
txtpnAjouterClient.setBounds(175, 11, 89, 20);
txtpnAjouterClient.setText("Ajouter Client");
frame.getContentPane().add(txtpnAjouterClient);
JTextPane txtpnNom = new JTextPane();
txtpnNom.setBackground(SystemColor.menu);
txtpnNom.setEnabled(false);
txtpnNom.setEditable(false);
txtpnNom.setForeground(Color.black);
txtpnNom.setBounds(36, 49, 72, 20);
txtpnNom.setText("Nom");
frame.getContentPane().add(txtpnNom);
}
}
Any help would be greatly appreciated!
(It is created properly when I use AjouterAdherent's main to initialize)
So look at the code that works properly:
AjouterAdherent window = new AjouterAdherent();
window.frame.setVisible(true);
I try to initialize a different JFRame, I get a blank JFrame
And look at the code that doesn't work:
AjouterAdherent ajouterAdherent = new AjouterAdherent();
ajouterAdherent.setVisible(true);
What is the difference?
The problem is your class extends JFrame and also creates a new JFrame, so you have two frame instances, one with components and one without.
Don't extend JFrame! Then you won't cause confusion.
You should only extend a class when you add new functionality to the class. Adding components to the frame is not adding new functionality.

How to add an JPanel from an other class to an existing Frame

I have my MainWindow class where menue buttons and everything else are. In the middle of it is an Panel called content. I want to load JPanels from other classes into this field. But when i start the code below nothing shows up.
MainWindow Class:
public class MainWindow {
private JFrame frame;
private JScrollPane Content;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainWindow() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
JPanel TopPanel = new JPanel();
scrollPane.setColumnHeaderView(TopPanel);
JLabel lblNewLabel = new JLabel("Made by " + Globals.Author);
TopPanel.add(lblNewLabel);
JButton btnHome = new JButton("Home");
TopPanel.add(btnHome);
btnHome.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
Content.add(new Home());
}
});
Content = new JScrollPane();
scrollPane.setViewportView(Content);
}
}
JPanel Class:
public class Home extends JPanelContentTemplate {
/**
* Create the panel.
*/
protected void InitializeComponents(){
setLayout(new BorderLayout(0, 0));
JPanel OptionsMenuePanel = new JPanel();
add(OptionsMenuePanel, BorderLayout.WEST);
JPanel ConentPanel = new JPanel();
add(ConentPanel, BorderLayout.CENTER);
ConentPanel.setLayout(new GridLayout(1, 2, 0, 0));
JLabel lblConnectedWith = new JLabel("Connected With:");
ConentPanel.add(lblConnectedWith);
JTextPane textServerIP = new JTextPane();
ConentPanel.add(textServerIP);
}
#Override
protected void Refresh() {
// TODO Auto-generated method stub
}
}
The InitializeComponents method comes from an Self Created Superclass:
public abstract class JPanelContentTemplate extends JPanel {
/**
* Create the panel.
*/
public JPanelContentTemplate() {
InitializeComponents();
}
protected abstract void InitializeComponents();
protected abstract void Refresh();
}
I also tried an repaint etc.
Thanks for Help
Nothing shows up because you add nothing but empty JScrollPanes to your GUI:
Content = new JScrollPane();
scrollPane.setViewportView(Content);
Here is a simple example of adding separate panels to the MainFrame. I hope it helps you.
public class SidePanel extends JPanel {
private JLabel label;
public SidePanel() {
setBorder(BorderFactory.createEtchedBorder());
label = new JLabel("Hello");
setVisible(true);
/* More Code Goes Here */
}
}
public class CenterPanel extends JPanel {
/* Center Panel Code */
}
public class MainFrame extends JFrame {
private SidePanel sidePanel;
private CenterPanel centerPanel;
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
sidePanel = new SidePanel();
centerPanel = new CenterPanel();
add(sidePanel, BorderLayout.WEST);
add(centerPanel, BorderLayout.CENTER);
setSize(300, 300);
setVisible(true);
}
}
/* Main App */
public static void main(String [] args) {
try {
/* Lambda Expression */
SwingUtitlities.InvokeLater(() -> new MainFrame());
} catch(Exception ex) {
ex.printStackTrace();
}
}

Connecting 2 Jframes with JUNG

I have created 2 JFrames and tying to link two JFrames Windows together. But i couldnt get the content in the 2nd Jframe. Can anyone help me with this?
My first Frame:
public class Main extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnOpenTheJung = new JButton("Open the JUNG window");
btnOpenTheJung.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
**JungLearning1 frame1 = new JungLearning1();
frame1.setVisible(true);
frame1.setSize(600, 400);**
}
});
btnOpenTheJung.setBounds(172, 99, 145, 34);
contentPane.add(btnOpenTheJung);
}}
My second Frame:
public class JungLearning1 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DirectedSparseGraph<String, String> g = new DirectedSparseGraph<String, String>();
g.addVertex("Vertex1");
g.addVertex("Vertex2");
g.addVertex("Vertex3");
g.addEdge("Edge1", "Vertex1", "Vertex2");
g.addEdge("Edge2", "Vertex1", "Vertex3");
g.addEdge("Edge3", "Vertex3", "Vertex1");
VisualizationImageServer<String, String> vs = new VisualizationImageServer<String, String>(
new CircleLayout<String, String>(g), new Dimension(
200, 200));
**JFrame frame1 = new JFrame();
frame1.getContentPane().add(vs);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.pack();
frame1.setVisible(true);**
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JungLearning1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}}
I want to display the 2nd frame along with the content when clicking the butten from the first window

Enable/Disable buttons on main frame from jInternalFrame in Eclipse

As I said in the title, I am writing a code for a small program in java. I used Internal Frames to implement it. The thing is I am trying to add a login option to the program. The login class is a jInternalFrame and I want it to Enable/Disable specific buttons in my main window. I tried google-ing a solution but didn't seem to find it. (Keep in mind that I'm really new to programing so I expect there might be other problems in my code I don't see at the moment).
Main Window code:
import javax.swing.JDesktopPane;
public class FereastraPrincipala extends JFrame {
private static final long serialVersionUID = 1L;
JDesktopPane jdpDesktop;
static int openFrameCount = 0;
public FereastraPrincipala() {
super("Fereastra Principala");
// Make the main window positioned as 50 pixels from each edge of the
// screen.
int inset = 50;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(inset, inset, screenSize.width - inset * 2,screenSize.height - inset * 2);
// Add a Window Exit Listener
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// Create and Set up the GUI.
jdpDesktop = new JDesktopPane();
// A specialized layered pane to be used with JInternalFrames
setContentPane(jdpDesktop);
setJMenuBar(createMenuBar());
// Make dragging faster by setting drag mode to Outline
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
}
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu mnFile = new JMenu("File");
mnFile.setMnemonic(KeyEvent.VK_F);
JMenuItem mntmLogin = new JMenuItem("Login");
mnFile.add(mntmLogin);
mntmLogin.setMnemonic(KeyEvent.VK_N);
menuBar.add(mnFile);
mntmLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
login();
}
});
JMenuItem mntmLogout = new JMenuItem("Logout");
mnFile.add(mntmLogout);
mntmLogout.setEnabled(true);
JMenuItem mntmCreazaUser = new JMenuItem("Creaza User");
mntmCreazaUser.setMnemonic(KeyEvent.VK_C);
mnFile.add(mntmCreazaUser);
mntmCreazaUser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
creazaUser();
}
});
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.setMnemonic(KeyEvent.VK_X);
mnFile.add(mntmExit);
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JMenu mnAdministrator = new JMenu("Administrator");
mnAdministrator.setMnemonic(KeyEvent.VK_N);
menuBar.add(mnAdministrator);
mnAdministrator.setEnabled(true);
JMenu mnPresedinte = new JMenu("Presedinte");
mnPresedinte.setMnemonic(KeyEvent.VK_P);
menuBar.add(mnPresedinte);
mnPresedinte.setEnabled(true);
JMenuItem mnHelp = new JMenuItem("Help");
mnHelp.setMnemonic(KeyEvent.VK_H);
menuBar.add(mnHelp);
mnHelp.setEnabled(true);
mnHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
help();
}
});
return menuBar;
}
protected void login() {
Login frame = new Login();
frame.setVisible(true);
// Every JInternalFrame must be added to content pane using JDesktopPane
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
protected void help() {
Help frame1 = new Help();
frame1.setVisible(true);
jdpDesktop.add(frame1);
try {
frame1.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
protected void creazaUser() {
CreazaUser frame = new CreazaUser();
frame.setVisible(true);
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
FereastraPrincipala frame = new FereastraPrincipala();
frame.setVisible(true);
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
}
Login class code:
import java.awt.EventQueue;
public class Login extends JInternalFrame {
private static Login instance = null;
private JTextField textField;
private JPasswordField passwordField;
public static Login getInstance(){
if(instance == null) {instance = new Login(); }
return instance;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Login() {
super("Login", false, true, false, false);
setVisible(true);
setBounds(200, 200, 290, 173);
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("Username:");
lblNewLabel.setBounds(22, 29, 79, 14);
panel.add(lblNewLabel);
JLabel label = new JLabel("Parola:");
label.setBounds(22, 54, 79, 14);
panel.add(label);
textField = new JTextField();
textField.setColumns(10);
textField.setBounds(123, 26, 98, 20);
panel.add(textField);
passwordField = new JPasswordField();
passwordField.setBounds(123, 51, 98, 20);
panel.add(passwordField);
JButton button = new JButton("Autentificare");
button.setBounds(80, 97, 112, 23);
panel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String user = textField.getText();
String pass = passwordField.getText();
if(user.equals("admin") && pass.equals("admin")) {
FereastraPrincipala(mntmLogout.setEnabled(true));
FereastraPrincipala(mnAdministrator.setEnabled(true));
JOptionPane.showMessageDialog(null,"Login esuat","Login esuat",JOptionPane.ERROR_MESSAGE);
} else if (user.equals("prez") && pass.equals("prez")) {
/* FereastraPrincipala(mntmLogout.setEnabled(true)):
FereastraPrincipala(mnPresedinte.setEnabled(true)); */
JOptionPane.showMessageDialog(null,"Login esuat","Login esuat",JOptionPane.ERROR_MESSAGE);
} else { JOptionPane.showMessageDialog(null,"Login esuat","Login esuat",JOptionPane.ERROR_MESSAGE);}
}
});
}
}

Code runs multiple times

Here's and example code where the code i'm executing runs multiple times.
First class:
public class Test2 extends JFrame {
public static int asd = 0;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test2 frame = new Test2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnShowtest = new JButton("ShowTest2");
btnShowtest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Test1 a1 = new Test1();
a1.setVisible(true);
dispose();
}
});
contentPane.add(btnShowtest, BorderLayout.CENTER);
}
}
When button is pressed this one opens:
public class Test1 extends JFrame {
public static int ab = 1;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test1 frame = new Test1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test1() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnrun = new JButton("runt");
btnrun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Test3.error();
}
});
contentPane.add(btnrun, BorderLayout.CENTER);
}
}
When button is pressed it executes this code:
public class Test3 {
public static void error(){
JOptionPane.showMessageDialog(null, "error");
Test2.asd += 1;
System.err.print(Test2.asd);
final Frame[] frames = Frame.getFrames();
for (Frame f : frames){
f.dispose();
Test2 newtet = new Test2();
newtet.setVisible(true);
}
}
}
Now every time I press the buttons again it executes Test3.error(); multiple times.
For example if I press the buttons 10 times then Test3.error(); runs 10 times.
I'm guessing it's a simple fix but I can't figure it out.
Edit:
When i press the btnrun button it runs the "Test3.error()" Code, then closes all frames and creates another main frame.
When i get back to btnrun and press it again, it runs "Test3.error()" 2 times instead of once. 2 JOptionePanes and creates new mainframe twice.
If i do it again it runs "Test3.error()" 3 times and so on it keeps doing that.
What i want is for it to always run "Test3.error()" just once but for some reason it doesn't.
Another example:
public class Frame1 extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 frame = new Frame1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Frame1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnRun = new JButton("Run");
btnRun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Run.run();
}
});
contentPane.add(btnRun, BorderLayout.CENTER);
}
}
And the code that runs:
public class Run {
public static void run(){
final Frame[] frames = Frame.getFrames();
for (Frame f : frames){
f.dispose();
Frame1 newtet = new Frame1();
newtet.setVisible(true);
}
}
}
Same problem.
Everytime i press the run button, it executes the code as many times as i've pressed it in the past.
Press button once it diposes Frame1 and recreates Frame1.
Press it again and it executes the code 2 times in a row.
(disposes Frame1, creates Frame1, diposes Frame1 and creates Frame1)
Yes, you've added an action listener here:
JButton btnrun = new JButton("runt");
btnrun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Test3.error();
}
});
That means "When the button is clicked, call Test3.error()." Were you expecting the action listener to be removed automatically after executing once?
EDIT: Okay, I think I've worked out what's going on. This is the problem:
final Frame[] frames = Frame.getFrames();
for (Frame f : frames){
f.dispose();
Test2 newtet = new Test2();
newtet.setVisible(true);
}
For every existing frame, you're disposing that frame and creating a new Test2(). So if you had 3 frames on the screen (a Test1, a Test2 and a Test3) when you click the button, you'll end up with three new Test2 frames. I suspect you didn't mean to do that. Did you mean this instead?
for (Frame f : Frame.getFrames()){
f.dispose();
}
Test2 newtet = new Test2();
newtet.setVisible(true);

Categories