JAVA实现网络对战带技能五子棋(一)——图形界面设计(下)

JAVA实现网络对战带技能五子棋(一)——图形界面设计(下)

Jpanel、标签、按钮设置

首先进入上篇中Demo.java的Design界面,可以直接将左边工具栏中的组件拖至界面,并且可以编辑所选定组件的属性(布局、名称、背景色、内容等)
在这里插入图片描述
在这里为了方便自由摆放组件,我将上图中主面板Layout设置为Absolute Layout。然后将Jpanel、标签、按钮拖入界面,并设置好属性,界面更改后代码也会同步更改。初始化部分的代码与效果如下:

    public JLabel l1 = new JLabel();     //提示内容
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
    
    
		frame = new JFrame();
		frame.getContentPane().setBackground(new Color(250, 250, 210));
		frame.setBounds(100, 100, 800, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		panel.setForeground(new Color(0, 0, 0));
		panel.setBackground(new Color(238, 232, 170));
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JButton okbn = new JButton("\u51C6\u5907");
		okbn.setFont(new Font("楷体", Font.PLAIN, 17));
		okbn.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
			}
		});
		okbn.setBounds(659, 139, 117, 31);
		panel.add(okbn);
		
		JButton endbn = new JButton("\u7ED3\u675F\u56DE\u5408");
		endbn.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
			}
		});
		endbn.setFont(new Font("楷体", Font.PLAIN, 17));
		endbn.setBounds(659, 180, 117, 31);
		panel.add(endbn);
		
		JButton giveupbn = new JButton("\u8BA4\u8F93");
		giveupbn.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
			}
		});
		giveupbn.setFont(new Font("楷体", Font.PLAIN, 17));
		giveupbn.setBounds(659, 221, 117, 31);
		panel.add(giveupbn);
		
		JButton jn1 = new JButton("\u5927\u7206\u70B8");
		jn1.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
			
			}
		});
		jn1.setFont(new Font("楷体", Font.PLAIN, 17));
		jn1.setBounds(659, 316, 117, 31);
		panel.add(jn1);
		
		JButton jn2 = new JButton("LW\u529D\u964D");
		jn2.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
			}
		});
		jn2.setFont(new Font("楷体", Font.PLAIN, 17));
		jn2.setBounds(659, 357, 117, 31);
		panel.add(jn2);
		
		JButton jn3 = new JButton("\u6EE1\u5934\u5927\u6C49");
		jn3.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
				
			}
		});
		jn3.setFont(new Font("楷体", Font.PLAIN, 17));
		jn3.setBounds(659, 398, 117, 31);
		panel.add(jn3);
		
		JButton jn4 = new JButton("\u8FD4\u672C\u56DE\u6E90");
		jn4.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
			}
		});
		jn4.setFont(new Font("楷体", Font.PLAIN, 17));
		jn4.setBounds(659, 439, 117, 31);
		panel.add(jn4);
		
		JButton jn5 = new JButton("\u547C\u53EBSeven");
		jn5.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
			}
		});
		jn5.setFont(new Font("楷体", Font.PLAIN, 15));
		jn5.setBounds(659, 480, 117, 31);
		panel.add(jn5);
		
		JLabel lblNewLabel = new JLabel("     LW\u6311\u6218\u8D5B");
		lblNewLabel.setFont(new Font("华光行楷_CNKI", Font.PLAIN, 20));
		lblNewLabel.setBounds(260, 10, 153, 31);
		panel.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("       \u6280\u80FD");
		lblNewLabel_1.setFont(new Font("华光行楷_CNKI", Font.PLAIN, 20));
		lblNewLabel_1.setBounds(659, 275, 117, 31);
		panel.add(lblNewLabel_1);
		
		//初始化提示栏
		l1.setText("\u5373\u5C06\u5F00\u6218");
		l1.setFont(new Font("华文行楷", Font.PLAIN, 20));
		l1.setBounds(659, 0, 117, 141);
		panel.add(l1);
	}

在这里插入图片描述

棋盘、棋子绘制

不同于前面机械式的布局,棋盘、棋子绘制需要覆盖Jpanel组件的paint方法与paintComponent方法。在这里我自定义一个lwpanel类继承于Jpanel类,然后在paint方法里绘制棋子,在paintComponent方法里绘制棋盘。这样每次我调用repaint方法就会重新画一次棋子达到更新的效果(因为每次repaint会自动调用paint方法),棋盘却不会重新绘制,节省了资源。

自定义的lwpanel类

class lwpanel extends JPanel{
    
    
		Graphics2D g2;
		public lwpanel(){
    
    
			
		}
		//画棋子
		public void paint(Graphics g) {
    
    
			super.paint(g);
			g2 = (Graphics2D)g;
			for(int i=4;i<=36;i++)
			{
    
    
				for(int j=4;j<=42;j++)
				{
    
    
					if(xy[i][j]==1)
					{
    
    
						g2.setColor(Color.WHITE);
						g2.fillOval((j-3)*15-8,(i-3)*15-8,16,16);
					}
					else if(xy[i][j]==-1)
					{
    
    
						g2.setColor(Color.BLACK);
						g2.fillOval((j-3)*15-8,(i-3)*15-8,16,16);
					}
				}
			}
		}
		//画棋盘
		public void paintComponent(Graphics g){
    
    
			super.paintComponent(g);
			g2 = (Graphics2D)g;
			for (int i=1;i<=hnum;i++ )
			{
    
    
				Line2D line =new Line2D.Double(0,i*15,600,i*15);
				g2.draw(line);
			}
			for (int i=1;i<=lienum;i++ )
			{
    
    
				Line2D line =new Line2D.Double(i*15,0,i*15,510);
				g2.draw(line);
			}
			g2.fillOval(20*15-4,17*15-4,8,8);
			g2.fillOval(16*15-3,13*15-3,6,6);
			g2.fillOval(16*15-3,21*15-3,6,6);
			g2.fillOval(24*15-3,13*15-3,6,6);
			g2.fillOval(24*15-3,21*15-3,6,6);
		}
		private int lienum=39;
		private int hnum=33;
		
	}
	
	public static int[][] xy=new int[41][47];       //扩充后的矩阵(需转换坐标)

整个图形界面代码及效果图

至此就完成了图形界面制作,那么如何根据下的棋子判断是否赢棋了呢?请听下回分解!

package test1;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;

public class Demo {
    
    

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
    
    
		EventQueue.invokeLater(new Runnable() {
    
    
			public void run() {
    
    
				try {
    
    
					Demo window = new Demo();
					window.frame.setVisible(true);
				} catch (Exception e) {
    
    
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Demo() {
    
    
		initialize();
	}

	public lwpanel p1 = new lwpanel();   //棋盘
	public JLabel l1 = new JLabel();     //提示内容
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
    
    
		frame = new JFrame();
		frame.getContentPane().setBackground(new Color(250, 250, 210));
		frame.setBounds(100, 100, 800, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		panel.setForeground(new Color(0, 0, 0));
		panel.setBackground(new Color(238, 232, 170));
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		//初始化棋盘面板
		p1.setBackground(new Color(205, 133, 63));
		p1.setBounds(30, 50, 600, 510);
		panel.add(p1);
		
		JButton okbn = new JButton("\u51C6\u5907");
		okbn.setFont(new Font("楷体", Font.PLAIN, 17));
		okbn.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
			}
		});
		okbn.setBounds(659, 139, 117, 31);
		panel.add(okbn);
		
		JButton endbn = new JButton("\u7ED3\u675F\u56DE\u5408");
		endbn.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
			}
		});
		endbn.setFont(new Font("楷体", Font.PLAIN, 17));
		endbn.setBounds(659, 180, 117, 31);
		panel.add(endbn);
		
		JButton giveupbn = new JButton("\u8BA4\u8F93");
		giveupbn.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
			}
		});
		giveupbn.setFont(new Font("楷体", Font.PLAIN, 17));
		giveupbn.setBounds(659, 221, 117, 31);
		panel.add(giveupbn);
		
		JButton jn1 = new JButton("\u5927\u7206\u70B8");
		jn1.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
			
			}
		});
		jn1.setFont(new Font("楷体", Font.PLAIN, 17));
		jn1.setBounds(659, 316, 117, 31);
		panel.add(jn1);
		
		JButton jn2 = new JButton("LW\u529D\u964D");
		jn2.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
			}
		});
		jn2.setFont(new Font("楷体", Font.PLAIN, 17));
		jn2.setBounds(659, 357, 117, 31);
		panel.add(jn2);
		
		JButton jn3 = new JButton("\u6EE1\u5934\u5927\u6C49");
		jn3.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
				
			}
		});
		jn3.setFont(new Font("楷体", Font.PLAIN, 17));
		jn3.setBounds(659, 398, 117, 31);
		panel.add(jn3);
		
		JButton jn4 = new JButton("\u8FD4\u672C\u56DE\u6E90");
		jn4.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
			}
		});
		jn4.setFont(new Font("楷体", Font.PLAIN, 17));
		jn4.setBounds(659, 439, 117, 31);
		panel.add(jn4);
		
		JButton jn5 = new JButton("\u547C\u53EBSeven");
		jn5.addActionListener(new ActionListener() {
    
    
			public void actionPerformed(ActionEvent e) {
    
    
				
			}
		});
		jn5.setFont(new Font("楷体", Font.PLAIN, 15));
		jn5.setBounds(659, 480, 117, 31);
		panel.add(jn5);
		
		JLabel lblNewLabel = new JLabel("     LW\u6311\u6218\u8D5B");
		lblNewLabel.setFont(new Font("华光行楷_CNKI", Font.PLAIN, 20));
		lblNewLabel.setBounds(260, 10, 153, 31);
		panel.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("       \u6280\u80FD");
		lblNewLabel_1.setFont(new Font("华光行楷_CNKI", Font.PLAIN, 20));
		lblNewLabel_1.setBounds(659, 275, 117, 31);
		panel.add(lblNewLabel_1);
		
		//初始化提示栏
		l1.setText("\u5373\u5C06\u5F00\u6218");
		l1.setFont(new Font("华文行楷", Font.PLAIN, 20));
		l1.setBounds(659, 0, 117, 141);
		panel.add(l1);
	}
	
	class lwpanel extends JPanel{
    
    
		Graphics2D g2;
		public lwpanel(){
    
    
			
		}
		//画棋子
		public void paint(Graphics g) {
    
    
			super.paint(g);
			g2 = (Graphics2D)g;
			for(int i=4;i<=36;i++)
			{
    
    
				for(int j=4;j<=42;j++)
				{
    
    
					if(xy[i][j]==1)
					{
    
    
						g2.setColor(Color.WHITE);
						g2.fillOval((j-3)*15-8,(i-3)*15-8,16,16);
					}
					else if(xy[i][j]==-1)
					{
    
    
						g2.setColor(Color.BLACK);
						g2.fillOval((j-3)*15-8,(i-3)*15-8,16,16);
					}
				}
			}
		}
		//画棋盘
		public void paintComponent(Graphics g){
    
    
			super.paintComponent(g);
			g2 = (Graphics2D)g;
			for (int i=1;i<=hnum;i++ )
			{
    
    
				Line2D line =new Line2D.Double(0,i*15,600,i*15);
				g2.draw(line);
			}
			for (int i=1;i<=lienum;i++ )
			{
    
    
				Line2D line =new Line2D.Double(i*15,0,i*15,510);
				g2.draw(line);
			}
			g2.fillOval(20*15-4,17*15-4,8,8);
			g2.fillOval(16*15-3,13*15-3,6,6);
			g2.fillOval(16*15-3,21*15-3,6,6);
			g2.fillOval(24*15-3,13*15-3,6,6);
			g2.fillOval(24*15-3,21*15-3,6,6);
		}
		private int lienum=39;
		private int hnum=33;
		
	}
	
	public static int[][] xy=new int[41][47];       //扩充后的矩阵(需转换坐标)

}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Lwwwwwwwl/article/details/113008964