Глобальные переменные keylistener - Java
Формулировка задачи:
Здравствуйте! нужна программа которая при нажатии клавиши меняет значение параметра drawstring. Объявил глобальную переменную DataClass.Path. Прога скомпилировалась, а значение параметра при нажатии клавиш "d" и "a" не меняется. Что делать? код ниже
Листинг программы
- package my3000;
- import java.awt.*;
- import javax.swing.*;
- import java.io.*;
- import javax.imageio.*;
- import java.awt.event.*;
- /**
- *
- * @author maxim
- */
- public class My3000 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- DataClass.path="aaaaa";
- System.out.println("dcwdc");
- MyFrame frame = new MyFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.show();
- frame.addKeyListener(new KeyListener() {
- @SuppressWarnings("empty-statement")
- public void keyTyped(KeyEvent ke) // обработка нажатий клавиш
- {
- char c;
- c = ke.getKeyChar();
- switch (c){
- case 'a' : DataClass.path="bbbbb";
- break;
- case 'd' : DataClass.path="ccccc";
- break;
- }
- }
- public void keyPressed(KeyEvent ke) {}
- public void keyReleased(KeyEvent ke) {}
- });
- // TODO code application logic here
- }
- }
- class MyFrame extends JFrame
- {
- public MyFrame()
- {
- setTitle("MainFraim");
- setSize(300,200);
- MyPanel panel = new MyPanel();
- Container pane=getContentPane();
- pane.add(panel);
- }
- }
- class MyPanel extends JPanel
- {
- private Image im;
- public MyPanel()
- {
- try
- {
- im=ImageIO.read(new File(DataClass.path));
- }
- catch (IOException exception)
- {}
- }
- public void paintComponent (Graphics g)
- {
- super.paintComponent(g);
- // g.drawImage(im,22,22,null);
- g.drawString(DataClass.path, 12, 12);
- }
- }
Решение задачи: «Глобальные переменные keylistener»
textual
Листинг программы
- public class My3000 {
- /**
- * @param args
- * the command line arguments
- */
- public static void main(String[] args) {
- DataClass.path = "aaaaa";
- System.out.println("dcwdc");
- MyFrame frame = new MyFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- frame.addKeyListener(new KeyListener() {
- @SuppressWarnings("empty-statement")
- public void keyTyped(KeyEvent ke) // обработка нажатий клавиш
- {
- char c;
- c = ke.getKeyChar();
- System.out.println("Get key char: " + c);
- switch (c) {
- case 'a':
- DataClass.path = "bbbbb";
- break;
- case 'd':
- DataClass.path = "ccccc";
- break;
- }
- }
- public void keyPressed(KeyEvent ke) {
- }
- public void keyReleased(KeyEvent ke) {
- }
- });
- // TODO code application logic here
- }
- }
- class MyFrame extends JFrame {
- public MyFrame() {
- setTitle("MainFraim");
- setSize(300, 200);
- MyPanel panel = new MyPanel();
- Container pane = getContentPane();
- pane.add(panel);
- }
- }
- class DataClass {
- public static String path;
- }
- class MyPanel extends JPanel {
- private Image im;
- public MyPanel() {
- try {
- im = ImageIO.read(new File(DataClass.path));
- } catch (IOException exception) {
- }
- }
- public void paintComponent (Graphics g) {
- super.paintComponent(g);
- // g.drawImage(im,22,22,null);
- g.drawString(DataClass.path, 12, 12);
- setSize(100,50);
- repaint();
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д