Domanda

hi guys i seem to be having a issue with the lay out of my applet, I have a 5 button (cluster) in the applet that are in the top(North) center I would like them to be in the bottom(south) center of the applet, can anyone help get it to the bottom?- the button cluster is fine I just want it to be at the bottom of my applets frame no the top like it currently is (I want to move the buttons cluster to the bottom)...thanks guys

 import java.awt.*;
 import java.awt.event.*;
 import java.applet.*;


  public class moveIt extends Applet implements ActionListener

      {

 private Image cup;
 private Panel Keypad = new Panel();
 public int top = 15;
 public int left = 15;
 private Button Keyarray[] = new Button[5];
 public void init ()
 {
cup=getImage(getDocumentBase(), "cup.gif");
Canvas myCanvas= new Canvas();


 Keyarray[0] = new Button ("Up");
 Keyarray[1] = new Button ("Left");
 Keyarray[2] = new Button ("Down");
 Keyarray[3] = new Button ("Right");
 Keyarray[4] = new Button ("Center");
 setBackground(Color.BLUE);

 Panel frame = new Panel();
 frame.setLayout(new BorderLayout());
 frame.add(myCanvas, BorderLayout.NORTH);
 Keypad.setLayout(new BorderLayout());     
 frame.add(Keypad, BorderLayout.SOUTH);
 //Keypad.setLayout(new BorderLayout());


 Keypad.add(Keyarray[0], BorderLayout.NORTH);
 Keypad.add(Keyarray[1], BorderLayout.WEST);
 Keypad.add(Keyarray[2], BorderLayout.SOUTH);
 Keypad.add(Keyarray[3], BorderLayout.EAST);
 Keypad.add(Keyarray[4], BorderLayout.CENTER);

 Keyarray[0].addActionListener(this);
 Keyarray[1].addActionListener(this);
 Keyarray[2].addActionListener(this);
 Keyarray[3].addActionListener(this);
 Keyarray[4].addActionListener(this);


 }//end of method init


 public void paint(Graphics g)


 {

     g.drawImage(cup, left, top, this);

 }

 public void actionPerformed(ActionEvent e)
 {
     String arg= e.getActionCommand();

      if (arg.equals("Up"))
            top -= 15;
        if (arg.equals("down"))
            top += 15;
        if (arg.equals("Left"))
            left -= 15;
        if (arg.equals("Right"))
            left += 15;
        if (arg.equals("Center"))
        {
            top=60;
            left=125;
        }

     repaint();



      }//end paint method

   }//end of class
È stato utile?

Soluzione

I'm not sure I got your point... here is how I modified init() to have the button in the bottom area.

I also moved canvas to center because, if you have it in NORTH, you can't see it as it is 0x0 size. Anyway I'm not sure this is what you actually want so maybe you will have to move it back to NORTH.

Basically what I did to solve the buttons issue is just removing the Panel frame you had and set BorderLayout manager to the applet.

public void init() {
  cup = getImage(getDocumentBase(), "cup.gif");
  Canvas myCanvas = new Canvas();

  Keyarray[0] = new Button("Up");
  Keyarray[1] = new Button("Left");
  Keyarray[2] = new Button("Down");
  Keyarray[3] = new Button("Right");
  Keyarray[4] = new Button("Center");
  setBackground(Color.BLUE);

  // I just removed Panel frame and added components directly to applet
  setLayout(new BorderLayout());
  add(myCanvas, BorderLayout.CENTER);
  myCanvas.setBackground(Color.red);
  Keypad.setLayout(new BorderLayout());
  add(Keypad, BorderLayout.SOUTH);
  // Keypad.setLayout(new BorderLayout());

  Keypad.add(Keyarray[0], BorderLayout.NORTH);
  Keypad.add(Keyarray[1], BorderLayout.WEST);
  Keypad.add(Keyarray[2], BorderLayout.SOUTH);
  Keypad.add(Keyarray[3], BorderLayout.EAST);
  Keypad.add(Keyarray[4], BorderLayout.CENTER);

  Keyarray[0].addActionListener(this);
  Keyarray[1].addActionListener(this);
  Keyarray[2].addActionListener(this);
  Keyarray[3].addActionListener(this);
  Keyarray[4].addActionListener(this);

}// end of method init

I hope it helps.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top