Reputation: 11
I have some issues with my Java Swing code.
I want to move between buttons using the keyboard (UP, DOWN key) and press the button using the ENTER key. But I think there is no way to use the keyboard.
Can anyone teach me how to move buttons with the keyboard UP and DOWN keys?
I also have used JRadioButton
, but it was difficult...
The below code is my code!
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class StartScreen extends JFrame {
JButton[] buttons;
private KeyListener playerKeyListener;
public StartScreen() {
setTitle("테트리스 시작 화면");
setSize(400, 500);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setBackground(Color.PINK);
JPanel jPanel = new JPanel();
jPanel.setBackground(Color.PINK);
jPanel.setBounds(0,0,400,500);
jPanel.setLayout(null);
String[] btnText = {"일반 모드 게임 시작", "아이템 모드 게임 시작", "게임 설정", "스코어 보드", "게임 종료"};
buttons = new JButton[5];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(btnText[i]);
buttons[i].setBackground(new Color(0,0,0,0));
buttons[i].setVisible(true);
buttons[i].setBorderPainted(true);
jPanel.add(buttons[i]);
}
int y = 150;
for (int i = 0; i < buttons.length; i++) {
buttons[i].setBounds(125, y, 150, 50);
y += 60;
}
JLabel jLabel = new JLabel("Tetris");
Font font = new Font("Arial", Font.BOLD, 40);
jLabel.setFont(font);
jLabel.setLayout(null);
jLabel.setBounds(145,80,150,40);
jPanel.add(jLabel, BorderLayout.CENTER);
getContentPane().add(jPanel);
setVisible(true);
playerKeyListener = new PlayerKeyListener();
addKeyListener(playerKeyListener);
setFocusable(true);
requestFocus();
}
public class PlayerKeyListener implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_DOWN:
break;
case KeyEvent.VK_UP:
break;
default:
System.out.println("");
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
public static void main(String[] args) {
new StartScreen();
}
}
Upvotes: 0
Views: 348
Reputation: 324207
Following shows two different approaches:
Choose the approach that best meets your requirment.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class FocusTraversalKeys extends JPanel
{
public FocusTraversalKeys()
{
for (int i = 0; i < 5; i++)
{
JButton button = new JButton( String.valueOf(i) );
add( button );
// Add left arrow key as a focus traversal key.
// Applies only to this specific component.
Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>( button.getFocusTraversalKeys(
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS ) );
set.add( KeyStroke.getKeyStroke( "LEFT" ) );
button.setFocusTraversalKeys(
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set );
}
// Add right arrow key as a focus traversal key.
// Applies to all components on the panel
InputMap im = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
String rightText = "RIGHT";
im.put(KeyStroke.getKeyStroke(rightText), rightText);
getActionMap().put(rightText, new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
}
});
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("FocusTraversalKeys");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new FocusTraversalKeys() );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
Upvotes: 1