import javax.swing.*;
import com.borland.jbcl.layout.XYConstraints;
import com.borland.jbcl.layout.XYLayout;import java.awt.*;
import java.awt.event.*;public class MainFrame extends JFrame{
private Timer timer;
private JButton play=new JButton();
private JButton stop=new JButton();
private JLabel showtext=new JLabel();
private JTextArea textarea=new JTextArea();
private JToolBar status=new JToolBar();
private XYLayout xyLayout = new XYLayout(); //界面布局
private Panel jMainPanel = new Panel();
private CntrPanel cntrpanel=new CntrPanel();
public MainFrame() {
try {
setTitle("NewGo");
setLocation(200, 80);
// setSize(800, 600);
jbInit();
getContentPane().add(jMainPanel, "North");
getContentPane().addKeyListener(new KeyAdapter() { //主窗口添加键盘监听器
public void keyPressed(KeyEvent e) {
ProcessKeyEvent(e);
}
}); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
}); }
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
// jMainPanel.setBackground(new Color(72, 112, 112));
jMainPanel.setForeground(UIManager.getColor(
"TextArea.selectionBackground"));
jMainPanel.setLayout(xyLayout);
play.setBackground(Color.pink);
play.setFont(new java.awt.Font("Dialog", 0, 14));
play.setForeground(SystemColor.desktop);
play.setText("Play");
play.addActionListener(new PlayButton_actionAdapter(this));
stop.setBackground(Color.pink);
stop.setFont(new java.awt.Font("Dialog", 0, 14));
stop.setForeground(SystemColor.desktop);
stop.setText("Stop");
stop.addActionListener(new StopButton_actionAdapter(this));
showtext=new JLabel("Welcome");
status.add(showtext);
xyLayout.setWidth(600);
xyLayout.setHeight(600);
jMainPanel.add(play,new XYConstraints(50, 10, 150, 30));
jMainPanel.add(stop,new XYConstraints(350, 10, 150, 30));
jMainPanel.add(cntrpanel,new XYConstraints(20,50,560,500));
jMainPanel.add(status,new XYConstraints(0, 570, 600, 30));
addKeyListener(new KeyAdapter() { //主窗口添加键盘监听器
public void keyPressed(KeyEvent e) {
ProcessKeyEvent(e);
}
});
}
}
public void ProcessKeyEvent(KeyEvent e) {
showtext.setText("key pressed");
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
showtext.setText("down");
break;
case KeyEvent.VK_UP:
showtext.setText("up");
break;
default:
break;
}
}
public static void main(String[] args) {
MainFrame objMainFrame = new MainFrame();
objMainFrame.pack();
objMainFrame.setVisible(true);
objMainFrame.validate();
objMainFrame.setResizable(false);
}
}执行后,ProcessKeyEvent()根本就不执行,请高手指点,谢谢!
import com.borland.jbcl.layout.XYConstraints;
import com.borland.jbcl.layout.XYLayout;import java.awt.*;
import java.awt.event.*;public class MainFrame extends JFrame{
private Timer timer;
private JButton play=new JButton();
private JButton stop=new JButton();
private JLabel showtext=new JLabel();
private JTextArea textarea=new JTextArea();
private JToolBar status=new JToolBar();
private XYLayout xyLayout = new XYLayout(); //界面布局
private Panel jMainPanel = new Panel();
private CntrPanel cntrpanel=new CntrPanel();
public MainFrame() {
try {
setTitle("NewGo");
setLocation(200, 80);
// setSize(800, 600);
jbInit();
getContentPane().add(jMainPanel, "North");
getContentPane().addKeyListener(new KeyAdapter() { //主窗口添加键盘监听器
public void keyPressed(KeyEvent e) {
ProcessKeyEvent(e);
}
}); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
}); }
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
// jMainPanel.setBackground(new Color(72, 112, 112));
jMainPanel.setForeground(UIManager.getColor(
"TextArea.selectionBackground"));
jMainPanel.setLayout(xyLayout);
play.setBackground(Color.pink);
play.setFont(new java.awt.Font("Dialog", 0, 14));
play.setForeground(SystemColor.desktop);
play.setText("Play");
play.addActionListener(new PlayButton_actionAdapter(this));
stop.setBackground(Color.pink);
stop.setFont(new java.awt.Font("Dialog", 0, 14));
stop.setForeground(SystemColor.desktop);
stop.setText("Stop");
stop.addActionListener(new StopButton_actionAdapter(this));
showtext=new JLabel("Welcome");
status.add(showtext);
xyLayout.setWidth(600);
xyLayout.setHeight(600);
jMainPanel.add(play,new XYConstraints(50, 10, 150, 30));
jMainPanel.add(stop,new XYConstraints(350, 10, 150, 30));
jMainPanel.add(cntrpanel,new XYConstraints(20,50,560,500));
jMainPanel.add(status,new XYConstraints(0, 570, 600, 30));
addKeyListener(new KeyAdapter() { //主窗口添加键盘监听器
public void keyPressed(KeyEvent e) {
ProcessKeyEvent(e);
}
});
}
}
public void ProcessKeyEvent(KeyEvent e) {
showtext.setText("key pressed");
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
showtext.setText("down");
break;
case KeyEvent.VK_UP:
showtext.setText("up");
break;
default:
break;
}
}
public static void main(String[] args) {
MainFrame objMainFrame = new MainFrame();
objMainFrame.pack();
objMainFrame.setVisible(true);
objMainFrame.validate();
objMainFrame.setResizable(false);
}
}执行后,ProcessKeyEvent()根本就不执行,请高手指点,谢谢!
导致你的KEYEVENT永远也不到达getContentPane()?
Toolkit toolkit = Toolkit.getDefaultToolkit();
toolkit.addAWTEventListener(new testListener(), AWTEvent.KEY_EVENT_MASK);
监听:
class testListener implements AWTEventListener {
public void eventDispatched(AWTEvent event) {
if(event){Do}
}
}不过这个在整个frame失去焦点后也会失效。