8

anyone know or have an idea as to why my button disappears after i resize the applet?

this is my code:


import java.awt.event.*;
import javax.swing.*;
import acm.program.*;

public class button extends ConsoleProgram {

    public void init(){

        hiButton = new JButton("hi");
        add(hiButton, SOUTH);
        addActionListeners();


    }

    public   void actionPerformed(ActionEvent e){
        if(hiButton == e.getSource()){
           println("hello") ;
        }

    }
private JButton hiButton;


}

mre
  • 43,520
  • 33
  • 120
  • 170

6 Answers6

1

I'm not sure if it is a good Idea to redefine the init-method. When I have a look at http://jtf.acm.org/javadoc/student/acm/program/ConsoleProgram.html I would expect that you have implement only the run-method. Overriding init without calling super.init() Looks strange to me.

Maybe I would be better to derive from JApplet directly for your first steps in Applet programming.

0

Assuming that

  • your ConsoleProgram extends (directly or indirectly) JApplet
  • You declared SOUTH as a static final variable that has the value BorderLayout.SOUTH (otherwise your code doesn't compile)

The code should work, no need to repaint (unless you would like to do some application-specific optimization). I just copied and pasted your code (by expliciting the two assumptions above), I see the applet and the button doesn't disappear on resize.

Anyway there are few "not good" things in the code:

  1. First of all, a naming convention issue: the class name should be "Button" with the first letter capitalized (on top of that, it's a poor name for an Applet)
  2. Second, action listeners should be attached before adding the component;
  3. Third, as Oracle doc suggests here, the code that builds the GUI should be a job that runs on the event dispatcher thread. You can do that by wrapping the build gui code in a Runnable using a SwingUtilities.invokeAndWait(Runnable()
Andrea
  • 2,714
  • 3
  • 27
  • 38
0

Have you tried calling super.init() at the start of your init() method?

Edward Falk
  • 9,991
  • 11
  • 77
  • 112
0

Try explicitly using a layout for your Console and then use relative positioning.

-1

To re-size a button in Applet:

public class Button extends JApplet implements ActionListener {

   private JButton button;

   public void init() {
      Container container = getContentPane();
      container.setLayout(null);
      container.setBackground(Color.white);
      button = new JButton("Press Me");
      button.setSize(getWidth()/2,20);
      button.setLocation(getWidth()/2-button.getSize().width/2, getHeight()/2-button.getSize().height/2);
      container.add(button);
      button.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e) {
      int width = (button.getSize().width == getWidth()/2) ? getWidth()/4 : getWidth()/2;
      int height = button.getSize().height;
      button.setSize(width,height);
      button.setLocation(getWidth()/2-width/2, getHeight()/2-height/2);
   }
}

To re-size a button in JFrame:

public class Button extends JFrame implements ActionListener {
   private JButton button;

   public Button(String title) {
      Container container = getContentPane();
      container.setLayout(null);
      container.setBackground(Color.white);
      setTitle(title);
      setSize(400,400);
      button = new JButton("Press Me");
      button.setSize(getWidth()/2,20);
      button.setLocation(getWidth()/2-button.getSize().width/2,
                     getHeight()/2-button.getSize().height/2);
      container.add(button);
      button.addActionListener(this);
    }

   public void actionPerformed(ActionEvent e) {
      int width = (button.getSize().width == getWidth()/2) ? getWidth()/4 : getWidth()/2;
      int height = button.getSize().height;
      button.setSize(width,height);
      button.setLocation(getWidth()/2-width/2, getHeight()/2-height/2);
   }

   public static void main(String[] args) {
      Button button = new Button("Test");
      button.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      button.setVisible(true);
   }
}
Ahmed Hassanien
  • 527
  • 5
  • 18
  • -1 for recommending a null layout (aka: manual sizing/locating of components): whatever the problem, that's **not** the Swing'ish way of a solution. – kleopatra Jan 06 '13 at 12:52
  • @kleopatra What is the problem of using a null layout for "Absolute Positioning". It is in the docs (http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html) and my code is an example. – Ahmed Hassanien Jan 07 '13 at 01:57
  • the main problem is that you have to re-do all the calculations when any property on any of the children which effects the layout changes - which is exactly what a manager does. Doing so manually is not only a waste of time (you have to re-code lots of code) but also tricky to get right: you need to listen to all those changes and still might not get notified on all (missing manual re/validation, f.i.) – kleopatra Jan 07 '13 at 10:16
-2

Have you declared the repaint method...???

You are using swing. It needs to have declared a repaint.

Please define a custom repaint mwthod

Fr_nkenstien
  • 1,923
  • 7
  • 33
  • 66