Solved

Events in a frame problem

Posted on 2000-04-22
3
228 Views
Last Modified: 2011-09-20
I have an applet that has 4 buttons, each will create a frame.  Each frame will have it's own events(TextField, button etc.) and Labels. The problem is that the frame will display only one event, ie: if i have two Labels or a Label and a Textfield the last event added (add()) is the only one displayed.

Thanks, eric

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class DemoClosingFrame2 extends Applet implements ActionListener
{
//Variables


//Applet Attributes
private Label header = new Label("Checkbook Balance Calculator");
private Font bigFont = new Font("TimesRoman", Font.BOLD,18);

private Button currBalanceButton = new Button("Enter Balance");
private Button depositButton = new Button("Enter Deposit");
private Button checkButton = new Button("Enter Check");
private Button endBalanceButton = new Button("Ending Balance");

//Frame Attributes
private FrameYouCanClose2 fycc = new FrameYouCanClose2("Demo Components");      

public void init()
      {
      //init Applet Attributes
      header.setFont(bigFont);
      add(header);
      add(currBalanceButton);
      add(depositButton);
      add(checkButton);
      add(endBalanceButton);
      
      //init Frame Attributes
      fycc.setSize(200,300);
      
      //init currBalanceButton ActionListener
      currBalanceButton.addActionListener(this);
      }
      
public void activateButtons()
      {
      depositButton.addActionListener(this);
      checkButton.addActionListener(this);
      endBalanceButton.addActionListener(this);
      }
      
public void actionPerformed(ActionEvent e)
      {
      String str = e.getActionCommand();
      if(str.equals("Enter Balance"))
            {
            activateButtons();//if the current balance is entered activate other options
            }
      fycc.setVisible(true);
      

      }
}

import java.awt.*;
import java.awt.event.*;

public class FrameYouCanClose2 extends Frame implements ActionListener
      {
      
      TextField beginBalance = new TextField("",10);
      Label enter = new Label("Enter Amount Here");
      
      FrameYouCanClose2(String str)
            {
            super(str);
            add(beginBalance);
            add(enter);
            
            beginBalance.addActionListener(this);
            
            WindowYouCanClose adapter = new WindowYouCanClose(this);
            addWindowListener(adapter);
            }

      public void actionPerformed(ActionEvent e)
      {
      String str = beginBalance.getText();
      }
}
0
Comment
Question by:eboen
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 25 total points
ID: 2740847
Umm, I think you are mixing up events with components. Anyway, your problem is that the default layoutmanager for Frame is BorderLayout, and the default location of a BorderLayout is the center. What happens is that whenever you add a component, BorderLayout resizes it to the size of the entire Frame. What you need to do is set some other Layout manager or a null layout manager. This is an example of how to add a TextField and a label to a Frame.


public class MyApplet extends Applet{

  Frame frame;

  public void init();
    frame = new Frame("Test frame");
    frame.setLayout(null); // Setting layout to null.
    frame.reshape(50,50,500,400);
   
    TextField tf = new TextField();
    tf.reshape(50,50,100,20); // Reshape it or it will have (0,0) size.
    frame.add(tf);
 
    Label l = new Label("nice Label, eh?");
    l.reshape(50,100,100,20); // Reshape it too.
    frame.add(tf);
    frame.show();
  }



  public void stop(){
    frame.dispose();
  }

}

Good luck, Sasha :-)
0
 

Author Comment

by:eboen
ID: 2740915
Thanks, it works.  I'm getting at -depricated warning so I'm assuming setLayout and reshape are from an earlier version of jdk <1.2.

Thanks
Eric
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2741383
reshape is deprecated, use setBounds instead, setLayout isn't deprecated.
Btw, I don't think anything in 1.1 was deprecated, only stuff in 1.0 were...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XML Paring  Error - Premature end of file. 7 77
Java array passed to SQL where clause 23 61
get weblogic logged in user in java 2 57
Image decoding from Camera 3 72
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now