我如何修复下面给出的 Class KeyBoard 中的 Java Typing Tutor 程序 KeyBoard 按钮背景颜色?
How can i fix the Java Typing Tutor program's KeyBoard button's background Color in Class KeyBoard given below?
键盘class:
package keyBoardTypingTutor;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class KeyBoard {
private final JFrame f = new JFrame("Keyboard");
private final JPanel keyboard = new JPanel();
private Color buttonBackground;
private static JButton[][] b = new JButton[5][40];
private static final String[][] key = {
{"~","1","2","3","4","5","6","7","8","9","0","-","=","Backspace"},
{"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","\"},
{"Caps","A","S","D","F","G","H","J","K","L",";","'","Enter"},
{"Shift","Z","X","C","V","B","N","M",",",".","/","\u2191"},
{" ","\u2190","\u2193","\u2192"}
};
public KeyBoard() {
// TODO Auto-generated constructor stub
keyboard.setLayout(new GridBagLayout());
Insets zeroInsets = new Insets(0, 0, 0, 0);
Font monospaced = new Font("Monospaced",Font.PLAIN, 12);
JPanel pRow;
GridBagConstraints cRow = new GridBagConstraints();
GridBagConstraints cButton = new GridBagConstraints();
cRow.anchor = GridBagConstraints.WEST;
cButton.ipady = 21;
for(int row=0; row<key.length; row++){
pRow = new JPanel(new GridBagLayout());
cRow.gridy = row;
for(int col=0; col<key[row].length; col++){
switch(key[row][col]){
case "Backspace":
cButton.ipadx = 0;
break;
case "Tab":
cButton.ipadx = 17;
break;
case "Caps":
cButton.ipadx = 14;
break;
case "Enter":
cButton.ipadx = 27;
break;
case "Shift":
cButton.ipadx = 27;
break;
case "/":
cButton.insets = new Insets(0, 0, 0, 29);
break;
case " ":
cButton.ipadx = 247;
cButton.insets = new Insets(0, 192, 0, 73);
break;
default:
cButton.ipadx = 7;
cButton.insets = zeroInsets;
break;
}
b[row][col] = new JButton(key[row][col]);
b[row][col].setFont(monospaced);
b[row][col].setFocusable(false);
pRow.add(b[row][col], cButton);
// b[row][col].setForeground(Color.WHITE);
// b[row][col].setBackground(Color.DARK_GRAY);
buttonBackground = b[row][col].getBackground();
}
keyboard.add(pRow, cRow);
}
f.add(keyboard);
}
public void pressedButton(int row, int col){
b[row][col].setBackground(Color.BLUE);
b[row][col].setForeground(Color.WHITE);
System.out.print("here");
}
public void releasedButton(int row, int col){
b[row][col].setBackground(buttonBackground);
b[row][col].setForeground(Color.BLACK);
}
public void launch(){
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.pack();
f.setResizable(false);
f.setLocationRelativeTo(null);
}
public JPanel getKeyboard() {
return keyboard;
}
public JButton[][] getB() {
return b;
}
}
TextAreaFrame class-->>
package keyBoardTypingTutor;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class TextAreaFrame {
private JTextArea textArea;
private JPanel areaPanel;
private JFrame frame;
private KeyBoard keyBoard;
private JButton[][] b;
public TextAreaFrame() {
// TODO Auto-generated constructor stub
areaPanel = new JPanel();
keyBoard = new KeyBoard();
textArea = new JTextArea(7, 50);
textArea.setLineWrap(true);
textArea.setFont(new Font("SansSerif", Font.PLAIN, 18));
areaPanel.add(textArea);
KeyHandler handler = new KeyHandler();
textArea.addKeyListener(handler);
}
private class KeyHandler implements KeyListener {
public void keyPressed(KeyEvent event) {
// System.out.print(event.getKeyCode());
switch (event.getKeyCode()) {
case KeyEvent.VK_0:
System.out.print(event.getKeyCode());
keyBoard.pressedButton(0, 10);
break;
case KeyEvent.VK_1:
keyBoard.pressedButton(0, 1);
break;
case KeyEvent.VK_2:
keyBoard.pressedButton(0, 2);
break;
case KeyEvent.VK_3:
keyBoard.pressedButton(0, 3);
break;
case KeyEvent.VK_4:
keyBoard.pressedButton(0, 4);
break;
case KeyEvent.VK_5:
keyBoard.pressedButton(0, 5);
break;
case KeyEvent.VK_6:
keyBoard.pressedButton(0, 6);
break;
case KeyEvent.VK_7:
keyBoard.pressedButton(0, 7);
break;
case KeyEvent.VK_8:
keyBoard.pressedButton(0, 8);
break;
case KeyEvent.VK_9:
keyBoard.pressedButton(0, 9);
break;
}
}
public void keyReleased(KeyEvent event) {
// System.out.print(event.getKeyCode());
switch (event.getKeyCode()) {
case KeyEvent.VK_0:
keyBoard.releasedButton(0, 10);
break;
case KeyEvent.VK_1:
keyBoard.releasedButton(0, 1);
break;
case KeyEvent.VK_2:
keyBoard.releasedButton(0, 2);
break;
case KeyEvent.VK_3:
keyBoard.releasedButton(0, 3);
break;
case KeyEvent.VK_4:
keyBoard.releasedButton(0, 4);
break;
case KeyEvent.VK_5:
keyBoard.releasedButton(0, 5);
break;
case KeyEvent.VK_6:
keyBoard.releasedButton(0, 6);
break;
case KeyEvent.VK_7:
keyBoard.releasedButton(0, 7);
break;
case KeyEvent.VK_8:
keyBoard.releasedButton(0, 8);
break;
case KeyEvent.VK_9:
keyBoard.releasedButton(0, 9);
break;
}
}
public void keyTyped(KeyEvent event) {
// System.out.print(event.getKeyCode());
}
}
public JPanel getAreaPanel() {
return areaPanel;
}
// public void launch(){
// frame = new JFrame("Area");
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setVisible(true);
// frame.setSize(800, 300);
// frame.add(areaPanel);
// }
// public static void main(String[] args){
// TextAreaFrame f = new TextAreaFrame();
// f.launch();
// }
}
LabelFrame class-->>
package keyBoardTypingTutor;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class LabelFrame {
private JLabel label;
private JPanel panel;
public LabelFrame() {
// TODO Auto-generated constructor stub
String text = "A quick Brown Fox Jumps over the Lazy Dog. Programmers are mentalzz.";
panel = new JPanel();
label = new JLabel(text);
label.setFont(new Font("SansSerif", Font.PLAIN, 18));
label.setHorizontalTextPosition(SwingConstants.LEFT);
panel.add(label);
}
public JPanel getLabelPanel() {
return panel;
}
public JLabel getLabel() {
return label;
}
}
键盘测试class-->>
package keyBoardTypingTutor;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class KeyBoardTest extends JFrame {
private KeyBoard keyBoard;
private TextAreaFrame textAreaFrame;
private LabelFrame labelFrame;
public KeyBoardTest() {
// TODO Auto-generated constructor stub
keyBoard = new KeyBoard();
textAreaFrame = new TextAreaFrame();
labelFrame = new LabelFrame();
add(labelFrame.getLabelPanel(), BorderLayout.PAGE_START);
add(textAreaFrame.getAreaPanel(), BorderLayout.CENTER);
add(keyBoard.getKeyboard(), BorderLayout.PAGE_END);
// keyBoard.getKeyboard().setBackground(Color.GRAY);
// labelFrame.getLabelPanel().setBackground(Color.GRAY);
// labelFrame.getLabel().setForeground(Color.WHITE);
// textAreaFrame.getAreaPanel().setBackground(Color.GRAY);
}
public static void main(String[] args) {
KeyBoardTest test = new KeyBoardTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
test.pack();
test.setResizable(false);
test.setLocationRelativeTo(null);
// try {
// UIManager
// .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
// SwingUtilities.updateComponentTreeUI(test);
// } catch (Exception e) {
// // TODO: handle exception
// e.printStackTrace();
// }
}
}
在这个程序中,当我按下物理键盘上的一个按钮时,TextAreaFrame
class 的 KeyEventListener
的方法 keyPressed()
和 keyReleased()
调用 KeyBoard
class 的 pressedButton()
和 releasedButton()
方法,我想在其中更改当前按下的按钮的背景颜色。但是在这个程序中颜色没有改变。我浪费了很多时间来解决这个问题但无法解决问题。
您正在创建 Keyboard
的两个实例,一个在 KeyBoardTest
中显示在屏幕上,另一个在 TextAreaFrame
中没有显示。
public class KeyBoardTest extends JFrame {
private KeyBoard keyBoard;
private TextAreaFrame textAreaFrame;
private LabelFrame labelFrame;
public KeyBoardTest() {
// TODO Auto-generated constructor stub
// This is the keyboard that's on the screen
keyBoard = new KeyBoard();
textAreaFrame = new TextAreaFrame();
labelFrame = new LabelFrame();
add(labelFrame.getLabelPanel(), BorderLayout.PAGE_START);
add(textAreaFrame.getAreaPanel(), BorderLayout.CENTER);
add(keyBoard.getKeyboard(), BorderLayout.PAGE_END);
// keyBoard.getKeyboard().setBackground(Color.GRAY);
// labelFrame.getLabelPanel().setBackground(Color.GRAY);
// labelFrame.getLabel().setForeground(Color.WHITE);
// textAreaFrame.getAreaPanel().setBackground(Color.GRAY);
}
//...
public class TextAreaFrame {
private JTextArea textArea;
private JPanel areaPanel;
private JFrame frame;
private KeyBoard keyBoard;
private JButton[][] b;
public TextAreaFrame() {
// TODO Auto-generated constructor stub
areaPanel = new JPanel();
// This one is not, but is the instance you are trying to
// change...
keyBoard = new KeyBoard();
相反,传递屏幕上 TextAreaFrame
的 KeyBoard
实例
public class KeyBoardTest extends JFrame {
private KeyBoard keyBoard;
private TextAreaFrame textAreaFrame;
private LabelFrame labelFrame;
public KeyBoardTest() {
// TODO Auto-generated constructor stub
keyBoard = new KeyBoard();
textAreaFrame = new TextAreaFrame(keyBoard);
labelFrame = new LabelFrame();
//...
public class TextAreaFrame {
private JTextArea textArea;
private JPanel areaPanel;
private JFrame frame;
private KeyBoard keyBoard;
private JButton[][] b;
public TextAreaFrame(KeyBoard keyBoard) {
// TODO Auto-generated constructor stub
areaPanel = new JPanel();
this.keyBoard = keyBoard;
就我个人而言,我认为 KeyListener
你正在把自己逼入一个深坑。我个人的方法可能是为您的键盘支持的每个击键创建一个键绑定,然后将该值简单地注入 JTextArea
并通知 KeyBoard
,但这就是我
键盘class:
package keyBoardTypingTutor;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class KeyBoard {
private final JFrame f = new JFrame("Keyboard");
private final JPanel keyboard = new JPanel();
private Color buttonBackground;
private static JButton[][] b = new JButton[5][40];
private static final String[][] key = {
{"~","1","2","3","4","5","6","7","8","9","0","-","=","Backspace"},
{"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","\"},
{"Caps","A","S","D","F","G","H","J","K","L",";","'","Enter"},
{"Shift","Z","X","C","V","B","N","M",",",".","/","\u2191"},
{" ","\u2190","\u2193","\u2192"}
};
public KeyBoard() {
// TODO Auto-generated constructor stub
keyboard.setLayout(new GridBagLayout());
Insets zeroInsets = new Insets(0, 0, 0, 0);
Font monospaced = new Font("Monospaced",Font.PLAIN, 12);
JPanel pRow;
GridBagConstraints cRow = new GridBagConstraints();
GridBagConstraints cButton = new GridBagConstraints();
cRow.anchor = GridBagConstraints.WEST;
cButton.ipady = 21;
for(int row=0; row<key.length; row++){
pRow = new JPanel(new GridBagLayout());
cRow.gridy = row;
for(int col=0; col<key[row].length; col++){
switch(key[row][col]){
case "Backspace":
cButton.ipadx = 0;
break;
case "Tab":
cButton.ipadx = 17;
break;
case "Caps":
cButton.ipadx = 14;
break;
case "Enter":
cButton.ipadx = 27;
break;
case "Shift":
cButton.ipadx = 27;
break;
case "/":
cButton.insets = new Insets(0, 0, 0, 29);
break;
case " ":
cButton.ipadx = 247;
cButton.insets = new Insets(0, 192, 0, 73);
break;
default:
cButton.ipadx = 7;
cButton.insets = zeroInsets;
break;
}
b[row][col] = new JButton(key[row][col]);
b[row][col].setFont(monospaced);
b[row][col].setFocusable(false);
pRow.add(b[row][col], cButton);
// b[row][col].setForeground(Color.WHITE);
// b[row][col].setBackground(Color.DARK_GRAY);
buttonBackground = b[row][col].getBackground();
}
keyboard.add(pRow, cRow);
}
f.add(keyboard);
}
public void pressedButton(int row, int col){
b[row][col].setBackground(Color.BLUE);
b[row][col].setForeground(Color.WHITE);
System.out.print("here");
}
public void releasedButton(int row, int col){
b[row][col].setBackground(buttonBackground);
b[row][col].setForeground(Color.BLACK);
}
public void launch(){
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.pack();
f.setResizable(false);
f.setLocationRelativeTo(null);
}
public JPanel getKeyboard() {
return keyboard;
}
public JButton[][] getB() {
return b;
}
}
TextAreaFrame class-->>
package keyBoardTypingTutor;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class TextAreaFrame {
private JTextArea textArea;
private JPanel areaPanel;
private JFrame frame;
private KeyBoard keyBoard;
private JButton[][] b;
public TextAreaFrame() {
// TODO Auto-generated constructor stub
areaPanel = new JPanel();
keyBoard = new KeyBoard();
textArea = new JTextArea(7, 50);
textArea.setLineWrap(true);
textArea.setFont(new Font("SansSerif", Font.PLAIN, 18));
areaPanel.add(textArea);
KeyHandler handler = new KeyHandler();
textArea.addKeyListener(handler);
}
private class KeyHandler implements KeyListener {
public void keyPressed(KeyEvent event) {
// System.out.print(event.getKeyCode());
switch (event.getKeyCode()) {
case KeyEvent.VK_0:
System.out.print(event.getKeyCode());
keyBoard.pressedButton(0, 10);
break;
case KeyEvent.VK_1:
keyBoard.pressedButton(0, 1);
break;
case KeyEvent.VK_2:
keyBoard.pressedButton(0, 2);
break;
case KeyEvent.VK_3:
keyBoard.pressedButton(0, 3);
break;
case KeyEvent.VK_4:
keyBoard.pressedButton(0, 4);
break;
case KeyEvent.VK_5:
keyBoard.pressedButton(0, 5);
break;
case KeyEvent.VK_6:
keyBoard.pressedButton(0, 6);
break;
case KeyEvent.VK_7:
keyBoard.pressedButton(0, 7);
break;
case KeyEvent.VK_8:
keyBoard.pressedButton(0, 8);
break;
case KeyEvent.VK_9:
keyBoard.pressedButton(0, 9);
break;
}
}
public void keyReleased(KeyEvent event) {
// System.out.print(event.getKeyCode());
switch (event.getKeyCode()) {
case KeyEvent.VK_0:
keyBoard.releasedButton(0, 10);
break;
case KeyEvent.VK_1:
keyBoard.releasedButton(0, 1);
break;
case KeyEvent.VK_2:
keyBoard.releasedButton(0, 2);
break;
case KeyEvent.VK_3:
keyBoard.releasedButton(0, 3);
break;
case KeyEvent.VK_4:
keyBoard.releasedButton(0, 4);
break;
case KeyEvent.VK_5:
keyBoard.releasedButton(0, 5);
break;
case KeyEvent.VK_6:
keyBoard.releasedButton(0, 6);
break;
case KeyEvent.VK_7:
keyBoard.releasedButton(0, 7);
break;
case KeyEvent.VK_8:
keyBoard.releasedButton(0, 8);
break;
case KeyEvent.VK_9:
keyBoard.releasedButton(0, 9);
break;
}
}
public void keyTyped(KeyEvent event) {
// System.out.print(event.getKeyCode());
}
}
public JPanel getAreaPanel() {
return areaPanel;
}
// public void launch(){
// frame = new JFrame("Area");
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setVisible(true);
// frame.setSize(800, 300);
// frame.add(areaPanel);
// }
// public static void main(String[] args){
// TextAreaFrame f = new TextAreaFrame();
// f.launch();
// }
}
LabelFrame class-->>
package keyBoardTypingTutor;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class LabelFrame {
private JLabel label;
private JPanel panel;
public LabelFrame() {
// TODO Auto-generated constructor stub
String text = "A quick Brown Fox Jumps over the Lazy Dog. Programmers are mentalzz.";
panel = new JPanel();
label = new JLabel(text);
label.setFont(new Font("SansSerif", Font.PLAIN, 18));
label.setHorizontalTextPosition(SwingConstants.LEFT);
panel.add(label);
}
public JPanel getLabelPanel() {
return panel;
}
public JLabel getLabel() {
return label;
}
}
键盘测试class-->>
package keyBoardTypingTutor;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class KeyBoardTest extends JFrame {
private KeyBoard keyBoard;
private TextAreaFrame textAreaFrame;
private LabelFrame labelFrame;
public KeyBoardTest() {
// TODO Auto-generated constructor stub
keyBoard = new KeyBoard();
textAreaFrame = new TextAreaFrame();
labelFrame = new LabelFrame();
add(labelFrame.getLabelPanel(), BorderLayout.PAGE_START);
add(textAreaFrame.getAreaPanel(), BorderLayout.CENTER);
add(keyBoard.getKeyboard(), BorderLayout.PAGE_END);
// keyBoard.getKeyboard().setBackground(Color.GRAY);
// labelFrame.getLabelPanel().setBackground(Color.GRAY);
// labelFrame.getLabel().setForeground(Color.WHITE);
// textAreaFrame.getAreaPanel().setBackground(Color.GRAY);
}
public static void main(String[] args) {
KeyBoardTest test = new KeyBoardTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
test.pack();
test.setResizable(false);
test.setLocationRelativeTo(null);
// try {
// UIManager
// .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
// SwingUtilities.updateComponentTreeUI(test);
// } catch (Exception e) {
// // TODO: handle exception
// e.printStackTrace();
// }
}
}
在这个程序中,当我按下物理键盘上的一个按钮时,TextAreaFrame
class 的 KeyEventListener
的方法 keyPressed()
和 keyReleased()
调用 KeyBoard
class 的 pressedButton()
和 releasedButton()
方法,我想在其中更改当前按下的按钮的背景颜色。但是在这个程序中颜色没有改变。我浪费了很多时间来解决这个问题但无法解决问题。
您正在创建 Keyboard
的两个实例,一个在 KeyBoardTest
中显示在屏幕上,另一个在 TextAreaFrame
中没有显示。
public class KeyBoardTest extends JFrame {
private KeyBoard keyBoard;
private TextAreaFrame textAreaFrame;
private LabelFrame labelFrame;
public KeyBoardTest() {
// TODO Auto-generated constructor stub
// This is the keyboard that's on the screen
keyBoard = new KeyBoard();
textAreaFrame = new TextAreaFrame();
labelFrame = new LabelFrame();
add(labelFrame.getLabelPanel(), BorderLayout.PAGE_START);
add(textAreaFrame.getAreaPanel(), BorderLayout.CENTER);
add(keyBoard.getKeyboard(), BorderLayout.PAGE_END);
// keyBoard.getKeyboard().setBackground(Color.GRAY);
// labelFrame.getLabelPanel().setBackground(Color.GRAY);
// labelFrame.getLabel().setForeground(Color.WHITE);
// textAreaFrame.getAreaPanel().setBackground(Color.GRAY);
}
//...
public class TextAreaFrame {
private JTextArea textArea;
private JPanel areaPanel;
private JFrame frame;
private KeyBoard keyBoard;
private JButton[][] b;
public TextAreaFrame() {
// TODO Auto-generated constructor stub
areaPanel = new JPanel();
// This one is not, but is the instance you are trying to
// change...
keyBoard = new KeyBoard();
相反,传递屏幕上 TextAreaFrame
KeyBoard
实例
public class KeyBoardTest extends JFrame {
private KeyBoard keyBoard;
private TextAreaFrame textAreaFrame;
private LabelFrame labelFrame;
public KeyBoardTest() {
// TODO Auto-generated constructor stub
keyBoard = new KeyBoard();
textAreaFrame = new TextAreaFrame(keyBoard);
labelFrame = new LabelFrame();
//...
public class TextAreaFrame {
private JTextArea textArea;
private JPanel areaPanel;
private JFrame frame;
private KeyBoard keyBoard;
private JButton[][] b;
public TextAreaFrame(KeyBoard keyBoard) {
// TODO Auto-generated constructor stub
areaPanel = new JPanel();
this.keyBoard = keyBoard;
就我个人而言,我认为 KeyListener
你正在把自己逼入一个深坑。我个人的方法可能是为您的键盘支持的每个击键创建一个键绑定,然后将该值简单地注入 JTextArea
并通知 KeyBoard
,但这就是我