大家好我似乎对我的applet布局有问题,我在applet中有一个5按钮(群集)位于顶部(北)中心我希望它们位于底部(南)applet的中心,任何人都可以帮助它到底部吗? - 按钮集群很好我只是希望它在我的applets框架的底部没有像现在这样的顶部(我想移动按钮集群)到底部)...谢谢你们
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
答案 0 :(得分:1)
我不确定我是否明白了......这就是我如何修改init()以使按钮位于底部区域。
我还将画布移动到中心,因为如果你在北方有它,你就看不到它,因为它是0x0大小。无论如何,我不确定这是你真正想要的,所以也许你必须把它移回到北方。
基本上我解决按钮问题的方法就是删除你的Panel框架并将BorderLayout manager设置为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
我希望它有所帮助。