为什么我不能同时打开JPanel并运行循环

时间:2016-11-24 17:19:03

标签: java eclipse swing loops jpanel

我试图在gui中实时查看控制器的输入。我用的是.poll();从控制器轴获取新信息的方法。我需要不断轮询它以使元素实时移动但我只能运行循环或打开窗口,而不是两者。

这是我已经归结为循环和gui元素的代码,它运行循环并每秒打印出“hello”但不打开gui元素:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

public class movedot extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    movedot frame = new movedot();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @throws InterruptedException 
     */
    public movedot() throws InterruptedException {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblDot = new JLabel("dot");
        lblDot.setBounds(0,0,2,2);
        contentPane.add(lblDot);
        loop();
    /*  while (true){
            lblDot.setBounds(new Rectangle(new Point(x++,y++),lblDot.getPreferredSize()));
        Thread.sleep(1000);
    }*/
    }
    public void loop() throws InterruptedException{
        while (true){
        System.out.println("hello");
        Thread.sleep(1000);
    }
    }
}

这就是我对控制器的实现,与此相同,运行逻辑并打印出最终数字但不打开gui(那里可能还有很多额外的东西我还需要清理):

package controller2;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Controller;
import org.lwjgl.input.Controllers;


public class controllergui {

    private JFrame frame;
    static Controller controller;
    public static boolean Start;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    controllergui window = new controllergui();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public controllergui() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    public void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 218, 241);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);





//logic     
        try {
            Controllers.create();
        } catch (LWJGLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            controller=Controllers.getController(16);
            JLabel label = new JLabel("");
            label.setIcon(new ImageIcon("C:\\Users\\The Pheonix\\workspace\\controller2\\img\\dot.jpg"));
            label.setBounds(100, 100, 2, 2);
            frame.getContentPane().add(label);
            while(true){
                controller.poll();
                Start=controller.isButtonPressed(0);
            //      System.out.println(Start);
            //      System.out.println(controller.getAxisValue(0));
                    float axis1= controller.getAxisValue(1);
                    float axis2= controller.getAxisValue(3);
            //      System.out.println(controller.getAxisValue(2));
            //      System.out.println(controller.getAxisValue(3));
            //      System.out.println(controller.getAxisValue(4));
            //      System.out.println(controller.getAxisValue(5));
                    float axis1Stage1 = axis1 + 1 ;
                    float axis1Stage2 = axis1Stage1 *100;
                    int finalNumber = Math.round(axis1Stage2);
                    System.out.println(finalNumber);
                //  System.out.println(axis3Stage1);


            }
    }
}

0 个答案:

没有答案