⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dialog.java

📁 模仿Fyrad32编写的四子棋游戏
💻 JAVA
字号:
package fyrad;
import javax.swing.JPanel;//设置游戏参数对话框
import java.awt.Frame;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import java.awt.Dimension;
import javax.swing.JRadioButton;
import java.awt.*;
import javax.swing.*;
public class Dialog extends JDialog 
{

	private static final long serialVersionUID = 1L;//
    private JPanel contentPane = null;//当前面板
    private JLabel label1 = null;//第1个玩家的信息
    private JLabel label2 = null;//第1个玩家的姓名
    private JLabel label3 = null;//提示第1个玩家选择颜色
    private JLabel label4 = null;//第2个玩家的信息
    private JLabel label5 = null;//第2个玩家的姓名
    private JLabel label6 = null;//提示第2个玩家选择颜色
    private JLabel label7 = null;//对战模式
    private JButton button1 = null;//选择玩家1棋子颜色
    private JButton button2 = null;//选择玩家2棋子颜色
    private JButton button3 = null;//确定按钮
    private JButton button4 = null;//取消按钮
    private JRadioButton radioButton1 = null;//选择人人对战
    private JRadioButton radioButton2 = null;//选择人机对战
    private JTextField textField1 = null;//输入玩家1的姓名
    private JTextField textField2 = null;//输入玩家2的姓名
    //主程序参数信息
    public Mode playMode;
	public UserInfo user1,user2;
	public InfoShow info;
	public String user1Name,user2Name;
	public Color user1Color,user2Color;
	ButtonGroup group=new ButtonGroup();  
	public Dialog(Frame owner)
	{ super(owner); initialize(); }
	public Dialog(Frame owner,Mode playmode,UserInfo user1,UserInfo user2,InfoShow info)
	{
		super(owner);
		this.playMode=playmode;
		this.user1=user1;
		this.user2=user2;
		this.info=info;
		initialize();
	}
    //初始化参数信息
	private void initialize() {
		group.add(getJRadioButton1());
		group.add(getJRadioButton2());
		getJRadioButton2().setSelected(true);
		this.setTitle("Options");
		this.setSize(300, 263);
		Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
		int ScreenWidth=screenSize.width;
		int ScreenHeight=screenSize.height;
		int x=(ScreenWidth-this.getWidth())/2;
		int y=(ScreenHeight-this.getHeight())/2;
		this.setLocation(x, y);
		this.setContentPane(getJContentPane());
	}
    //返回设置好的面板
	private JPanel getJContentPane()
    {
		if (contentPane == null) 
		{
			label1 = new JLabel();
			label1.setText("Player1 Info:");
			label1.setBounds(new Rectangle(24, 16, 75, 17));
			label2 = new JLabel();
			label2.setText("Name:");
			label2.setBounds(new Rectangle(24, 42, 37, 17));
			label3 = new JLabel();
			label3.setText("Color:");
			label3.setBounds(new Rectangle(158, 43, 49, 17));
			label3.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
			label4 = new JLabel();
			label4.setText("Player2 Info:");
			label4.setBounds(new Rectangle(24, 71, 75, 17));
			label5 = new JLabel();
			label5.setText("Name:");
			label5.setBounds(new Rectangle(23, 97, 38, 16));
			label6 = new JLabel();
			label6.setText("Color:");
			label6.setBounds(new Rectangle(155, 94, 53, 17));
			label7 = new JLabel();
			label7.setText("Game Type:");
			label7.setBounds(new Rectangle(25, 130, 75, 18));
			contentPane = new JPanel();
			contentPane.setLayout(null);
			contentPane.setName("Setting");
			contentPane.setBorder(BorderFactory.createTitledBorder("Game Setting"));
			contentPane.add(label1, null);
			contentPane.add(label2, null);
			contentPane.add(getJTextField1(), null);
			contentPane.add(label3, null);
			contentPane.add(getJButton1(), null);
			contentPane.add(label4, null);
			contentPane.add(label5, null);
			contentPane.add(getJTextField2(), null);
			contentPane.add(label6, null);
			contentPane.add(getJButton2(), null);
			contentPane.add(label7, null);
			contentPane.add(getJRadioButton1(), null);
			contentPane.add(getJRadioButton2(), null);
			contentPane.add(getJButton3(), null);
			contentPane.add(getJButton4(), null);
		}
		return contentPane;
	}
    //返回显示“Player”的文本框
	private JTextField getJTextField1() 
	{
		if (textField1 == null) 
		{
			textField1 = new JTextField("Player");
			textField1.setBounds(new Rectangle(73, 43, 62, 17));
		}
		return textField1;
	}
    //返回Player1选择颜色按钮
	private JButton getJButton1() 
	{
		if (button1 == null) 
		{
			button1 = new JButton();
			button1.setText("Choose");
			button1.setBackground(Color.yellow);
			button1.setBounds(new Rectangle(210, 43, 79, 18));
			button1.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					System.out.println("actionPerformed()"); 
					JColorChooser color1=new JColorChooser();
					user1Color=JColorChooser.showDialog(color1, "Player1 Color", Color.yellow);
					if(user1Color==null) user1Color=Color.yellow;
					button1.setBackground(user1Color);
				}
			});
		}
		return button1;
	}
    //返回显示“Computer”的文本框
	private JTextField getJTextField2() 
	{
		if (textField2 == null) 
		{
			textField2 = new JTextField("Computer");
			textField2.setBounds(new Rectangle(74, 95, 62, 17));
		}
		return textField2;
	}
    //返回Player2选择颜色按钮
	private JButton getJButton2() 
	{
		if (button2 == null) 
		{
			button2 = new JButton();
			button2.setText("Choose");
			button2.setBackground(Color.red);
			button2.setBounds(new Rectangle(210, 93, 79, 19));
			button2.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					System.out.println("actionPerformed()"); 
					JColorChooser color2=new JColorChooser();
					user2Color=JColorChooser.showDialog(color2, "Player2 Color", Color.red);
					if(user2Color==null) user2Color=Color.red;
					button2.setBackground(user2Color);
				}
			});
		}
		return button2;
	}
    //返回显示“Two Player”的单选按钮
	private JRadioButton getJRadioButton1() 
	{
		if (radioButton1 == null) 
		{
			radioButton1 = new JRadioButton();
			radioButton1.setText("Two Player");
			radioButton1.setBounds(new Rectangle(25, 158,100, 19));
			radioButton1.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					textField1.setText("Player 1");
					textField2.setText("Player 2");
					System.out.println("actionPerformed()"); 
				}
			});
		}
		return radioButton1;
	}
    //返回显示“One Player”的单选按钮
	private JRadioButton getJRadioButton2() 
	{
		if (radioButton2 == null) 
		{
			radioButton2 = new JRadioButton();
			radioButton2.setText("One Player");
			radioButton2.setBounds(new Rectangle(134, 157,100, 21));
			radioButton2.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					textField1.setText("Player");
					textField2.setText("Compuer");
					System.out.println("actionPerformed()");
				}
			});
		}
		return radioButton2;
	}
    //返回"OK"的按钮
	private JButton getJButton3() 
	{
		if (button3 == null) 
		{
			button3 = new JButton();
			button3.setText("OK");
			button3.setBounds(new Rectangle(119, 200, 68, 23));
			button3.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					System.out.println("actionPerformed()");
				    user1Name=getJTextField1().getText();
				    user2Name=getJTextField2().getText();
				    user1.SetName(user1Name);
				    user1.SetColor(user1Color);
				    user2.SetName(user2Name);
				    user2.SetColor(user2Color);
				    user1.nameCtrl.setText("Player Name:"+user1Name);
				    if(user1Color==null) { user1Color=Color.yellow; }
				    user1.userColor=user1Color;
				    user1.colorButton.setBackground(user1Color);
				    user2.nameCtrl.setText("Player Name:"+user2Name);
				    if(user2Color==null) { user2Color=Color.red; }
				    user2.userColor=user2Color;
				    user2.colorButton.setBackground(user2Color);
				    info.SetP1Name(user1Name);
				    info.SetP2Name(user2Name);
                    if(radioButton1.isSelected()) { playMode.playMode=1; }
				    else if(radioButton2.isSelected()) { playMode.playMode=0; }  
				    dispose();
				}
			});
		}
		return button3;
	}

	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	//返回"Cancel"的按钮
	private JButton getJButton4() 
	{
		if (button4 == null) 
		{
			button4 = new JButton();
			button4.setBounds(new Rectangle(200, 200, 85, 23));
			button4.setText("Cancel");
			button4.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					System.out.println("actionPerformed()"); 
					dispose();
				}
			});
		}
		return button4;
	}
	//返回Player姓名
	public String setUser1Name()
	{ return user1Name; }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -