我正在尝试编写一个使用java创建GUI的代码,在按下键盘上的按钮后,GUI中的相关按钮会改变它的颜色。您可以检查代码,因为颜色变化没有发生。谢谢
package controller;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import java.util.Enumeration;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;
public class GUI extends JPanel implements ActionListener, KeyListener {
boolean running = true;
int frontKey = KeyEvent.VK_W;
int backKey = KeyEvent.VK_S;
int leftKey = KeyEvent.VK_A;
int rightKey = KeyEvent.VK_D;
static boolean backKeyPressed = false;
static boolean frontKeyPressed = false;
static boolean leftKeyPressed = false;
static boolean rightKeyPressed = false;
int listenKey = 0;
public JButton bFront, bBack, bLeft, bRight;
StyledDocument instructionsText;
public JTextPane instructions;
public GUI() {
setLayout(new GridBagLayout());
this.setPreferredSize(new Dimension(400,400));
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 4;
StyledDocument instructionsText = new DefaultStyledDocument();
Style defaultStyle = instructionsText.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setAlignment(defaultStyle, StyleConstants.ALIGN_CENTER);
try {
instructionsText.insertString(0, "Control the Robot using the keyboard.\n Change the key bindings by clicking the buttons below.", null);
} catch (BadLocationException e) {
e.printStackTrace();
}
instructions = new JTextPane(instructionsText);
instructions.setFocusable(false);
add(instructions,c);
bFront = new JButton("Forward key: " + KeyEvent.getKeyText(frontKey));
bFront.setActionCommand("forward");
bFront.addActionListener(this);
bFront.setFocusable(false);
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 1;
add(bFront,c);
bBack = new JButton("Backward key: " + KeyEvent.getKeyText(backKey));
bBack.setActionCommand("backward");
bBack.addActionListener(this);
bBack.setFocusable(false);
c.gridx = 1;
c.gridy = 4;
add(bBack,c);
bLeft = new JButton("Left key: " + KeyEvent.getKeyText(leftKey));
bLeft.setActionCommand("left");
bLeft.addActionListener(this);
bLeft.setFocusable(false);
c.gridx = 0;
c.gridy = 3;
add(bLeft,c);
bRight = new JButton("Right key: "+ KeyEvent.getKeyText(rightKey));
bRight.setActionCommand("right");
bRight.addActionListener(this);
bRight.setFocusable(false);
c.gridx = 3;
c.gridy = 3;
add(bRight,c);
setFocusable(true);
addKeyListener(this);
}
public static void main(String arg[]) {
// TODO Auto-generated method stub
//Create and show the GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Telepresence Robot Controller");
frame.setLocationRelativeTo(null);
//Tell the program to exit when the GUI is closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final GUI gui = new GUI(); //create the GUI
gui.setOpaque(true); //Set it to visible
frame.setContentPane(gui); //attach it to the Jframe
//Display the window.
frame.pack();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("forward")) {
listenKey = 1;
} else if(e.getActionCommand().equals("backward")) {
listenKey = 2;
} else if(e.getActionCommand().equals("left")) {
listenKey = 3;
} else if(e.getActionCommand().equals("right")) {
listenKey = 4;
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(listenKey == 0)
if(e.getKeyCode() == frontKey) {
bFront.setBackground(Color.yellow);
frontKeyPressed = true;
} else if(e.getKeyCode() == backKey) {
bBack.setBackground(Color.yellow);
backKeyPressed = true;
} else if(e.getKeyCode() == leftKey) {
bLeft.setBackground(Color.yellow);
leftKeyPressed = true;
} else if(e.getKeyCode() == rightKey) {
bRight.setBackground(Color.yellow);
rightKeyPressed = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
switch(listenKey) {
case 1:
frontKey = e.getKeyCode();
bFront.setText("Forward key: " + KeyEvent.getKeyText(frontKey));
listenKey = 0;
break;
case 2:
backKey = e.getKeyCode();
bBack.setText("Backward key: " + KeyEvent.getKeyText(backKey));
listenKey = 0;
break;
case 3:
leftKey = e.getKeyCode();
bLeft.setText("Left key: " + KeyEvent.getKeyText(leftKey));
listenKey = 0;
break;
case 4:
rightKey = e.getKeyCode();
bRight.setText("Right key: " + KeyEvent.getKeyText(rightKey));
listenKey = 0;
break;
default:
if(e.getKeyCode() == frontKey) {
bFront.setBackground(Color.yellow);
frontKeyPressed = false;
} else if(e.getKeyCode() == backKey) {
bBack.setBackground(null);
backKeyPressed = false;
} else if(e.getKeyCode() == leftKey) {
bLeft.setBackground(null);
leftKeyPressed = false;
} else if(e.getKeyCode() == rightKey) {
bRight.setBackground(null);
rightKeyPressed = false;
}
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}