現在javaでキーを感知するプログラムを作成しているのですが、キーを押しても反応しません。
KeyListenerは単独で設置することはできないのでしょうか。
その場合、プログラムを実行して、キー入力を常時受け付けるにはどのようにしたらよいでしょうか。
import java.io.File;
import java.io.IOException;
import javax.swing.;
import java.awt.;
import java.awt.event.;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import java.applet.;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class mendo3 extends JFrame implements ActionListener,KeyListener{
static mendo3 frame = new mendo3("* 。violin mania。 *"); static JFrame frames = new JFrame("* 。 music select。 *");//曲選択
public static void main(String args[]){
frame.setVisible(true);
}
mendo3(String title){ setTitle(title); setBounds(600,10,600,1000); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); p.setLayout(null); ImageIcon top_bg = new ImageIcon("top.png"); JLabel bg1 = new JLabel(top_bg); bg1.setBounds(0,0,600,1000); ImageIcon icon2 = new ImageIcon("option.png"); JButton fileButton = new JButton(icon2); fileButton.setBounds(370,750,200,50); fileButton.addActionListener(this); ImageIcon icon1 = new ImageIcon("start.png"); JButton button = new JButton(icon1); button.setBounds(370,600,200,50); button.addActionListener(this); button.setActionCommand("start"); p.add(bg1); p.add(button); p.add(fileButton); Container contentPane = getContentPane(); contentPane.add(p, BorderLayout.CENTER); } public void init(){ addKeyListener(this); } public void actionPerformed(ActionEvent e){ String cmd = e.getActionCommand(); if (cmd.equals("start")){ AudioClip audio; audio = Applet.newAudioClip(getClass().getResource("pinpon.wav")); audio.play(); } } public void keyPressed(KeyEvent e){ } public void keyReleased(KeyEvent e){ } public void keyTyped(KeyEvent e){ char key = e.getKeyChar(); if(key == 'a'){ AudioClip audio; audio = Applet.newAudioClip(getClass().getResource("pinpon.wav")); audio.play(); } }
}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/30 06:13
2015/12/30 07:45
2016/01/03 07:21 編集