Solved

Events in a frame problem

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
starter POM and spring-boot-starter,  spring-boot-web 2 54
JavaScript/Java - Changing an image background color 4 66
Java SE 8u111  Lot of stuff broke 11 69
javap bin 2 34
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…
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 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 regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

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