添加从左到右的JPanels AND键盘输入



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

2。当我添加键盘.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();
        }
    }
}
}

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

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();
}

相关内容

  • 没有找到相关文章

最新更新