📄 attpanel.java
字号:
package system;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
//游戏的属性面板类
public class AttPanel
extends JPanel {
private MainFrame mainFrame;
private NamePanel namePanel; //绘制游戏名称的面板
private LevelScorePanel LSPanel;
private ButtonPanel buttonPanel; //含有控制游戏的开始,暂停,继续按钮的面板
//把ButtonPanel类里的三个按钮在这里声明,
//为了使程序能从MainFrame类里调用,从而设置是否可用状态
private JButton start = new JButton("开始");
private JButton stop = new JButton("暂停");
private JButton goOn = new JButton("继续");
//显示难度等级和得分的文本域
JTextField levelField = new JTextField(3);
JTextField scoreField = new JTextField(9);
public AttPanel(MainFrame frame) {
this.mainFrame = frame;
this.namePanel = new NamePanel();
this.LSPanel = new LevelScorePanel(this);
this.buttonPanel = new ButtonPanel(this);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
JSplitPane top = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
this.namePanel,this.LSPanel);
top.setDividerLocation(80);
top.setDividerSize(0);
topPanel.add(top);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BorderLayout());
JSplitPane bottom = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
this.buttonPanel,new JPanel());
bottom.setDividerLocation(35);
bottom.setDividerSize(0);
bottomPanel.add(bottom);
JSplitPane s = new JSplitPane(JSplitPane.VERTICAL_SPLIT,topPanel,bottomPanel);
s.setDividerLocation(200);
s.setDividerSize(0);
this.setLayout(new BorderLayout());
this.add(s);
}
public JButton getStartButton() {
return this.start;
}
public JButton getStopButton() {
return this.stop;
}
public JButton getGoOnButton() {
return this.goOn;
}
public JTextField getLevelField() {
return this.levelField;
}
public JTextField getScoreField() {
return this.scoreField;
}
public MainFrame getMainFrame() {
return this.mainFrame;
}
//显示难度等级
public void setLevel(int level){
this.levelField.setText(level+"");
}
//显示得分
public void setScore(int score){
this.scoreField.setText(score+"");
}
}
//=========================绘制游戏名称的内部面板类=================================
class NamePanel
extends JPanel {
public NamePanel() {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//===========================绘制游戏名称字符===============================
int width = getWidth();
int height = getHeight();
g2.setFont(new Font("Dialog", Font.BOLD, 36)); //设置绘制字符串的字体
FontMetrics fm = g2.getFontMetrics();
int ascent = fm.getAscent();
int descent = fm.getDescent();
int length = fm.stringWidth(Config.GAME_NAME);
int x = (width - length) / 2;
int y = (height - (ascent + descent)) / 2 + ascent;
//绘制带阴影效果的字符串
g2.setColor(new Color(0, 0, 0, 200)); //字符串背景色
g2.drawString(Config.GAME_NAME, x, y);
g2.setColor(Color.red); //字符串前景色--红色
g2.drawString(Config.GAME_NAME, x - 2, y - 2);
//=======================绘制作者字符串===============================
g2.setFont(new Font("Dialog", Font.PLAIN, 12));
fm = g2.getFontMetrics();
length = fm.stringWidth("华游工作室");
x = (width - length) / 2;
y += 20;
g2.setColor(Color.BLACK);
g2.drawString("华游工作室", x, y);
}
}
//=======================显示得分和难度等级的面板==============================
class LevelScorePanel
extends JPanel {
private JTextField levelField;
private JTextField scoreField;
private GridBagConstraints g = new GridBagConstraints();
public LevelScorePanel(AttPanel p) {
setLayout(new GridBagLayout());
add(new JLabel("难度等级"),0,0,1,1);
levelField = p.getLevelField();
levelField.setEditable(false);
levelField.setHorizontalAlignment(JTextField.RIGHT);
levelField.setOpaque(false);
levelField.setFocusable(false);
levelField.setForeground(Color.red);
levelField.setText(p.getMainFrame().getLevel() + "");
add(levelField,1,0,1,1);
add(new JLabel("得 分"),2,0,1,1);
scoreField = p.getScoreField();
scoreField.setEditable(false);
scoreField.setHorizontalAlignment(JTextField.RIGHT);
scoreField.setOpaque(false);
scoreField.setForeground(Color.blue);
scoreField.setFocusable(false);
scoreField.setText(p.getMainFrame().getScore() + "");
add(scoreField,3,0,1,1);
}
public void add(Component c, int x, int y,int w,int h){
g.gridx = y;
g.gridy = x;
g.gridwidth = w;
g.gridheight = h;
g.weightx = 0;
g.weighty = 0;
add(c,g);
}
}
//====================带有开始,暂停,继续三个按钮的内部面板类=========================
class ButtonPanel
extends JPanel {
public ButtonPanel(final AttPanel p) {
FlowLayout f = new FlowLayout(FlowLayout.LEFT);
setLayout(f); //设置布局为向左对齐的流布局
JButton start = p.getStartButton();
start.setFocusable(false);
start.setToolTipText("开始新游戏,热键为" + KeyEvent.getKeyText(Config.START));
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p.getMainFrame().startNewGame();
}
});
JButton stop = p.getStopButton();
stop.setFocusable(false);
stop.setToolTipText("暂停游戏,热键为" + KeyEvent.getKeyText(Config.STOP));
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p.getMainFrame().stopGame();
}
});
JButton goOn = p.getGoOnButton();
goOn.setFocusable(false);
goOn.setToolTipText("继续游戏,热键为" + KeyEvent.getKeyText(Config.CONTINUE));
goOn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p.getMainFrame().continueGame();
}
});
add(start);
add(stop);
add(goOn);
stop.setEnabled(false);
goOn.setEnabled(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -