
Создаю на JFrame панель JPanel , В неё помещаю InternalFrame и toolbar.
Вешаю на панель KeyListenera. Но кнопки работаю колько если не нажимать на toolbar и InternalFrame.
То есть спадает фокус. Необходимо что бы отлавливал нажатие кнопок постоянно. Тут что то с фокусом делать надо, но я не пойму что.
package com.company;
import javax.swing.;
import java.awt.;
import java.awt.event.*;
public class Main extends JFrame {
private static class MyPanel extends JPanel{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
repaint();
}
}
public static void main(String[] args) {
Main MyFrame = new Main();
MyFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
MyFrame.setLocation(0, 0);
MyFrame.setSize(500, 400);
MyFrame.setResizable(true);
MyFrame.setLayout(new GridLayout(1, 0));
MyFrame.setLocationRelativeTo(null);
MyPanel game_field = new MyPanel();
game_field.setBackground(Color.WHITE);
game_field.setVisible(true);
game_field.setLocation(0, 0);
JButton button100 = new JButton("Компановка");
JButton button101 = new JButton("Пуск");
JButton button102 = new JButton("Обновить всё");
JButton button103 = new JButton("Стереть линии");
JButton button1 = new JButton();
game_field.setLayout(new BorderLayout());
JLabel TextLabel = new JLabel();
TextLabel.setText("Соберите необходимую схему");
JInternalFrame internalFrame = new JInternalFrame("эементы", false, false, false, false);
game_field.add(internalFrame, BorderLayout.WEST);
internalFrame.setPreferredSize(new Dimension(240, 100));
internalFrame.setVisible(true);
JToolBar toolBar = new JToolBar("Инструментальная панель");
toolBar.setBounds(0, 0, 600, 30);
// toolBar.setLayout(new BorderLayout());
toolBar.add(button100);
toolBar.add(button101);
toolBar.add(button102);
toolBar.add(button103);
toolBar.addSeparator();
toolBar.add(TextLabel);
game_field.add(toolBar, BorderLayout.PAGE_START);
MyFrame.setFocusable(true);
MyFrame.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
super.keyTyped(e);
}
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
MyFrame.setTitle("Чар"+e.getKeyChar());
}
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
}
});
MyFrame.add(game_field);
MyFrame.setVisible(true);
}
}







