Question
Hello, Everytime I press the continue and back button the previous frame never closes. If I select the continue button, Im taken to the next
Hello,
Everytime I press the continue and back button the previous frame never closes. If I select the continue button, Im taken to the next frame but the previous frame is still there How can I just have the active frame displayed at all times.
Below is the code.
package jframe;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
//create class and extend with JFrame
public class jrrame1 extends JFrame
{
//declare variable
private JPanel contentPane;
/**
* Launch the application.
*/
//main method
public static void main(String[] args)
{
/* It posts an event (Runnable)at the end of Swings event list and is
processed after all other GUI events are processed.*/
EventQueue.invokeLater(new Runnable()
{
public void run()
{
//try - catch block
try
{
//Create object of OldWindow
jrrame1 frame = new jrrame1();
//set frame visible true
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public jrrame1()//constructor
{
//set default close operation
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//set bounds of the frame
setBounds(100, 100, 450, 300);
//create object of JPanel
contentPane = new JPanel();
//set border
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//set ContentPane
setContentPane(contentPane);
//set null
contentPane.setLayout(null);
//create object of JButton and set label on it
JButton btnNewFrame = new JButton("Continue");
//add actionListener
btnNewFrame.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
//call the object of NewWindow and set visible true
jframe2 frame = new jframe2();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//jrrame.setVisible(true);
//set default close operation
}
});
//set font of the Button
btnNewFrame.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));
//set bounds of the Button
btnNewFrame.setBounds(143, 195, 116, 25);
//add Button into contentPane
contentPane.add(btnNewFrame);
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"Test1", "Test2"}));
comboBox.setBounds(93, 106, 248, 27);
contentPane.add(comboBox);
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JComboBox DeviceCB = (JComboBox) event.getSource();
Object selectDevice = DeviceCB.getSelectedItem();
if(selectDevice.toString().equals("Test 1"))
{
}
}
});
}
}
--------------
Other class
package jframe;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import jframe.jrrame1;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
//create class and extend with JFrame
public class jframe2 extends jrrame1
{
//declare variable
private JPanel contentPane;
/**
* Launch the application.
*/
//main method
/**
* Create the frame.
*/
public jframe2() //constructor
{
//set default close operation
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//set bounds of the frame
setBounds(100, 100, 450, 300);
//create object of JPanel
contentPane = new JPanel();
//set border
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//set ContentPane
setContentPane(contentPane);
//set null
contentPane.setLayout(null);
//create object of JButton and set label on it
JButton btnNewFrame = new JButton("Back");
//add actionListener
btnNewFrame.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
//call the object of NewWindow and set visible true
jrrame1 frame = new jrrame1();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//jrrame.setVisible(true);
//set default close operation
}
});
//set font of the Button
btnNewFrame.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));
//set bounds of the Button
btnNewFrame.setBounds(143, 195, 116, 25);
//add Button into contentPane
contentPane.add(btnNewFrame);
JComboBox comboBox1 = new JComboBox();
comboBox1.setModel(new DefaultComboBoxModel(new String[] {"Test3", "Test4"}));
comboBox1.setBounds(77, 61, 230, 27);
contentPane.add(comboBox1);
}
}
Thanks
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started