Skip to main content

Java

Go Search
Linda Noss
APCS
Java
VGP
Web Page Design
Consumer Math
Desktop Publishing
See your grade
  
Linda Noss > Java > Code Hints > How to use a TextField  

Code Hints

Modify settings and columns
Use this page to post questions and answers (no complete programs)
  
View: 
Post
Started: 11/8/2011 9:22 AM
Picture: Linda Noss
Linda Noss
How to use a TextField
public class testBrick extends Applet implements ActionListener {
 private Brick myBrick;
 private Label lblQuestion;  //create the label
 private TextField txtQuestion; // create the textField
 private int brickWidth =10;
 
 public void init(){
  this.setSize(500,500);
  myBrick = new Brick(50,50, new Color(153,0,0),100,50);
// create the Label and TextFeild and add to the applet and ActionListener
  lblQuestion = new Label("What is the width of the brick");
  add(lblQuestion);
  txtQuestion = new TextField();
  add(txtQuestion);
  txtQuestion.addActionListener(this);
 }
 
 public void paint(Graphics g){ 
//use the information from the TextField
  myBrick.setWidth(brickWidth);
  myBrick.display(g);
 }
 @Override
 public void actionPerformed(ActionEvent e) {
//Get the information out of the TextField and parse it
  if(e.getSource() == txtQuestion){
   brickWidth = Integer.parseInt(txtQuestion.getText());
  }
  repaint();
 }
}