Jeg har et problem med mit program - skal have mit JLabel input vist på skærmen - det får jeg dog ikke.. Meget frustrerende. Vil have den til at blive vist under bg(png).
Window.java
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Image;
- import java.awt.List;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
-
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- /**
- * Game DotMS
- * @author Jacob Andreas Sneding Rohde
- * @version 1.0
- */
- public class Window extends JFrame
- {
- public static Window window;
- Keylistener keylistener;
-
- int windowx, windowy; // Window size
- int charlesx, charlesy; // Where charles is positioned
-
- // For the taskbar
- ArrayList<ImageIcon> icons;
-
- boolean gameIsRunning;
-
- //For the pictures
- JLabel bglabel, characterlabel, pantslabel, shirtlabel, sockslabel, tanktoplabel, underpantslabel, wiglabel, menulabel, kisslabel, gameoverlabel, closetlabel;
- boolean bpants, bshirt, bsocks, btanktop, bunderpants, bwig;
-
- // Stages
- boolean menu, game, closet, gameOver;
-
- // For the prompt
- String command = "Hello";
- String outputString = "";
- JTextArea output; // Output, where the response will get shown i:
- JLabel input; // For the commands
-
-
- public ImageIcon bg, charles, pants, shirt, socks, tanktop, underpants, wig, clothes, kiss, menupic, gameoverpic;
- Image taskbarIcon;
-
- // CONTAINER
- JPanel container;
-
- public Window() // ONLY MAKE ONE OBJECT OF EACH CLASS!!!!
- {
- System.out.println("Remeber to check if it prints out \"succesfully started program!\"");
- //panel.add(output, 0, 800);
- windowx = 1000;
- windowy = 750;
-
- charlesx = 50;
- charlesy = 60;
-
- keylistener = new Keylistener(this);
- this.setFocusable(true);
- this.addKeyListener(keylistener);
- this.setBackground(Color.black);
-
- // The Panel of the window, the "container"
- container = new JPanel();
- this.add(container);
- container.setSize(windowx, windowy);
- container.setLayout(null);
- container.setOpaque(true);
- container.setBackground(Color.BLACK);
-
- // For the output stupid
- output = new JTextArea();
- output.setFocusable(false);
- output.setForeground(Color.WHITE);
- output.setBackground(Color.BLACK);
- output.setEditable(false);
- output.setText(outputString);
- output.setLineWrap(true);
-
- // For the input pendejo
- input = new JLabel();
- input.setText("olwaihdoisahd");
- input.setFocusable(true);
- input.setBackground(Color.black);
- input.setForeground(Color.white);
- input.setBounds(5, 605, 600, 605);
- container.add(input);
-
-
- init();
-
- this.setTitle("MC Charles - Put it on");
- this.setVisible(true);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.setSize(windowx, windowy);
- // For the taskbar icon
- this.setIconImage(taskbarIcon);
- System.out.println("Succesfully added a icon to the taskbar");
- startGame();
- }
-
- public void addPicture(ImageIcon img)
- {
-
- }
-
- public void startGame()
- {
- gameIsRunning = true;
- menu = true;
- game();
- }
-
- public void game()
- {
- boolean succes = false;
-
- while(gameIsRunning == true)
- {
- if(menu == true)
- {
- container.add(menulabel);
- if(succes == false)
- {
- System.out.println("Succesfully stated the program");
- succes = true;
- }
- }
-
- else if(game == true)
- {
- container.add(bglabel);
- container.add(characterlabel);
- }
-
- else if(closet == true)
- {
- container.add(closetlabel);
- }
-
- else if(gameOver == true)
- {
- container.add(gameoverlabel);
- }
- }
- }
-
- // Init pics and JLabels
- public void init()
- {
- // Init the ImageIcons
- menupic = new ImageIcon("res/menu.png");
- kiss = new ImageIcon("res/kiss.png");
- gameoverpic = new ImageIcon("res/gameOver.png");
- clothes = new ImageIcon("res/Closet.png");
- bg = new ImageIcon("res/Background.png");
- charles = new ImageIcon("res/charles.png");
- pants = new ImageIcon("res/pants.png");
- shirt = new ImageIcon("res/shirt.png");
- socks = new ImageIcon("res/socks.png");
- tanktop = new ImageIcon("res/tanktop.png");
- underpants = new ImageIcon("res/underpants.png");
- wig = new ImageIcon("res/wig.png");
-
- //Init the JLabels and positioning them
- menulabel = new JLabel(menupic);
- menulabel.setBounds(0, 0, 800, 600);
- kisslabel = new JLabel(kiss);
- kisslabel.setBounds(0, 0, 800, 600);
- gameoverlabel = new JLabel(gameoverpic);
- gameoverlabel.setBounds(0, 0, 800, 600);
- closetlabel = new JLabel(clothes);
- closetlabel.setBounds(0, 0, 800, 600);
- bglabel = new JLabel(bg);
- bglabel.setBounds(0, 0, 800, 600);
- characterlabel = new JLabel(charles);
- characterlabel.setBounds(charlesx, charlesy, 300, 400);
- pantslabel = new JLabel(pants);
- pantslabel.setBounds(charlesx, charlesy, 300, 400);
- shirtlabel = new JLabel(shirt);
- shirtlabel.setBounds(charlesx, charlesy, 300, 400);
- sockslabel = new JLabel(socks);
- sockslabel.setBounds(charlesx, charlesy, 300, 400);
- tanktoplabel = new JLabel(tanktop);
- tanktoplabel.setBounds(charlesx, charlesy, 300, 400);
- underpantslabel = new JLabel(underpants);
- underpantslabel.setBounds(charlesx, charlesy, 300, 400);
- wiglabel = new JLabel(wig);
- wiglabel.setBounds(charlesx, charlesy, 300, 400);
-
- //For the taskbar
- try
- {
- taskbarIcon = ImageIO.read(new File("res/TaskbarIcon.png"));
- }
- catch (IOException e)
- {
- System.out.println("Couldn't init taskbarIcon");
- }
- }
-
- // Refreshing the output for the prompt
- public void refreshOutput()
- {
- output.setText(outputString.toUpperCase());
- }
- // Refreshing the input for the prompt
- public void refreshInput()
- {
- input.setText("\n> " + command);
- }
-
- public static void main(String[] args)
- {
- window = new Window();
- }
- }
Keylistener.java
- import java.awt.event.KeyListener;
- import java.awt.event.KeyEvent;
- /**
- * Game DotMS
- * @author Jacob Andreas Sneding Rohde
- * @version 1.0
- */
- public class Keylistener implements KeyListener
- {
- boolean bpants, bshirt, bsocks, btanktop, bunderpants, bwig;
-
- Window window;
-
- public Keylistener(Window w)
- {
- window = w;
- }
-
- public void keyPressed(KeyEvent e)
- {
- int keyTyped = e.getKeyCode();
-
- if(keyTyped == KeyEvent.VK_1)
- {
- window.command += "1";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_2)
- {
- window.command += "2";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_3)
- {
- window.command += "3";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_4)
- {
- window.command += "4";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_5)
- {
- window.command += "5";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_6)
- {
- window.command += "6";
- window.refreshInput();
- }
- else if(keyTyped == KeyEvent.VK_7)
- {
- window.command += "7";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_8)
- {
- window.command += "8";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_9)
- {
- window.command += "9";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_0)
- {
- window.command += "0";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_COMMA)
- {
- window.command += ",";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_PERIOD)
- {
- window.command += ".";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_A)
- {
- window.command += "A";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_B)
- {
- window.command += "B";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_C)
- {
- window.command += "C";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_D)
- {
- window.command += "D";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_E)
- {
- window.command += "E";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_F)
- {
- window.command += "F";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_G)
- {
- window.command += "G";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_H)
- {
- window.command += "H";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_I)
- {
- window.command += "I";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_J)
- {
- window.command += "J";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_K)
- {
- window.command += "K";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_L)
- {
- window.command += "L";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_M)
- {
- window.command += "M";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_N)
- {
- window.command += "N";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_O)
- {
- window.command += "O";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_P)
- {
- window.command += "P";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_Q)
- {
- window.command += "Q";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_R)
- {
- window.command += "R";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_S)
- {
- window.command += "S";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_T)
- {
- window.command += "T";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_U)
- {
- window.command += "U";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_V)
- {
- window.command += "V";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_W)
- {
- window.command += "W";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_X)
- {
- window.command += "X";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_Y)
- {
- window.command += "Y";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_Z)
- {
- window.command += "Z";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_SPACE)
- {
- window.command += " ";
- window.refreshInput();
- }
-
- else if(keyTyped == KeyEvent.VK_BACK_SPACE)
- {
- int delete = window.command.length() - 1;
- window.command = window.command.substring(0, delete);
-
- window.refreshInput();
- }
-
- /* if(keyTyped == KeyEvent.VK_ENTER)
- {
-
- String addOutput
-
- window.outputString += "\n>>> ";
- window.outputString += addOutput;
- window.refreshOutput();
- window.command = "";
- window.refreshInput();
- } */
-
- }
-
- public void keyReleased(KeyEvent e)
- {
-
- }
-
- public void keyTyped(KeyEvent e)
- {
-
- }
-
- }