Solved

Events in a frame problem

Posted on 2000-04-22
3
227 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

759 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

20 Experts available now in Live!

Get 1:1 Help Now