我有这个开头,这是GUI运行时出现的第一帧。
private void populateFrame(JFrame frame){
mainPanel = new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.setFont(new Font(Font.SERIF, Font.BOLD,50));
g.drawString("Press the spacebar to play", 690, 1000);
}
};
frame.getContentPane().add(mainPanel);
mainPanel.setBackground(Color.white);
mainPanel.setLayout(new BorderLayout());
frame.setTitle("Marvel Showdown!");
frame.setSize(1920,1080);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
picture.setIcon(new ImageIcon("C:\\Users\\Quentin Clayton\\Pictures\\Marvel Showdown\\Marvel Showdown Menu.png"));
mainPanel.add(picture, BorderLayout.CENTER);
mainPanel.repaint();
mainPanel.addKeyListener(this);
mainPanel.setFocusable(true);
mainPanel.requestFocus();
frame.setVisible(true);
然后就是我要切换到的面板,这是10个按钮供人们选择。
private void mainPanel(/*JPanel mainPanel*/){
frame.getContentPane().removeAll();
frame.getContentPane().add(Panel2);
Panel2.setBackground(Color.WHITE);
Panel2.setLayout(new BorderLayout());
frame.setTitle("Picture Buttons");
frame.setSize(1920, 1080);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Icon pic1 = new ImageIcon("C:\\Users\\Quentin Clayton\\Pictures\\Marvel Showdown\\Deadpool.png");
Icon pic2 = new ImageIcon("C:\\Users\\Quentin Clayton\\Pictures\\Marvel Showdown\\Captain America.png");
Icon pic3 = new ImageIcon("C:\\Users\\Quentin Clayton\\Pictures\\Marvel Showdown\\Domino.png");
Icon pic4 = new ImageIcon("C:\\Users\\Quentin Clayton\\Pictures\\Marvel Showdown\\IronMan.png");
Icon pic5 = new ImageIcon("C:\\Users\\Quentin Clayton\\Pictures\\Marvel Showdown\\magneto.png");
Icon pic6 = new ImageIcon("C:\\Users\\Quentin Clayton\\Pictures\\Marvel Showdown\\Spiderman.png");
Icon pic7 = new ImageIcon("C:\\Users\\Quentin Clayton\\Pictures\\Marvel Showdown\\Venom.png");
Icon pic8 = new ImageIcon("C:\\Users\\Quentin Clayton\\Pictures\\Marvel Showdown\\Wolverine.png");
Icon pic9 = new ImageIcon("C:\\Users\\Quentin Clayton\\Pictures\\Marvel Showdown\\Gambit.png");
Icon pic10 = new ImageIcon("C:\\Users\\Quentin Clayton\\Pictures\\Marvel Showdown\\Josh.png");
JButton button1 = new JButton(pic1);
JButton button2 = new JButton(pic2);
JButton button3 = new JButton(pic3);
JButton button4 = new JButton(pic4);
JButton button5 = new JButton(pic5);
JButton button6 = new JButton(pic6);
JButton button7 = new JButton(pic7);
JButton button8 = new JButton(pic8);
JButton button9 = new JButton(pic9);
JButton button10 = new JButton(pic10);
mainPanel.add(button1);
mainPanel.add(button2);
mainPanel.add(button3);
mainPanel.add(button4);
mainPanel.add(button5);
mainPanel.add(button6);
mainPanel.add(button7);
mainPanel.add(button8);
mainPanel.add(button9);
mainPanel.add(button10);
mainPanel.setLayout(new GridLayout(2,4));
//frame.setSize(1920, 100);
frame.setVisible(true);
}
最后这就是我将空格键作为击中键的地方。
@Override
public void keyPressed(KeyEvent e) {
System.out.println("press space bar");
//if (showTitleScreen) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
通过按空格键可以切换到面板的任何提示。
答案 0 :(得分:1)
有关使用所有按钮
切换到面板的任何提示
您应该使用CardLayout
来容纳两个面板。然后按空格键可以切换面板。阅读How to Use CardLayout上的Swing教程中的部分。你应该可以使用" next()" CardLayout交换到下一个面板的方法。
最后这就是我将空格键作为击中键的地方。
不要使用KeyListener。 Swing旨在与Key Bindings
一起使用。
您需要创建Action
。阅读How to Use Actions上Swing教程中的部分,了解更多信息和示例。
然后,您将使用键绑定将Action
绑定到空格键KeyStroke
。查看Key Bindings以获取有关如何将KeyStroke绑定到Action的示例。
在这种情况下,您可能希望将KeyStroke绑定到框架的根窗格。