The Artima Developer Community
Sponsored Link

Java Answers Forum
JFrames

1 reply on 1 page. Most recent reply: Apr 16, 2003 1:24 AM by Mary Ismael

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
David Morrisroe

Posts: 1
Nickname: davem
Registered: Apr, 2003

JFrames Posted: Apr 14, 2003 4:31 PM
Reply to this message Reply
Advertisement
I Have This Code and It should make the interface on a single window But when run ther is a frame with the menu on it and a frame with the other interface on it any help would be much apperchead.

// David Morrisroe	
// 13-04-2003
// Calcculator.java - A Java Program To Implement A Simple Calculater Using Menus
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
 
public class Calculator extends JFrame{
	JFrame MyFrame;
	JMenuBar MyMenuBar;
	JTextField Number1, Number2;
	JLabel label1, label2, label3, AnsLabel;
	 
	public  Calculator()
	{		
		super("Calculator");
		// Create The JFrame For The Program
		MyFrame = new JFrame("Calculator");
		// Create The Menu For The Program
		MyMenuBar = new JMenuBar();
		// Add The Menu To The Frame
		MyFrame.setJMenuBar(MyMenuBar);
		// Add a Menu To The Menu Bar
		JMenu Calc = new JMenu("Calculate");
		MyMenuBar.add(Calc);
		// Add Menu Items To The menu
		Calc.add(new JMenuItem("Add"));
		Calc.add(new JMenuItem("Subtract"));
		Calc.add(new JMenuItem("Multiply"));
		Calc.add(new JMenuItem("Divide"));
		// Add A Separator
		Calc.addSeparator();
		Calc.add(new JMenuItem("Exit"));
		
		Number1 = new JTextField(6);
		Number2 = new JTextField(6);
		label1 = new JLabel("Number 1");
		label2 = new JLabel("Number 2");
		label3 = new JLabel("Solution");
		AnsLabel = new JLabel();
		
		
		Container c = getContentPane();
		c.setLayout(new GridLayout(3,2));
		
		c.add(label1);
		c.add(Number1);
		c.add(label2);
		c.add(Number2);
		c.add(label3);
		c.add(AnsLabel);
		
		MyFrame.setSize(300,300);
		MyFrame.setVisible(true);
		show();
	}
	
	public static void main(String args[])
	{
		Calculator MyCalc = new Calculator();
		
		MyCalc.addWindowListener(
			new WindowAdapter(){
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			}
		);
	}
} 


Thank You

Dave Morrisroe


Mary Ismael

Posts: 3
Nickname: maryam
Registered: Apr, 2003

Re: JFrames Posted: Apr 16, 2003 1:24 AM
Reply to this message Reply
i think you are working in two instances of JFrame
the first one is that which you got from your super class JFrame for beeing as subclass to it
and the other instance is the one that you make as a data member in your class so you have to :
i think you have to choose between two things :
you have to extends Object not JFrame
or you have not to use a JFrame as data member and you make all of your coding to the instance that you have from your super class(JFrame) like that:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Calculator extends JFrame{
JMenuBar MyMenuBar;
JTextField Number1, Number2;
JLabel label1, label2, label3, AnsLabel; public Calculator() { super("Calculator");
MyMenuBar = new JMenuBar();
setJMenuBar(MyMenuBar);
// Add a Menu To The Menu Bar
JMenu Calc = new JMenu("Calculate"); MyMenuBar.add(Calc);
// Add Menu Items To The menu Calc.add(new JMenuItem("Add"));
Calc.add(new JMenuItem("Subtract")); Calc.add(new JMenuItem("Multiply")); Calc.add(new JMenuItem("Divide"));
// Add A Separator Calc.addSeparator();
Calc.add(new JMenuItem("Exit")); Number1 = new JTextField(6); Number2 = new JTextField(6);
label1 = new JLabel("Number 1"); label2 = new JLabel("Number 2"); label3 = new JLabel("Solution"); AnsLabel = new JLabel(); Container c = getContentPane(); c.setLayout(new GridLayout(3,2)); c.add(label1);
c.add(Number1);
c.add(label2);
c.add(Number2);
c.add(label3);
c.add(AnsLabel); setSize(300,300);
setVisible(true);
show();
}
public static void
main(String args[]) { Calculator MyCalc = new Calculator(); MyCalc.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } } ); }}

Flat View: This topic has 1 reply on 1 page
Topic: com.sun.java.util.collections Previous Topic   Next Topic Topic: Help - Heap file implementation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use