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]; //擴充後的矩陣(需轉換座標)
}