从左到右添加JPanel和键盘输入

时间:2014-06-18 03:31:24

标签: java swing keyboard jpanel boxlayout

1。 我无法按顺序添加我的JPanel。从本质上讲,用户将选择1到4"无人机"对象同时运行。如果他们选择两个,布局应该添加2"无人机"从左到右的JFrame面板。当前代码只是将它们堆叠在一起。

2。 当我添加keyboard.nextInt();在程序中,它初始化JFrame,然后冻结。

public class RunDrones {

public int numDrones;
final JFrame display;
private final JLabel howMany;
private MakeDrone DronePane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            new RunDrones();
        }
    });
}

public RunDrones() {
    //Scanner keyboard = new Scanner(System.in);

    display = new JFrame();
    display.setLayout(new GridLayout());
    JPanel container = new JPanel();
    container.setLayout(new BoxLayout(container,BoxLayout.LINE_AXIS));

    DronePane = new MakeDrone();
    DronePane.setSize(200, 400);
    DronePane.setBorder(BorderFactory.createLineBorder(Color.black));

    howMany = new JLabel("How many Drones (1-4): ", JLabel.CENTER);
    howMany.setFont(new Font("Verdana", 1, 25));

    display.setSize(800, 400);
    display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    display.setLocationRelativeTo(null);
    container.add(howMany);
    display.add(container);
    display.setVisible(true);

    //numDrones = keyboard.nextInt();

    numDrones = 2;

    if (numDrones >= 5) {
        howMany.setText("BETWEEN 1 AND 4 IDIOT. Try again: ");
        //numDrones = keyboard.nextInt();
    }
    else {
        container.remove(howMany);
        switch (numDrones){
        case 4:
            Timer drone1 = new Timer(30, new Drone(DronePane));
            container.add(DronePane);
            drone1.setInitialDelay(0);
            drone1.start();
        case 3:
            Timer drone2 = new Timer(30, new Drone(DronePane));
            container.add(DronePane);
            drone2.setInitialDelay(0);
            drone2.start();
        case 2:
            Timer drone3 = new Timer(30, new Drone(DronePane));
            container.add(DronePane);
            drone3.setInitialDelay(0);
            drone3.start();
        case 1:
            Timer drone4 = new Timer(30, new Drone(DronePane));
            container.add(DronePane);
            drone4.setInitialDelay(0);
            drone4.start();
        }
    }
}

public static int getRandom() {
    int firstNum = new Random().nextInt(5) + 1;
    int secondNum = new Random().nextInt(5) + 1;
    return firstNum - secondNum;
}

public class MakeDrone extends JPanel {

    private int yPos;

    public void setYPos(int yPos) {
        this.yPos = yPos;
        repaint();
    }

    public int getYPos() {
        return yPos;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); 

        g.setColor(Color.BLACK);
        int y = ((getHeight() / 2) - 90);
        int x = ((getWidth() / 2) - 45);
        g.drawRect(x, y, 90, 180);

        y = (getHeight() + getYPos()) / 2;
        //        y = (getHeight() / 2) + getYPos();
        g.setColor(getForeground());
        g.fillRect(x, y, 90, 12);

    }

}

public class Drone implements ActionListener {

    private int droneStatus;
    private MakeDrone levelBar;

    public Drone(MakeDrone levelBar) {
        this.levelBar = levelBar;
        levelBar.setForeground(Color.BLACK);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (droneStatus >= -85 && droneStatus <= 85) {
            droneStatus += getRandom();
            levelBar.setYPos(droneStatus);
            if (droneStatus < -40 && droneStatus > -85) {
                levelBar.setForeground(Color.ORANGE);
            }
            if (droneStatus < 85 && droneStatus > 40) {
                levelBar.setForeground(Color.ORANGE);
            }
            if (droneStatus <= 40 && droneStatus >= -40) {
                levelBar.setForeground(Color.BLACK);
            }
        } else {
            levelBar.setForeground(Color.RED);
            ((Timer) e.getSource()).stop();
        }
    }

}
}

1 个答案:

答案 0 :(得分:1)

根据您需要的列数添加无人机之前更改布局...

container.remove(howMany);
container.setLayout(new GridLayout(1, numDrones));
switch (numDrones){
    case 4:
    //...

不要忘记,如果需要,您需要在之后重置布局;)

<强>更新...

“其他”问题是您要将MakeDrone的实例添加到容器中。组件的实例只能驻留在单个父级上,所以实际上,您只添加了DronePane一次。

相反,您需要为每列创建MakeDrone的新实例。尝试使用更像

的内容替换switch语句
for (int index = 0; index < numDrones; index++) {
    MakeDrone md = new MakeDrone();
    Timer timer = new Timer(30, new Drone(md));
    container.add(md);
    timer.setInitialDelay(0);
    timer.start();
}