Solved

Events in a frame problem

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

726 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