📄 computergamedialog.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* this class creates a dialog, and set up the Human vs. Computer game
*
*/
class ComputerGameDialog extends JDialog
{
JLabel lb1 = new JLabel("请输入您的昵称:");
JTextField nameTf = new JTextField(10);
JButton bn1 = new JButton("确定");
JButton bn2 = new JButton("取消");
JTextField timeTf = new JTextField(10);
ButtonGroup g = new ButtonGroup();
JRadioButton rb1 = new JRadioButton("黑棋", true);
JRadioButton rb2 = new JRadioButton("白棋");
ChessMan chessMan;
boolean dialogResult;
public int totalSeconds;
/**
*
* @param parent the parent frame
* @param w the width of the dialog
* @param h the height of the dialog
*/
public ComputerGameDialog(JFrame parent, int w, int h)
{
//set up the dialog layout
super(parent, "设置", true);
setSize(w, h);
setLayout(new GridLayout(4, 1));
setLocation(400, 300);
JPanel p = new JPanel();
p.add(lb1);
p.add(nameTf);
add(p);
JPanel p1 = new JPanel();
p1.add(new JLabel("本方执"));
g.add(rb1);
g.add(rb2);
p1.add(rb1);
p1.add(rb2);
add(p1);
JPanel p2 = new JPanel();
timeTf.setText("600");
p2.add(new JLabel("下棋总时间"));
p2.add(timeTf);
add(p2);
JPanel p3 = new JPanel();
p3.setLayout(new FlowLayout());
p3.add(bn1);
p3.add(bn2);
add(p3);
//add action listener
bn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
if(rb1.isSelected() == true)
chessMan = ChessMan.Black;
else
chessMan = ChessMan.White;
totalSeconds = Integer.parseInt(timeTf.getText());
dialogResult = true;
}
catch(NumberFormatException ex)
{
System.out.println("Parse Integer Error!");
}
finally{
setVisible(false);
}
}
});
//if cancel button is pressed
bn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dialogResult = false;
setVisible(false);
}
});
//add exit window event
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{setVisible(false);}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -