Results 1 to 18 of 18
Thread: Applet Multiple windows
- 03-09-2012, 11:30 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 45
- Rep Power
- 0
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); } } }
-
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...
- 03-09-2012, 11:47 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 45
- Rep Power
- 0
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 :/.
-
Re: Applet Multiple windows
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.
- Lesson: Using Swing Components (The Java™ Tutorials > Creating a GUI With JFC/Swing)
- Using Top-Level Containers
- How to Make Frames (Main Windows)
- How to Use Panels
An applet is a gui that extends the Applet or JApplet class:I got a book about java and it said that that was an applet :/.
How to Make Applets
- 03-10-2012, 12:01 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 45
- Rep Power
- 0
Re: Applet Multiple windows
What is the difference between JApplet and Applet, is it the same for JPanel and Panel if it exists?An applet is a gui that extends the Applet or JApplet class:
-
Re: Applet Multiple windows
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.
- 03-10-2012, 12:05 AM #7
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!Why do they call it rush hour when nothing moves? - Robin Williams
-
Re: Applet Multiple windows
Hey there Darryl!
- 03-10-2012, 10:21 AM #9
Member
- Join Date
- Mar 2012
- Posts
- 45
- Rep Power
- 0
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.
- 03-10-2012, 11:22 AM #10
Re: Applet Multiple windows
Nothing about the linked tutorials is NetBeans-specific. Did you even go through them?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-10-2012, 08:57 PM #11
Member
- Join Date
- Mar 2012
- Posts
- 45
- Rep Power
- 0
-
Re: Applet Multiple windows
What type of GUI exactly are you trying to make?
- 03-10-2012, 10:33 PM #13
Member
- Join Date
- Mar 2012
- Posts
- 45
- Rep Power
- 0
Re: Applet Multiple windows
I'm making a calculator that is going to look something like this:What type of GUI exactly are you trying to make?
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.
-
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.
- 03-10-2012, 11:20 PM #15
Member
- Join Date
- Mar 2012
- Posts
- 45
- Rep Power
- 0
Re: Applet Multiple windows
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...
-
Re: Applet Multiple windows
Note that by "screen" I meant a JPanel, not a JFrame.
-
Re: Applet Multiple windows
And there are many examples of CardLayout and other layouts here in this very forum, so give it a search!
- 03-11-2012, 11:43 AM #18
Member
- Join Date
- Mar 2012
- Posts
- 45
- Rep Power
- 0
Re: Applet Multiple windows
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
-
Closing multiple windows
By jonytek in forum New To JavaReplies: 4Last Post: 05-24-2011, 08:11 AM -
trying to use multiple JFrame Windows
By tidus1103 in forum AWT / SwingReplies: 0Last Post: 03-08-2011, 03:40 PM -
multiple windows coming up
By Kyle227 in forum New To JavaReplies: 8Last Post: 04-27-2010, 01:14 AM -
multiple windows and eventlisteners
By kmyr in forum New To JavaReplies: 3Last Post: 01-10-2010, 01:26 PM -
Opeing multiple pdf files in different acrobat reader windows
By shweta.ahuja in forum Web FrameworksReplies: 2Last Post: 05-07-2008, 12:33 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks