2

I am following the programming methodologies course for learning java under stanford.edu, I tried a program for changing the font of a label according to the font given by the use inside JTextField , it is responding to enter key event but it is not printing anything inside JTextField and also one problem I am facing is that when I expand the window everything is going blank and only the window is shown.How should I solve this?

import acm.program.*;
import acm.graphics.*;
import acm.gui.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JApplet;
public class EnterText extends GraphicsProgram {
    public void init(){
        //System.out.println("hello");
        fontfield=new JTextField(MAX_FONT_NAME);
         fontfield.setText("Enter your question here.");
        this.fontfield.addActionListener(this);
         fontfield.selectAll();
        add(fontfield,SOUTH);


        add(new JLabel("FONT"),SOUTH);

        lasty=0;

        lastlabel=new GLabel(str);

        addGLabel(lastlabel);
        addActionListeners();
        addMouseListeners();
    }
    public void actionPerformed(ActionEvent e){
        //System.out.println("hello");
        if(e.getSource()==fontfield){
        //  println("hello" + fontfield.getText());
            GLabel label=new GLabel("Hello");
            label.setFont(lastlabel.getFont());
            label.setFont(fontfield.getText());

            addGLabel(label);
            lastlabel=label;
        }
    }

    private void addGLabel(GLabel label){
        //System.out.println("hello");
        lasty +=label.getHeight();
        lasty +=lastlabel.getDescent()-label.getDescent();
        add(label,LEFT_MARGIN,lasty);
    }


private static final int MAX_FONT_NAME=150;
private static final int LEFT_MARGIN=30;
private static final  String str="Hello Java";
private JTextField fontfield;
private GLabel lastlabel;
private double lasty;

}
Niranjan Dattatreya
  • 405
  • 1
  • 6
  • 18
  • 3
    Why code an applet? If it is due to spec by your instructor, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Aug 18 '14 at 02:21
  • *"when I expand the window everything is going blank"* That is a different question that should have been asked as a separate question. But given it can be responded to in comment, I'll do that. Applets were not designed to be resized. Experiments indicated that it was easy to resize them in some browsers, impossible in others. – Andrew Thompson Aug 18 '14 at 02:26
  • 2
    I'm afraid not many of use are familiar with the `acm` API – MadProgrammer Aug 18 '14 at 02:27
  • `add(fontfield,SOUTH); add(new JLabel("FONT"),SOUTH);` If [`GraphicsProgram`](http://cs.stanford.edu/people/eroberts/jtf/javadoc/complete/acm/program/GraphicsProgram.html) uses `BorderLayout` that will not work. Only ***one*** component can appear at each position in a `BorderLayout`. If you need multiple components there, add a `JPanel` with an appropriate layout and add the components to the panel. – Andrew Thompson Aug 18 '14 at 02:29
  • @MadProgrammer And those of us (that's the 'Royal' us) with a passing familiarity with it detest it for the fact it is 1) Applet based. 2) Used by teachers to dump students quickly into GUI programming. 3) Really doesn't seem to do anything easier or better than the native Swing and Graphics APIs.. I'd like to see a quiet (or a 'screaming at the stake as the flames lick') death to the [ACM Java graphics API](http://cs.stanford.edu/people/eroberts/jtf/javadoc/complete/index.html?overview-summary.html). – Andrew Thompson Aug 18 '14 at 02:36
  • 1
    @AndrewThompson: It's also an example of inadequate maintenance, as suggested [here](http://stackoverflow.com/a/12130829/230513). – trashgod Aug 18 '14 at 11:11
  • @MadProgrammer: I gave it a whirl [here](http://stackoverflow.com/a/12130829/230513). – trashgod Aug 18 '14 at 11:11

0 Answers0