How do I maintain data from main Jframe? - java

So I have this calculator when you first run it you will have to input name and section and then press submit and when you press submit 4 buttons will appear and each of those buttons will bring you to another JFrame. The problem is when I press back from that other JFrame it will direct me back from that main frame and you will again have to input name and section. How do I maintain the data which the user first inputted?

You can manage that using array of Jframes , when click frame you know the index of it

May be beacuse you are Extending JFrame. Which is a bad habit .
Here I have done a short EG which creates the Object of respective Frames and calls the method
setVisible(T/F) accordingly :
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TestMultiFrames {
JFrame mainFrame,frame2,frame3;
public TestMultiFrames(){
mainFrame =new JFrame();
frame2 =new JFrame();
frame3 =new JFrame();
JPanel p=new JPanel();
JButton but1=new JButton("frame2");
JButton but2=new JButton("frame3");
JButton but3=new JButton("back to frame 3");
p.add(but1);
frame2.add(but2);
frame3.add(but3);
but1.addActionListener(new CustomActionListener1());
but2.addActionListener(new CustomActionListener2());
but3.addActionListener(new CustomActionListener3());
mainFrame.add(p);
mainFrame.setSize(200,200);
frame2.setSize(200,200);
frame3.setSize(200,200);
mainFrame.setVisible(true);
}
public static void main(String... args)
{
new TestMultiFrames ();
}
class CustomActionListener1 implements ActionListener{
public void actionPerformed(ActionEvent e) {
mainFrame.setVisible(false);
frame2.setVisible(true);
}
}
class CustomActionListener2 implements ActionListener{
public void actionPerformed(ActionEvent e) {
frame2.setVisible(false);
frame3.setVisible(true);
}
}
class CustomActionListener3 implements ActionListener{
public void actionPerformed(ActionEvent e) {
frame3.setVisible(false);
frame2.setVisible(true);
}
}
}

Related

Calling different screen thru button from a screen

Hello I would like to ask how can I call my Main menu screen from MainScreen? and kindly explain a little more details about Listener.
below is my prepared code:
public class MainScreen {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
JLabel WelcomeNote = new JLabel("Welcome");
panel.add(WelcomeNote);
JButton Start = new JButton("Start");
panel.add(Start);
//Insert action for Start button here
}
}
public class MainMenu {
public static void main(String[] args){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
JLabel menuLbl = new JLabel("Main Menu");
panel.add(menuLbl);
}
}
What is wrong?
You cannot have two main methods in a single file in Java.
Program
Here is a demo program to change windows.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class First extends JFrame
{
JLabel jlb = new JLabel("Label in First Window");
JButton jb = new JButton("Next Window");
First()
{
super("First Windows");
//Set this frame
this.setSize(350,250);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Setting size of components
jlb.setBounds(10,10,200,40);
jb.setBounds(10,120,150,40);
add(jlb);
add(jb);
jb.addActionListener((e)->{
this.setVisible(false);
new Second();
});
setVisible(true);
}
}
class Second extends JFrame implements ActionListener
{
JLabel jlb = new JLabel("Label in Second Window");
JButton jb = new JButton("Prev. Window");
Second()
{
super("Second Window");
this.setSize(350,250);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Setting size of components
jlb.setBounds(10,10,200,40);
jb.setBounds(10,120,150,40);
add(jlb);
add(jb);
jb.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
this.setVisible(false);
new First();
}
}
class StartHere
{
public static void main(String[] args) {
Runnable r = ()->{
new First();
};
r.run();
}
}
Understanding the above program.
The StartHere class has a main method. It is just used for calling the first window you like. I could even call Second using new Second().
First and Second are similar codes.
Both of them have buttons. On each button (or JButton) I have added a method named addActionListner(this). This method fires up an ActionEvent which as you can see in Second class is captured by actionPerformed method. This method is declared in Functional Interface, ActionListener. The 'this' passed in Second class is you telling where the actionPerformed method is present in your code. The parameter is an ActionListener. Hence, you have to implement ActionListener for the class where you define actionPerformed.
Bonus
The First class doesn't seem to follow the norms described above. I passed a strange syntax. It is a new feature included in Java 8.
See this Oracle tutorial about Lambda Expressions.

Two labels overlap each other when using the Radio Button Listener

I made a simple program where there are two radio buttons each with an action listener.
After pressing first button, a label is printed and the same thing happens with the other.
The problem is that both the labels overlap after pressing first and second button.
Edit-The previous label must be removed and then new label must be on the screen.
ex-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ckbxdm{
JFrame frame;
JRadioButton r1,r2;
ButtonGroup grp;
JLabel l1,l2;
void box(){
frame=new JFrame("Hello");
r1=new JRadioButton("Login");
r2=new JRadioButton("Signup");
grp=new ButtonGroup();
grp.add(r1);
grp.add(r2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.getContentPane().add(r1);
frame.getContentPane().add(r2);
r1.setBounds(100,120,100,20);
r2.setBounds(200,120,100,20);
frame.setBounds(100,100,500,500);
frame.setVisible(true);
r1.addActionListener(new listener1());
r2.addActionListener(new listener2());
}
class listener1 implements ActionListener{
public void actionPerformed(ActionEvent ae){
frame.getContentPane().repaint();
frame.getContentPane().revalidate();
l1=new JLabel("Login area");
frame.getContentPane().add(l1);
l1.setBounds(100,200,100,20);
}
}
class listener2 implements ActionListener{
public void actionPerformed(ActionEvent ae){
frame.getContentPane().repaint();
frame.getContentPane().revalidate();
l2=new JLabel("Signup area");
frame.getContentPane().add(l2);
l2.setBounds(100,200,100,20);
}
}
public void itemStateChanged(ItemEvent ie){
frame.repaint();
}
}
public class CheckboxDemo{
public static void main(String args[]){
ckbxdm obj=new ckbxdm();
obj.box();
}
}
In addition to the answer provided above, if you want to hide the other label, you can set its visibility to false and repaint the parent component. You can learn more from here. Note that calling the revalidate method here is unnecessary since you are not removing any component from the hierarchy.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ckbxdm{
JFrame frame;
JRadioButton r1,r2;
ButtonGroup grp;
JLabel l1,l2;
void box(){
frame=new JFrame("Hello");
r1=new JRadioButton("Login");
r2=new JRadioButton("Signup");
grp=new ButtonGroup();
grp.add(r1);
grp.add(r2);
l1=new JLabel("Login area");
l1.setBounds(100,200,100,20);
l1.setVisible(false);
frame.getContentPane().add(l1);
l2=new JLabel("Signup area");
l2.setBounds(100,200,100,20);
l2.setVisible(false);
frame.getContentPane().add(l2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.getContentPane().add(r1);
frame.getContentPane().add(r2);
r1.setBounds(100,120,100,20);
r2.setBounds(200,120,100,20);
frame.setBounds(100,100,500,500);
frame.setVisible(true);
r1.addActionListener(new listener1());
r2.addActionListener(new listener2());
}
class listener1 implements ActionListener{
public void actionPerformed(ActionEvent ae){
l2.setVisible(false);
l1.setVisible(true);
frame.getContentPane().repaint();
}
}
class listener2 implements ActionListener{
public void actionPerformed(ActionEvent ae){
l1.setVisible(false);
l2.setVisible(true);
frame.getContentPane().repaint();
}
}
public void itemStateChanged(ItemEvent ie){
frame.repaint();
}
}
public class CheckboxDemo{
public static void main(String args[]){
ckbxdm obj=new ckbxdm();
obj.box();
}
}
try to change ur "sign up position" in X-Ycooridantes direction, let say change following line of code l2.setBounds(100,200,100,20);
to
l2.setBounds(200,200,100,20);
and the same way for l1 change it to l1.setBounds(50,200,100,20);
, it will definitely work

How to implement the ActionListener to get the Exit Button to work

Here is my code, but the error shows that the implemented ActionListener is not correct. I also declared the buttons so how do I make the system exit? What did I do wrong? Thanks in advance
import javax.swing.*;
import java.awt.*;
import java.awt.FlowLayout;
public class MyFrame extends JFrame implements ActionListener {
public MyFrame() {
// set flow layout for the frame
this.getContentPane().setLayout(new FlowLayout());
JButton ExitBtn = new JButton();
ExitBtn.setText("Exit");
JButton Find = new JButton("Find");
JButton Clear = new JButton("Clear");
// add buttons to frame
add(ExitBtn);
add(Find);
add(Clear);
}
public void actionPerformed(ActionEvent e){
System.exit(0);
ExitBtn.addActionListener(this);
}
public static void main(String[] args) {
MyFrame mf = new MyFrame();
mf.pack();
mf.setVisible(true);
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
onClick:
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
I think you should move the ExitBtn.addActionListener(this) call to the constructor of MyFrame class so that it looks like this:
JButton ExitBtn = new JButton();
ExitBtn.setText("Exit");
ExitBtn.addActionListener(this)
and actionPermormed method looks like this:
#Override
public void actionPerformed(ActionEvent e){
System.exit(0);
}

Java JButton - making simple menu

Hi take a look on this code:
package arkanoid;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.*;
public class Arkanoid extends JFrame
{
private static final long serialVersionUID = 6253310598075887445L;
static JFrame frame;
static class Action1 implements ActionListener {
public void actionPerformed (ActionEvent e) {
//frame = new JFrame("Arkanoid");
frame.setLocationRelativeTo(null);
frame.setIgnoreRepaint(true);
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(500,400);
frame.add(new Gra());
}
}
static class Action2 implements ActionListener {
public void actionPerformed (ActionEvent e) {
frame.dispose();
System.exit(0);
}
}
public static void main(String[] args)
{
//new Arkanoid();
frame = new JFrame("Arkanoid");
frame.setSize(500,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Arkanoid BETA");
frame.setLocationRelativeTo(null);
frame.setIgnoreRepaint(true);
frame.setResizable(false);
frame.setVisible(true);
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("Nowa Gra");
panel.add(button);
button.addActionListener (new Action1());
JButton button2 = new JButton("Wyjscie");
panel.add(button2);
button2.addActionListener (new Action2());
}
}
This code almost works, I want to make a button2 a quit button working like X button in top right frame's icons and button1 need to open a Gra() in the same window. When im doing it like this it isnt work fine:/ i need to click 2 times on button1 to go to Gra() and what is more KeyListeners in Gra() arent working :(
Im new in buttons, frames and panels in java so please help with this code. Correct it please.
There are a number of fundamental problems with your code, the least of which is why your button1 requires 2 clicks.
However, for your problem you should try rearranging the order of your button1 listener, so that your Component is added to the frame first, before setting it to be visible. An example that should work:
static class Action1 implements ActionListener {
public void actionPerformed (ActionEvent e) {
frame.add(new Gra());
frame.revalidate();
}
}
Note you have already set the size, location etc of frame in main, so there is no need to set them again every time the button is clicked.
I stress that there are more important problems with your code than this issue. You should take a look at Java's Modifier Types (static does not seem applicable here), as well as object-oriented concepts such as inheritance (you define your Arkanoid class to extend JFrame, yet have a JFrame object as a class variable).
I want to make a button2 a quit button working like X button in top right frame's
You can use the ExitAction class found in Closing an Application.
For other examples of how to use buttons read the Swing tutorial on How to Use Buttons. This is the place to start for all you Swing related questions.
There are many problems with your code. I've refactored it a little. With below code & #ricky116 answer I think you should get all of them.
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
public class Arkanoid extends JFrame
{
public Arkanoid() {
super("Arkanoid");
setSize(500,400);
setTitle("Arkanoid BETA");
setLocationRelativeTo(null);
setResizable(false);
final JPanel panel = new JPanel();
setContentPane(panel);
panel.add(new JButton(new AbstractAction("Nowa Gra") {
public void actionPerformed (ActionEvent e) {
panel.removeAll();
panel.add(new Gra());
panel.revalidate();
panel.repaint();
}
});
panel.add(new JButton(new AbstractAction("Wyjscie") {
public void actionPerformed (ActionEvent e) {
Arkanoid.this.setVisible(false);
}
});
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Arkanoid frame = new Arkanoid();
frame.setVisible(true);
}
});
}
}

ActionListener Event in Java

Ok, i started learning java, and this is a code from internet that doesn't work on my pc
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class form2
{
public static void main(String args[])
{
// Create Frame 1
JFrame frame = new JFrame("Frame 1");
frame.setSize(333,333);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create panel
JPanel panel = new JPanel();
// Create button
JButton button = new JButton("Press me!");
// Add things
panel.add(button);
frame.add(panel);
frame.setVisible(true);
// Add the action listener to that button
button.addActionListener(new action());
static class action implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
// Create Frame 2
JFrame frame2 = new JFrame("Frame 2");
frame2.setSize(200,200);
frame2.setVisible(true);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Clicked!");
JPanel panel2 = new JPanel();
// First add to frame2 the panel just create
frame2.add(panel2);
// Add to panel the label
panel2.add(label);
}
}
}
}
And it give me an error about action and i don't understand why.
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
action cannot be resolved to a type
Illegal modifier for the local class action; only abstract or final is permitted
at form2.main(form2.java:26)
What is my problem?? On that guy's computer works
http://www.youtube.com/watch?v=jEXxaPQ_fQo&feature=channel_video_title
Can anyone help me??
You are declaring a static class inside of a method which you shouldn't do. So take it out of the method, or better, make it an anonymous inner class:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Create Frame 2
JFrame frame2 = new JFrame("Frame 2");
frame2.setSize(200, 200);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Clicked!");
JPanel panel2 = new JPanel();
// First add to frame2 the panel just create
frame2.add(panel2);
// Add to panel the label
panel2.add(label);
frame2.setVisible(true);
}
});
What does this question have to do with java-ee by the way??
You declare a class inside a method, in this case main, however, it should be outside of it, like the guy in the video says.
Hope that helps!
Static classes cannot be defined inside a method. Move the class definition static class .... { } outside of your main method. Also, it is good practice to start classes with an uppercase character (e.g AddPanelAction).
The actionlistener class must be declared outside of the main method like this:
import javax.swing.*;
import java.awt.event.*;
public class Main
{
public static void main(String args[])
{
// Create Frame 1
JFrame frame = new JFrame("Frame 1");
frame.setSize(333,333);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create panel
JPanel panel = new JPanel();
// Create button
JButton button = new JButton("Press me!");
// Add things
panel.add(button);
frame.add(panel);
frame.setVisible(true);
// Add the action listener to that button
button.addActionListener(new action());
}
public static class action implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Create Frame 2
JFrame frame2 = new JFrame("Frame 2");
frame2.setSize(200,200);
frame2.setVisible(true);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Clicked!");
JPanel panel2 = new JPanel();
// First add to frame2 the panel just create
frame2.add(panel2);
// Add to panel the label
panel2.add(label);
}
}
}
Or you can declare the actionlistener by using an anonymous inner class like this:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Do things here
}
});
Static classes can not be defined inside a method. Move that class outside the main method or declare your class non-static within the main method itself, if you need . A static class always requires one outer non-static class.
A day late and dollar short.... but I'll still add it.
Most of the Java actionListener examples on the web are too darn complex. To understand it, you just really need a form, button, and the actionListener. In the example below, the form server as the listener through the addition of 'implements ActionListener'.
import java.applet.Applet;
import java.awt.Button;
import java.awt.Toolkit;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Sample extends Applet implements ActionListener {
Button button;
public void init() {
setLayout(new BorderLayout());
button = new Button("Test");
add("Center", button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// your code to do what you want when the button was clicked goes here
}
}

Categories

Resources