Results 1 to 18 of 18
Like Tree3Likes
  • 1 Post By Fubarable
  • 1 Post By Fubarable
  • 1 Post By DarrylBurke

Thread: Applet Multiple windows

  1. #1
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Applet Multiple windows

    I just joined this forum to get a question answered.

    I am creating a sort of calculator with an applet. the problem that I'm having is that when i use multiple classes(haven't really tried all in one class) i get multiple windows. Is there a way to make an applet convert into another instead of opening another window?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    
    public class MainClass extends JFrame implements ActionListener{
            //these are variables
    	private JLabel lab = new JLabel("Welcome", JLabel.CENTER);
    	private JButton exi = new JButton("Cancel");
    	private JButton add = new JButton("+");
    	private JButton sub = new JButton("-");
    	private JButton div = new JButton("/");
    	private JButton mul = new JButton("*");
    	Add addobj = new Add();
    	//this is the applet from what i understand
    	public MainClass(){
    		setLayout(new GridLayout(3,1));
    		JPanel p = new JPanel(new GridLayout(2,2));
    		JPanel p2 = new JPanel();
    		add(lab); add(p);
    		p.add(add); p.add(sub); p.add(div); p.add(mul);
    		add(exi); 
    		getContentPane().setBackground(Color.white);
    		lab.setFont(new Font("SansShief", Font.ITALIC, 20));
    		exi.setFont(new Font("SansShief", Font.BOLD, 20));
    		add.setFont(new Font("SansShief", Font.BOLD, 20));
    		sub.setFont(new Font("SansShief", Font.BOLD, 20));
    		div.setFont(new Font("SansShief", Font.BOLD, 20));
    		mul.setFont(new Font("SansShief", Font.BOLD, 20));
    		exi.addActionListener(this);
    		add.addActionListener(this);
    		sub.addActionListener(this);
    		div.addActionListener(this);
    		mul.addActionListener(this);
    		setSize(300, 300);
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
        
    	}
            //this is when i press the button add the add.class will start
    	public void actionPerformed(ActionEvent e){
    		if(e.getSource() == exi){
    			System.exit(0);
    		}else if(e.getSource()== add){
    			addobj.add();
    		}else if(e.getSource() == sub){
    
    		}
    	}
    	public static void main(String args[]){
    		MainClass main = new MainClass();
    	}
    
    }
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.Closeable;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.plaf.basic.BasicInternalFrameTitlePane.CloseAction;
    
    
    public class Add extends JFrame implements ActionListener {
    	private JTextField fnum = new JTextField(15);
    	private JTextField snum = new JTextField(15);
    	private JLabel sumnum = new JLabel("The Sum will come upp here once you press count");
    	private JLabel fdis = new JLabel("Type the first number");
    	private JLabel sumdis = new JLabel("The sum");
    	private JLabel sdis = new JLabel("Type the second number");
    	private JButton back = new JButton("Back");
    	private JButton sum = new JButton("Count");
    	//I think i'm creating a new applet here if i'm not entirely wrong
    	public void add(){
    		setLayout(new GridLayout(8,1));
    		add(fdis); add(fnum); add(sdis); add(snum); add(sumdis); add(sumnum); add(sum); add(back);
    		getContentPane().setBackground(Color.white);
    		fnum.setFont(new Font("SansShief", Font.ITALIC, 20));
    		snum.setFont(new Font("SansShief", Font.ITALIC, 20));
    		back.addActionListener(this);
    		sum.addActionListener(this);
    		pack();
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
    	public void actionPerformed(ActionEvent e){
    		Scanner sc1 = new Scanner(fnum.getText());
    		double fnumber = sc1.nextDouble();
    		Scanner sc2 = new Scanner(snum.getText());
    		double snumber = sc2.nextDouble();
    		double total = fnumber + snumber;
    		String s = String.format("%.5f", total);
    		if(e.getSource() == sum){
    			sumnum.setText(s);
    		}else if(e.getSource()== back){
    			MainClass.main(null);
    			
    		}
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Applet Multiple windows

    I don't see any applet code whatsoever in your post and wonder if you're confusing application with applet which has a distinct meaning in Java.

    But regardless, your problem is that you're code is geared towards creating JFrames when it should instead be geared towards creating JPanels. If your GUI's create JPanels, then they can be displayed in JFrames, or JDialogs or nested in other JPanels, or swapped with other JPanels via CardLayout, etc...

  3. #3
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Applet Multiple windows

    maybe this is to complicated for me because i don't really understand what you are talking about :(. I got a book about java and it said that that was an applet :/.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Applet Multiple windows

    Quote Originally Posted by totalspelnerd View Post
    maybe this is to complicated for me because i don't really understand what you are talking about :(.
    No, it's not too complicated, it's just something that you are going to understand very soon.

    Your classes both extend JFrame which is a top-level window or you can think of it as an isolated gui application. What I'm suggesting is that rather than do this, you instead create panels that holds all the stuff that makes up your GUI, and that you can then build a GUI with a JFrame that holds these panels. I urge you to learn about JPanels, JFrames, and all of Swing at the Java tutorials.



    I got a book about java and it said that that was an applet :/.
    An applet is a gui that extends the Applet or JApplet class:

    How to Make Applets
    totalspelnerd likes this.

  5. #5
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Applet Multiple windows

    An applet is a gui that extends the Applet or JApplet class:
    What is the difference between JApplet and Applet, is it the same for JPanel and Panel if it exists?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Applet Multiple windows

    Quote Originally Posted by totalspelnerd View Post
    What is the difference between JApplet and Applet, is it the same for JPanel and Panel if it exists?
    Yes, exactly. There are two main GUI libraries that come with core Java, AWT and Swing. AWT is the older of the two and is not used as much now, while Swing is newer and more powerful, and was built on top of AWT, although it too is showing its age. Most Swing components begin with a "J" to distinguish them from their AWT counterparts (if they exist). So for example AWT has Frame, Panel, Applet, TextField, while Swing has JFrame, JPanel, JApplet, and JTextField.

    I recommend that you stick with Swing GUI's unless you have compelling reason not to.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Applet Multiple windows

    Applet and Panel are part of the old AWT toolkit. JApplet and JPanel are part of Swing, which succeeded AWT at least 10 years ago.

    Unless you have a really justifiable need, and then only after understand all the ramifications, don't mix AWT and Swing Components in the same GUI.

    db

    edit hi Pete!
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Applet Multiple windows

    Hey there Darryl!
    DarrylBurke likes this.

  9. #9
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Applet Multiple windows

    Is there another tutorial that is the same but uses eclipse? I don't like using netbeans... and i want to learn what I'm doing and not generate code or copying without instructions.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Applet Multiple windows

    Nothing about the linked tutorials is NetBeans-specific. Did you even go through them?

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Applet Multiple windows

    Quote Originally Posted by Fubarable View Post
    Your classes both extend JFrame which is a top-level window or you can think of it as an isolated gui application. What I'm suggesting is that rather than do this, you instead create panels that holds all the stuff that makes up your GUI, and that you can then build a GUI with a JFrame that holds these panels. I urge you to learn about JPanels, JFrames, and all of Swing at the Java tutorials.

    i've read through this now but i still can't get my program to work correctly. either it create a new window or use the old but add a bunch more to it which end up being a mess. any tips on how i can make it work?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Applet Multiple windows

    What type of GUI exactly are you trying to make?

  13. #13
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Applet Multiple windows

    What type of GUI exactly are you trying to make?
    I'm making a calculator that is going to look something like this:
    Attachment 3222
    and when i press the + button i want it to change into this:
    Attachment 3223
    when i press the back button i want it to go back to the first GUI
    and the problem i have is that it opens another window(i don't have anything against that but then i want the other window to close, but i rather have it changing). So is there some way to change the Panel to look like something else or have different properties? The code (if you want it) is at the top. and yes i want it to use different classes if possible, to make it easier.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Applet Multiple windows

    The intial calculator screen can be done using BorderLayout for the overall GUI, GridLayout to hold your math operator buttons
    The next screen would probably work best with either BoxLayout or GridLayout.
    You can swap the two views with a CardLayout.

  15. #15
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Applet Multiple windows

    Quote Originally Posted by Fubarable View Post
    The intial calculator screen can be done using BorderLayout for the overall GUI, GridLayout to hold your math operator buttons
    The next screen would probably work best with either BoxLayout or GridLayout.
    You can swap the two views with a CardLayout.
    i know how to make the screens (already done that) but the only problem i have is to swap them. I'm not that good at multiple classes. I just try some stuff and hope that it works, but the cardlayout, i have no idea how to make into different classes... and it doesn't make it better that i have a hard time concentrating to read on a computer screen, one less when it's not my main language...

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Applet Multiple windows

    And there are many examples of CardLayout and other layouts here in this very forum, so give it a search!

  18. #18
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Applet Multiple windows

    Quote Originally Posted by Fubarable View Post
    And there are many examples of CardLayout and other layouts here in this very forum, so give it a search!
    I'm sorry if I'm sounding like "give everything to me" right now. I have got a simple test working and i understand how it works but I don't know how I can call a panel from a different class.

    i've copied the source code from CardLayoutDemo and played around with it to make it work like i want it.
    How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    I want to get the information from one panel in one class to another class, if that is possible. sorry if im not too clear about what i'm speaking about.

Similar Threads

  1. Closing multiple windows
    By jonytek in forum New To Java
    Replies: 4
    Last Post: 05-24-2011, 08:11 AM
  2. trying to use multiple JFrame Windows
    By tidus1103 in forum AWT / Swing
    Replies: 0
    Last Post: 03-08-2011, 03:40 PM
  3. multiple windows coming up
    By Kyle227 in forum New To Java
    Replies: 8
    Last Post: 04-27-2010, 01:14 AM
  4. multiple windows and eventlisteners
    By kmyr in forum New To Java
    Replies: 3
    Last Post: 01-10-2010, 01:26 PM
  5. Opeing multiple pdf files in different acrobat reader windows
    By shweta.ahuja in forum Web Frameworks
    Replies: 2
    Last Post: 05-07-2008, 12:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •