将多个面板添加到单个面板/框架中

时间:2014-11-13 12:30:02

标签: java swing

我正在使用挥杆进行二维足球比赛。我在不同的课程中制作了不同的组件。我在单个面板上组合所有这些元素时遇到了麻烦,因此我无法从单个类中处理所有这些组件......这是我的代码。请帮忙

//first class ball- which has ball physics


public class Ball extends JPanel{
private static final long serialVersionUID = 1L;
public int x;
public int y;
public int vx=1;
public int vy=1;
Random a = new Random();
public Ball(){
    this.x=a.nextInt(500);
    this.y=a.nextInt(400);
}
private void physics(){
    x += vx;
    y += vy;
    if(x>(getWidth()-20)){
        vx*=-1;
    }
    if(y>(getHeight()-20)){
        vy*=-1;
    }
    if(x<0&&vx<0){
        vx*=-1;
    }
    if(y<0&&vy<0){
        vy*=-1;
    }
}
public void paint(Graphics g){
    super.paint(g);
    Graphics2D cicle = (Graphics2D) g;
    cicle.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Graphics2D bg = (Graphics2D) g;
    bg.setColor(new Color(114, 255, 72,255));
    bg.fillRect(0, 0,getWidth() , getHeight());
    cicle.setColor(new Color(206, 72, 255,255));
    cicle.fillOval(x, y, 20, 20);

    Thread temp = new Thread(){
        public void run(){
            physics();
            try {
                Thread.sleep(16);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            repaint();
        }
    };
    temp.start();
}
}

//第二类播放器 - 通过箭头键移动vox

public class Team extends JPanel   implements ActionListener,KeyListener{
private Timer timer=new Timer(1,this);
private Attacker n=new Attacker(20,20,10);
public Team(){
    timer.start();
    setFocusable(true);
    addKeyListener(this);
    setFocusTraversalKeysEnabled(false);
}
public void paint(Graphics g){
    super.paint(g);
    Graphics2D p = (Graphics2D) g;
    p.fillRect(n.getX(), n.getY(), n.getSide(), n.getSide());
    }
public void actionPerformed(ActionEvent e) {
    repaint();
}
public void keyPressed(KeyEvent e){
    int keycode = e.getKeyCode();
    if(keycode == KeyEvent.VK_UP){
        n.move(-1);
    }
    else if(keycode == KeyEvent.VK_DOWN){   
        n.move(1);
    }
}
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}
}

//这只是玩家信息

public class Attacker extends Player implements Observer,Shootball{
private int x;
private int y;
private int side;
public int getSide() {
    return side;
}
public void setSide(int side) {
    this.side = side;
}
public Attacker (int x,int y,int side){
    this.x=x;
    this.y=y;
    this.side=side;
}
public void shootball() {
    // TODO Auto-generated method stub

}
public int getX() {
    return x;
}
public void setX(int x) {
    this.x = x;
}
public int getY() {
    return y;
}
public void setY(int y) {
    this.y = y;
}
public void move(int value){
    this.y+=value;
}

}

//主类中的代码

    public Foosball_Table()
    {
    setTitle("Background Color for JFrame");
    setSize(1000,600);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setResizable(false);


    add(new ball());
    add(new Player());

    // Just for refresh :) Not optional!
    setSize(999,599);
    setSize(1000,600);

}

只显示一个框而不显示任何其他内容

3 个答案:

答案 0 :(得分:1)

简单..使用LayoutManager ..例如setLayout(new FlowLayout())。更多参考https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

public Foosball_Table()
{
    setTitle("Background Color for JFrame");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setResizable(false);
    setLayout(new FlowLayout())
    add(new ball());
    add(new Player());
    // Just for refresh :) Not optional!
    pack();
}

答案 1 :(得分:0)

您需要开始使用Layout。有关“布局”的基本教程,请docs.oraclehttps://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

通常,您在Layout中组合在一起的不同面板中拥有不同的组件(在本例中为Player和Ball,甚至是背景字段)。通常的选择是FlowLayout,但您可能需要一个网格并使用面板的绝对定位。本教程涵盖了这一点。

另外,花一些时间做一下swing或JavaFX的教程(对你的项目来说是一个更明智和现代的选择),正如你所调用的那两个最后一个代码行,“刷新”它,给了我青光眼。

答案 2 :(得分:0)

请提供有关您的课程ballPlayer的更多信息。看起来很可能,这些类不是复合而是代表对象。可能您需要将这些类实例化为对象,例如

ball myBall = new ball();
Player myPlayer = new Player();

但我们只能猜测您目前提供的信息。

此外,Java类名称应始终以大写字母开头。我建议您将ball课程重命名为Ball。您也必须将代码文件重命名为Ball.java