?
Solved

Events in a frame problem

Posted on 2000-04-22
3
Medium Priority
?
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 75 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

770 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