Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
边界布局问题。java,applet_Java_Applet - Fatal编程技术网

边界布局问题。java,applet

边界布局问题。java,applet,java,applet,Java,Applet,大家好,我的小程序的布局似乎有问题,我的小程序中有一个5按钮(集群),位于小程序的顶部(北部)中心,我希望它们位于小程序的底部(南部)中心,有人能帮我把它放到底部吗?-按钮组很好,我只想把它放在我的小程序框架的底部不,像现在一样放在顶部(我想把按钮组移到底部)…谢谢大家 import java.awt.*; import java.awt.event.*; import java.applet.*; public class moveIt extends Applet implem

大家好,我的小程序的布局似乎有问题,我的小程序中有一个5按钮(集群),位于小程序的顶部(北部)中心,我希望它们位于小程序的底部(南部)中心,有人能帮我把它放到底部吗?-按钮组很好,我只想把它放在我的小程序框架的底部不,像现在一样放在顶部(我想把按钮组移到底部)…谢谢大家

 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

我不确定我是否明白你的意思。。。下面是我如何修改init()使按钮位于底部区域的

我还将画布移到了中间,因为如果它位于北方,则无法看到它,因为它的大小为0x0。无论如何,我不确定这是你真正想要的,所以也许你必须把它移回北方

基本上,我所做的解决按钮问题的方法就是删除您拥有的面板框架,并将BorderLayout管理器设置为小程序

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

我希望能有所帮助。

您能详细说明一下吗?我似乎无法理解您的问题所在。@Gliptal我有一组按钮,我想移动到小程序的底部。。。我使用了frame.add(键盘,BorderLayout.SOUTH);但是他们在北方,怎么会呢?我怎么修复它呢?这可能没什么意义,但是你的
键盘不应该大写。另外,尝试放置
Keypad.setLayout(newborderlayout())
call在将
Keypad
添加到
frame
之前@Gliptal刚刚尝试了仍然不起作用的方法,因此它应该做的是框架有一个边框布局,画布设置为北,小键盘设置为南,键盘有自己的边框布局和自己的组件(北部、南部、东部、西部和中部的按钮-就像框架容器中的容器/主div中有div的div)但由于某种原因,按钮是北部而不是南部,这就是问题的所在,通过一些小的编辑它可以工作…setLayout(new BorderLayout());add(myCanvas,BorderLayout.north);Keypad.setLayout(新建BorderLayout());添加(Keypad,BorderLayout.SOUTH);