📄 setupclass.java
字号:
package bin;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
class SetupUp extends JPanel
{
JLabel label1 = new JLabel("向上");
JButton button1 = new JButton("↑");
char up;
public SetupUp()
{
this.setSize(80,15);
this.setBackground(Color.BLACK);
this.setLayout(null);
label1.setBounds(0,0,60,15);
label1.setForeground(Color.WHITE);
button1.setBounds(50,0,80,15);
button1.addKeyListener(new KeyAdapter() //设置键盘监听
{
public void keyPressed(KeyEvent e)//监听键盘按下事件
{
up = e.getKeyChar(); //获得键盘按下所对应的字符键
button1.setText(up+" "); //把按键按下字符直接设置在button1上
button1.setFocusable(false);
button1.setForeground(Color.WHITE);
button1.setBackground(Color.BLACK);
}
});
button1.setBackground(Color.WHITE);
add(label1);
add(button1);
}
public void buttonDefault()//自定义了一个方法 目的: 当点击重置的时候调用这个方法
{
button1.setText("↑");
button1.setFocusable(true);
button1.setBackground(Color.WHITE);
button1.setForeground(Color.BLACK);
}
}
class SetDown extends JPanel
{
JLabel label2 = new JLabel("向下");
JButton button2 = new JButton("↓");
char down;
public SetDown()
{
this.setSize(80,15);
this.setBackground(Color.BLACK);
this.setLayout(null);
label2.setBounds(0,0,60,15);
label2.setForeground(Color.WHITE);
button2.setBounds(50,0,80,15);
button2.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
down = e.getKeyChar();//获得键盘按下字符赋给变量down
button2.setText(down+" ");
button2.setFocusable(true);//获得焦点
button2.setFocusable(false);//失去焦点
button2.setForeground(Color.WHITE);
button2.setBackground(Color.BLACK);
}
});
add(label2);
add(button2);
}
public void buttonDefault()
{
button2.setText("↓");
button2.setFocusable(true);
button2.setBackground(Color.WHITE);
button2.setForeground(Color.BLACK);
}
}
class SetLeft extends JPanel
{
JLabel label3 = new JLabel("向左");
JButton button3 = new JButton("←");
char left;
public SetLeft()
{
this.setSize(80,15);
this.setBackground(Color.BLACK);
this.setLayout(null);
label3.setBounds(0,0,60,15);
label3.setForeground(Color.WHITE);
button3.setBounds(50,0,80,15);
button3.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
left = e.getKeyChar();
button3.setText(left+" ");
button3.setFocusable(true);
button3.setFocusable(false);
button3.setBackground(Color.YELLOW);
button3.setForeground(Color.WHITE);
button3.setBackground(Color.BLACK);
}
});
add(label3);
add(button3);
}
public void buttonDefault()
{
button3.setText("←");
button3.setFocusable(true);
button3.setBackground(Color.WHITE);
button3.setForeground(Color.BLACK);
}
}
class SetRight extends JPanel
{
JLabel label4 = new JLabel("向右");
JButton button4 = new JButton("→");
char right;
public SetRight()
{
this.setSize(80,15);
this.setBackground(Color.BLACK);
this.setLayout(null);
label4.setBounds(0,0,60,15);
label4.setForeground(Color.WHITE);
button4.setBounds(50,0,80,15);
button4.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
right = e.getKeyChar();
button4.setText(right+" ");
button4.setFocusable(true);
button4.setFocusable(false);
button4.setForeground(Color.WHITE);
button4.setBackground(Color.BLACK);
}
});
add(label4);
add(button4);
}
public void buttonDefault()//创建了一个按键方法 目的:当我调用这个方法的时候使按键显示默认值
{
button4.setText("→");
button4.setFocusable(true);
button4.setBackground(Color.WHITE);
button4.setForeground(Color.BLACK);
}
}
class LeftPit extends JPanel
{
JLabel label5 = new JLabel("左陷阱");
JButton button5 = new JButton("J");
char leftpit;
public LeftPit()
{
this.setSize(80,15);
this.setBackground(Color.BLACK);
this.setLayout(null);
label5.setBounds(0,0,60,15);
label5.setForeground(Color.WHITE);
button5.setBounds(50,0,80,15);
button5.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
leftpit = e.getKeyChar();
button5.setText(leftpit+" ");
button5.setFocusable(true);
button5.setFocusable(false);
button5.setForeground(Color.WHITE);
button5.setBackground(Color.BLACK);
}
});
add(label5);
add(button5);
}
public void buttonDefault()
{
button5.setText("J");
button5.setFocusable(true);
button5.setBackground(Color.WHITE);
button5.setForeground(Color.BLACK);
}
}
class RightPit extends JPanel
{
JLabel label6 = new JLabel("右陷阱");
JButton button6 = new JButton("L");
char rightpit;
public RightPit()
{
this.setSize(80,15);
this.setBackground(Color.BLACK);
this.setLayout(null);
label6.setBounds(0,0,60,15);
label6.setForeground(Color.WHITE);
button6.setBounds(50,0,80,15);
button6.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
rightpit = e.getKeyChar();
button6.setText(rightpit+" ");
button6.setFocusable(true);
button6.setFocusable(false);
button6.setBackground(Color.PINK);
button6.setForeground(Color.WHITE);
button6.setBackground(Color.BLACK);
}
});
add(label6);
add(button6);
}
public void buttonDefault()
{
button6.setText("L");
button6.setFocusable(true);
button6.setBackground(Color.WHITE);
button6.setForeground(Color.BLACK);
}
}
class OkReset extends JPanel
{
JButton jrbutton = new JButton("确定");
JButton jrbutton1 = new JButton("重置");
SetupUp setup;
SetDown setdown;
SetLeft setleft;
SetRight setright;
LeftPit leftpit;
RightPit rightpit;
public OkReset(SetupUp setu,SetDown setdow,SetLeft setlef,SetRight setrigh,LeftPit leftpi,RightPit rightpi)
{
this.setup =setu;
this.setdown =setdow;
this.setleft =setlef;
this.setright=setrigh;
this.leftpit =leftpi;
this.rightpit=rightpi;
jrbutton.setFocusable(false);
this.setSize(100,165);
this.setBackground(Color.BLACK);
this.setLayout(null);
jrbutton.setBackground(Color.BLACK);
jrbutton.setForeground(Color.YELLOW);
jrbutton.setBounds(0,0,60,15);
jrbutton1.setBackground(Color.BLACK);
jrbutton1.setForeground(Color.YELLOW);
jrbutton1.setBounds(80,0,60,15);
jrbutton.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
jrbutton1.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
jrbutton1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
setup.buttonDefault();
setdown.buttonDefault();
setleft.buttonDefault();
setright.buttonDefault();
leftpit.buttonDefault();
rightpit.buttonDefault();
jrbutton1.setFocusable(false);
setup.button1.setFocusable(true);
}
});
add(jrbutton);
add(jrbutton1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -