Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JLabels/JPanel未显示或刷新_Java_Swing_User Interface_Jlabel - Fatal编程技术网

Java JLabels/JPanel未显示或刷新

Java JLabels/JPanel未显示或刷新,java,swing,user-interface,jlabel,Java,Swing,User Interface,Jlabel,我正在创建一个简单的游戏,玩家可以在2D地图上移动,但是我在显示地图时遇到问题,当玩家移动时,地图会刷新 到目前为止,GUI获得以下形式的映射: X###X ##### ...G. ##### X..GX 在服务器上,这是在构造函数顶部的线程中完成的。然后,该线程被传递给一个函数,该函数应该将显示正确图像的jlabel添加到JPanel中,但是这似乎没有发生,到目前为止没有显示任何内容。我的代码如下: import java.awt.Color; import java.awt.EventQu

我正在创建一个简单的游戏,玩家可以在2D地图上移动,但是我在显示地图时遇到问题,当玩家移动时,地图会刷新

到目前为止,GUI获得以下形式的映射:

X###X
#####
...G.
#####
X..GX
在服务器上,这是在构造函数顶部的线程中完成的。然后,该线程被传递给一个函数,该函数应该将显示正确图像的jlabel添加到JPanel中,但是这似乎没有发生,到目前为止没有显示任何内容。我的代码如下:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextPane;



public class dodGUI {

    public final Vector<String> messageQueue = new Vector<String>();
    public final Vector<String> responseQueue = new Vector<String>();

    final Queue<String> queue = new LinkedList<String>();
    final StringBuilder view = new StringBuilder();

    private JFrame frame = new JFrame();
    final JPanel mainMenu = new JPanel();
    final JPanel humanGameWindow = new JPanel();

    public static void main(String[] args) {
        dodGUI GUI = new dodGUI();
        GUI.frame.setVisible(true);
    }

    public void handleResponse(String response) {
        if(response.equals("WIN")) {
            JOptionPane.showMessageDialog(null,  "YOU WIN!!!");
            mainMenu.setVisible(true);
            humanGameWindow.setVisible(false);
        }

        if (queue.size() < 6) {
            queue.add(response);
        }
        else {
            queue.remove();
            queue.add(response);
        }
        if(queue.contains("LOOKREPLY") && !response.equals("LOOKREPLY")) {
            view.append(response);
        }

        if(view.length() == 25) {
            createImage(view.toString());
            view.delete(0,  25);
        }
    }

    public dodGUI() {

        new Thread(new Runnable() {
            public void run() {
                try {
                    while (true) {
                        String receiving = null;
                        synchronized (responseQueue) {
                            if (responseQueue.size() > 0) {
                                receiving = responseQueue.remove(0);
                            }
                        }
                        if (receiving != null) {
                            handleResponse(receiving);
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (NullPointerException npe2) {
                    npe2.printStackTrace();
                    System.err.println("Null Pointer2: " + npe2);
                }
            }
        }).start();

        frame.getContentPane().setBackground(new Color(255, 255, 255));
        frame.getContentPane().setLayout(null);
        frame.setBounds(100, 100, 500, 630);
        frame.setUndecorated(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //############################################################################################################################################################################

        mainMenu.setLayout(null);
        mainMenu.setBackground(Color.WHITE);
        mainMenu.setBounds(0, 0, 500, 600);
        mainMenu.setVisible(true);

        JLabel mainMenuTitle = new JLabel("DUNGEON OF DOOM!!");
        mainMenuTitle.setForeground(new Color(100, 149, 237));
        mainMenuTitle.setFont(new Font("Moire", Font.BOLD, 28));
        mainMenuTitle.setBounds(50, 13, 380, 50);
        mainMenu.add(mainMenuTitle);

        final JTextPane hostNameTextPane = new JTextPane();
        hostNameTextPane.setToolTipText("Enter the hostname");
        hostNameTextPane.setBackground(new Color(192, 192, 192));
        hostNameTextPane.setBounds(50, 100, 250, 30);
        hostNameTextPane.setFont(new Font("Moire", Font.BOLD, 19));
        mainMenu.add(hostNameTextPane);

        final JTextPane socketNumberTextPane = new JTextPane();
        socketNumberTextPane.setToolTipText("Enter the socket number");
        socketNumberTextPane.setBackground(new Color(192, 192, 192));
        socketNumberTextPane.setBounds(50, 175, 250, 30);
        socketNumberTextPane.setFont(new Font("Moire", Font.BOLD, 19));
        mainMenu.add(socketNumberTextPane);

        JTextPane userNameTextPane = new JTextPane();
        userNameTextPane.setToolTipText("Enter your Username");
        userNameTextPane.setBackground(new Color(192, 192, 192));
        userNameTextPane.setBounds(50, 250, 250, 30);
        userNameTextPane.setFont(new Font("Moire", Font.BOLD, 19));
        mainMenu.add(userNameTextPane);

        JButton mainMenuQuit = new JButton("Quit");
        mainMenuQuit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        mainMenuQuit.setBackground(new Color(100, 149, 237));
        mainMenuQuit.setBounds(220, 345, 70, 55);
        mainMenu.add(mainMenuQuit);

        JButton playGameHuman = new JButton("Play Game Human");
        final dodGUI gui = this;
        playGameHuman.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mainMenu.setVisible(false);
                humanGameWindow.setVisible(true);
                @SuppressWarnings("unused")
                ServerClient newclient = new ServerClient(gui, hostNameTextPane.getText(),
                        Integer.parseInt(socketNumberTextPane.getText()));
            }
        });
        playGameHuman.setBackground(new Color(100, 149, 237));
        playGameHuman.setBounds(50, 345, 150, 55);
        mainMenu.add(playGameHuman);

        frame.add(mainMenu);

        //############################################################################################################################################################################

        humanGameWindow.setLayout(null);
        humanGameWindow.setBackground(Color.WHITE);
        humanGameWindow.setBounds(0, 0, 500, 600);
        humanGameWindow.setVisible(false);

        JLabel gameTitle = new JLabel("DUNGEON OF DOOM!!");
        gameTitle.setForeground(new Color(100, 149, 237));
        gameTitle.setFont(new Font("Moire", Font.BOLD, 28));
        gameTitle.setBounds(93, 13, 380, 50);
        humanGameWindow.add(gameTitle);

        JButton up = new JButton("Up");
        up.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                synchronized (messageQueue) {
                    messageQueue.add("MOVE N");
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException a) {
                        a.printStackTrace();
                    }
                    messageQueue.add("LOOK");
                }
            }
        });
        up.setBackground(new Color(100, 149, 237));
        up.setBounds(274, 483, 100, 55);
        humanGameWindow.add(up);        

        JButton down = new JButton("Down");
        down.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                synchronized (messageQueue) {
                    messageQueue.add("MOVE S");
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException a) {
                        a.printStackTrace();
                    }
                    messageQueue.add("LOOK");
                }
            }
        });
        down.setBackground(new Color(100, 149, 237));
        down.setBounds(274, 540, 100, 55);
        humanGameWindow.add(down);

        JButton left = new JButton("Left");
        left.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                synchronized (messageQueue) {
                    messageQueue.add("MOVE W");
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException a) {
                        a.printStackTrace();
                    }
                    messageQueue.add("LOOK");
                }
            }
        });
        left.setBackground(new Color(100, 149, 237));
        left.setBounds(173, 520, 100, 55);
        humanGameWindow.add(left);

        JButton right = new JButton("Right");
        right.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                synchronized (messageQueue) {
                    messageQueue.add("MOVE E");
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException a) {
                        a.printStackTrace();
                    }
                    messageQueue.add("LOOK");
                }
            }
        });
        right.setBackground(new Color(100, 149, 237));
        right.setBounds(375, 520, 100, 55);
        humanGameWindow.add(right);

        JButton pickup = new JButton("Pickup");
        pickup.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                synchronized (messageQueue) {
                    messageQueue.add("PICKUP");
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException a) {
                        a.printStackTrace();
                    }
                    messageQueue.add("LOOK");
                }
            }
        });
        pickup.setBackground(new Color(100, 149, 237));
        pickup.setBounds(40, 520, 100, 55);
        humanGameWindow.add(pickup);

        JButton Exit = new JButton("Exit");
        Exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        Exit.setBackground(new Color(100, 149, 237));
        Exit.setBounds(427, 17, 70, 40);
        humanGameWindow.add(Exit);

        frame.add(humanGameWindow);
    }

        //##################################################################################################################################################

        public void createImage(String view) {

            final JPanel fiveByFiveLook = new JPanel();
            final JPanel sevenBySevenLook = new JPanel();

            String [] viewSplit = view.split("(?!^)");

            if(viewSplit.length == 25) {

                System.out.println("GOT HERE");
                fiveByFiveLook.setLayout(null);
                fiveByFiveLook.setBackground(Color.WHITE);
                fiveByFiveLook.setBounds(48, 75, 404, 404);
                fiveByFiveLook.setVisible(true);


                ArrayList<String> viewArray = new ArrayList<String>(Arrays.asList(viewSplit));


                JLabel fiveByFiveLabels[][] = new JLabel[5][5];

                for(int i = 0, k = 0; i <= 4; i++, k = k + 80) {
                    for(int j = 0, l = 0; j <= 4; j++, l = l + 80) {
                        fiveByFiveLabels[i][j] = new JLabel();
                        if(viewArray.get(0).equals("X")) {
                            fiveByFiveLabels[i][j].setIcon(new ImageIcon("Lantern.jpg"));
                            fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
                            viewArray.remove(0);
                        }
                        else if(viewArray.get(0).equals("#")) {
                            fiveByFiveLabels[i][j].setIcon(new ImageIcon("Wall.jpg"));
                            fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
                            viewArray.remove(0);
                        }
                        else if(viewArray.get(0).equals(".")) {
                            fiveByFiveLabels[i][j].setIcon(new ImageIcon("Floor.jpg"));
                            fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
                            viewArray.remove(0);
                        }
                        else if(viewArray.get(0).equals("E")) {
                            fiveByFiveLabels[i][j].setIcon(new ImageIcon("Exit.jpg"));
                            fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
                            viewArray.remove(0);
                        }
                        else if(viewArray.get(0).equals("G")) {
                            fiveByFiveLabels[i][j].setIcon(new ImageIcon("Gold.png"));
                            fiveByFiveLook.add(fiveByFiveLabels[i][j]); 
                            viewArray.remove(0);
                        }
                        fiveByFiveLabels[i][j].setBounds(l, k, 85, 85);
                    }
                }
                humanGameWindow.add(fiveByFiveLook);
            }

            else if(viewSplit.length == 49) {

                sevenBySevenLook.setLayout(null);
                sevenBySevenLook.setBackground(Color.WHITE);
                sevenBySevenLook.setBounds(48, 75, 404, 404);
                sevenBySevenLook.setVisible(true);

                JButton sevenBySevenLabels[][] = new JButton[7][7];

                for(int i = 0, k = 0; i <= 6; i++, k = k + 57) {
                    for(int j = 0, l = 0; j <= 6; j++, l = l + 57) {
                        sevenBySevenLabels[i][j] = new JButton();
                        sevenBySevenLabels[i][j].setBounds(l, k, 62, 62);
                        //sevenBySevenLabels[i][j].setIcon(new ImageIcon("Sword copy.jpg"));
                        sevenBySevenLook.add(sevenBySevenLabels[i][j]);
                    }
                }
                humanGameWindow.add(sevenBySevenLook);
            }
        }
}
导入java.awt.Color;
导入java.awt.EventQueue;
导入java.awt.Font;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.LinkedList;
导入java.util.Queue;
导入java.util.Vector;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
导入javax.swing.JTextPane;
公共类dodGUI{
public final Vector messageQueue=new Vector();
公共最终向量响应=新向量();
最终队列=新建LinkedList();
最终StringBuilder视图=新建StringBuilder();
私有JFrame=新JFrame();
最终JPanel主菜单=新JPanel();
最终JPanel humanGameWindow=新JPanel();
公共静态void main(字符串[]args){
dodGUI=新的dodGUI();
GUI.frame.setVisible(true);
}
公共无效句柄响应(字符串响应){
if(response.equals(“WIN”)){
showMessageDialog(null,“youwin!!!”);
mainMenu.setVisible(真);
humanGameWindow.setVisible(false);
}
if(queue.size()<6){
添加(响应);
}
否则{
queue.remove();
添加(响应);
}
if(queue.contains(“LOOKREPLY”)&&!response.equals(“LOOKREPLY”)){
视图。追加(响应);
}
如果(view.length()==25){
createImage(view.toString());
视图。删除(0,25);
}
}
公共图书馆{
新线程(newrunnable()){
公开募捐{
试一试{
while(true){
字符串接收=null;
已同步(响应队列){
如果(responseQueue.size()>0){
接收=响应请求删除(0);
}
}
如果(接收!=null){
HandlerResponse(接收);
}
试一试{
睡眠(50);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}捕获(NullPointerException npe2){
npe2.printStackTrace();
System.err.println(“空指针2:+npe2”);
}
}
}).start();
frame.getContentPane().setBackground(新颜色(255、255、255));
frame.getContentPane().setLayout(null);
机架立根(100100500630);
框架。设置未装饰(假);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//############################################################################################################################################################################
mainMenu.setLayout(空);
主菜单.背景(颜色.白色);
主菜单.设置边界(0,0,500,600);
mainMenu.setVisible(真);
JLabel mainmonutile=新的JLabel(“厄运地牢!!”;
设置前景(新颜色(100149237));
main menutitle.setFont(新字体(“莫尔”,Font.BOLD,28));
主立根(50,13380,50);
mainMenu.add(mainMenuTitle);
最终JTextPane主机名textpane=新JTextPane();
hostNameTextPane.setToolTipText(“输入主机名”);
setBackground(新颜色(192192192));
hostNameTextPane.setBounds(50、100、250、30);
hostNameTextPane.setFont(新字体(“Moire”,Font.BOLD,19));
mainMenu.add(主机名文本窗格);
最终JTextPane socketNumberTextPane=新JTextPane();
setToolTipText(“输入套接字编号”);
setBackground(新颜色(192192192));
socketNumberTextPane.setBounds(5017525030);
setFont(新字体(“莫尔”,Font.BOLD,19));
主菜单.添加(socketNumberTextPane);
JTextPane userNameTextPane=新的JTextPane();
setToolTipText(“输入您的用户名”);
setBackground(新颜色(192192192));
userNameTextPane.setBounds(5025025030);
userNameTextPane.setFont(新字体(“Moire”,Font.BOLD,19));
mainMenu.add(用户名文本窗格);
JButton mainMenuQuit=新JButton(“Quit”);
mainMenuQuit.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
系统出口(0);
}
});
mainMenuQuit.setBackground(新颜色(100149237));
主菜单退出。挫折(220、345、70、55);
mainMenu.add(mainMenuQuit);
JButton playGameHuman=新JButton(“Play Game Human”);
最终dodGUI=此;
playGameHuman.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
main menu.setVisible(假);
humanGameWindow.setVisible(true);
@抑制警告(“未使用”)
ServerClient newclient=newserverclient(gui,hostNameTextPane.getText(),
整数