Привязки клавиш или списки мыши не работают после нажатия кнопки даже после того, как на кнопке установлен фокус falseJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Привязки клавиш или списки мыши не работают после нажатия кнопки даже после того, как на кнопке установлен фокус false

Сообщение Anonymous »

Вот основной класс, в котором я все настроил.
package game;
import eneimes.hostile;
import item.*;
import objects.*;
import render.nextframe;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class Main {
public static JFrame force = new JFrame("force 2: a new hope");
public static ArrayList objectlistold = new ArrayList();
public static ArrayList objectlist = new ArrayList();
public static ArrayList shopobjectlist = new ArrayList();
public static ArrayList hostilelist = new ArrayList();
public static ArrayList inventory = new ArrayList();
public static int currentlistslot =0;
public static int unpoweredgens =0;
public static int nextemptyinv =0;
public static int playerhealth =100;
public static boolean playeralive =true;
public static boolean playercanmove =true;
public static boolean inshop =false;
public static int currentinvslot =0;
public static int MONEY =0;
public static int doornum =0;
public static Rectangle player = new Rectangle(20,350,20,50);
public static boolean playerhidden;
public static boolean Darkroom;
public static boolean DEBUGMODE;
public static boolean GODMOD;
public static void main(String[] args) {
force.setExtendedState(JFrame.MAXIMIZED_BOTH);
force.setUndecorated(true);
force.setFocusable(true);
force.getContentPane().setBackground(Color.darkGray);
force.getContentPane().setForeground(Color.darkGray);
force.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
force.setSize(width,height);
//set up movement and inputs
rightmove right = new rightmove();
InputMap in = force.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = force.getRootPane().getActionMap();
in.put(KeyStroke.getKeyStroke(KeyEvent.VK_D,0),"right");
am.put("right",right);
//KeyListener sus;
/*force.addKeyListener(sus = new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
char keyChar = e.getKeyChar();
int keycode = e.getKeyCode();
//set the varibles to a blankslate so resets work if your reading this its left over after death is added, might readd for easy reseting
/*if(keyChar == 'r'){

MONEY =0;
unpoweredgens=0;
playerhealth =100;
doornum=0;
playerhidden=false;
objectlistold.clear();
inventory.clear();
int listpointer =0;
while(listpointer inventory.size()-1){
currentinvslot=0;
}
currentinvslot++;
if(currentinvslot > inventory.size()-1){
currentinvslot=0;
}
}
}
//ACTIVATES DEBUG MAY CAUSE BUGS
if (keyChar == 'Z') {
DEBUGMODE = !DEBUGMODE;
}
//DEBUG COMMANDS
//COMMANDS BELOW SHOULD NOT BE USED DURING NORMAL GAMEPLAY AND MAY CONTAIN BUG
if ((DEBUGMODE)) {
if (keycode == KeyEvent.VK_H) {
Main.hostilelist.add(new MINIGAME());
}
if (keycode == KeyEvent.VK_L) {
Darkroom = !Darkroom;
}
if (keycode == KeyEvent.VK_G) {
GODMOD = !GODMOD;
}
if (keycode == KeyEvent.VK_S) {
Main.hostilelist.add(new searchlite());
}
if (keycode == KeyEvent.VK_I) {
roomgen.roomsize = 2000;
objectlist.clear();
objectlist.add(new door(10, 340, true));
drawer drawer = new drawer(80, 360);
drawer.containskey = true;
doornum = 62;
objectlist.add(drawer);
objectlist.add(new generator(130, 370));
objectlist.add(new desk(170, 360));
objectlist.add(new locker(240, 334));
objectlist.add(new shopobject(300, 365));
MONEY = 10000;
}
if (keycode == KeyEvent.VK_K) {
boolean gave = false;
cardhacker cardhacker = new cardhacker();
int pointer = 0;
while (pointer < Main.inventory.size()) {
if (Main.inventory.get(pointer) != null) {
if (Main.inventory.get(pointer).getClass() == cardhacker.class) {
gave = true;
Main.inventory.set(pointer, cardhacker);
}
}
pointer++;
}
if (!gave) {
Main.inventory.add(cardhacker);
Main.nextemptyinv++;
}
}
if (keycode == KeyEvent.VK_O) {
boolean gave = false;
flashlite flashlite = new flashlite();
int pointer = 0;
while (pointer < Main.inventory.size()) {
if (Main.inventory.get(pointer) != null) {
if (Main.inventory.get(pointer).getClass() == flashlite.class) {
gave = true;
Main.inventory.set(pointer, flashlite);
}
}
pointer++;
}
if (!gave) {
Main.inventory.add(flashlite);
Main.nextemptyinv++;
}
}
}
}
}

@Override
public void keyReleased(KeyEvent e) {

}
});*/
force.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
int button = e.getButton();
System.out.println("e");
if(playercanmove) {
if (button == 1) {
if (currentinvslot < inventory.size()) {
item current = inventory.get(currentinvslot);
if (current != null) {
current.used();
}
}
}
}
}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}
});
//should be menu code but the buttons being pressed cause ton of issues for some reason they just being there does not it's only when clicked they do
JButton Start = new JButton("start");
JButton Settingsorcredits = new JButton("");
JButton online = new JButton("online");
JLabel forcetext = new JLabel();
JLabel version = new JLabel("version 0.7" );
Start.setFocusable(false);
online.setFocusable(false);
Settingsorcredits.setFocusable(false);
version.setFocusable(false);
forcetext.setFocusable(false);
force.setLayout(null);
force.add(Start);
force.add(Settingsorcredits);
forcetext.setForeground(Color.blue);
version.setForeground(Color.blue);
force.add(forcetext);
force.add(online);
force.add(version);
forcetext.setFont(new Font("e", Font.BOLD, 40));
forcetext.setText("FORCE");
forcetext.setBounds(force.getBounds().width/2-75, force.getBounds().height/2-200,200,100);
version.setBounds(force.getBounds().width/2-50, force.getBounds().height/2-175,100,100);
Start.setBounds(force.getBounds().width/2-150,force.getBounds().height/2,300,40);
online.setBounds(force.getBounds().width/2-150,force.getBounds().height/2+50,300,40);
Settingsorcredits.setBounds(force.getBounds().width/2-150,force.getBounds().height/2+100,300,40);
force.setVisible(true);

//this code should be removed when the menu work
/*new nextframe();
//generates first roomnew
new roomgen();
force.requestFocus();
//starts the game loop
try {
new gamecode();
} catch (AWTException ignored) {
}*/

Start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
force.removeAll();
force.revalidate();
new nextframe();
//generates first roomnew
new roomgen();
force.requestFocus();
//starts the game loop
try {
new gamecode();
} catch (AWTException ignored) {
}

}
});
online.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
online.setText("Not added yet :(");
}
});

}
static class rightmove extends AbstractAction{
private int move =0;
public rightmove() {
System.out.println("test");
if(!Main.playerhidden && Main.playeralive){
move = 4;
if(player.x+move > roomgen.roomsize-player.width){
move=0;
}
}
}
public void actionPerformed(ActionEvent e){
System.out.println("test");
player.x += 4;
}
}
}

поэтому, когда вы нажимаете кнопку «Пуск», ни привязки клавиш, ни прослушиватель мыши не работают.
Я попробовал сделать так, чтобы кнопки не могли получить фокус
Я переключился на сочетания клавиш ниже из закомментированного прослушивателя клавиш, но это не помогло. все говорит, что он должен игнорировать фокус и работу.
это только для правильного движения, поскольку я просто пытался проверить, работают ли сочетания клавиш
InputMap in = force.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = force.getRootPane().getActionMap();
in.put(KeyStroke.getKeyStroke(KeyEvent.VK_D,0),"right");
am.put("right",right);


если я раскомментирую этот код внизу, он работает, но пропускает меню, которое сообщает мне, что что-то с кнопками, но все, что я пробовал, не работает
/*new nextframe();
//generates first roomnew
new roomgen();
force.requestFocus();
//starts the game loop
try {
new gamecode();
} catch (AWTException ignored) {
}*/


Подробнее здесь: https://stackoverflow.com/questions/792 ... -the-focus
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Привязки клавиш не работают после нажатия кнопки даже после того, как фокус на кнопке установлен на false
    Anonymous » » в форуме JAVA
    0 Ответы
    20 Просмотры
    Последнее сообщение Anonymous
  • Объединение нажатия клавиш и нажатия клавиш – jQuery
    Anonymous » » в форуме Jquery
    0 Ответы
    51 Просмотры
    Последнее сообщение Anonymous
  • Как использовать привязки клавиш вместо прослушивателей клавиш
    Anonymous » » в форуме JAVA
    0 Ответы
    17 Просмотры
    Последнее сообщение Anonymous
  • C# CefSharp chromeBrowser Попытка имитировать нажатия клавиш и щелчки мыши
    Anonymous » » в форуме C#
    0 Ответы
    25 Просмотры
    Последнее сообщение Anonymous
  • Кнопка получает фокус, даже если для TabStop установлено значение false
    Anonymous » » в форуме C#
    0 Ответы
    11 Просмотры
    Последнее сообщение Anonymous

Вернуться в «JAVA»