每当放置一个新的连接四件式标签时,前一个标签就会消失。 如何确保所有连续可见?
JFrame方法
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class C4_JFrame {
public static void main(String[] argS) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new Connect_Four("Connect Four");
frame.setSize(1400, 775);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
});
}
}
主要方法(转动方法,标签处理,最后)
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Connect_Four extends JFrame {
static JLabel background, label1, label2, label3, label4, label5, label6, label7, label8, label9;
static JLabel[] piecelabel;
static int piececounter;
static boolean turn_flag;
private static final long serialVersionUID = 1L;
public Connect_Four(String title) {
String board[][];
board = new String[7][6];
int counter[];
piececounter = 0;
turn_flag = false;
counter = new int[7];
for (int x = 0; x < 7; x++) {
counter[x] = 0;
for (int y = 0; y < 6; y++) {
board[x][y] = "empty";
}
}
JPanel panel = new JPanel();
setContentPane(panel);
getLayeredPane();
JTextArea display = new JTextArea();
display.setBounds(1175, 550, 190, 30);
Font font = new Font("Verdana", Font.BOLD, 12);
display.setFont(font);
display.setText("Too many pieces in that row\nPlease select a different row");
panel.add(display);
display.setVisible(false);
background = new JLabel();
label1 = new JLabel();
label2 = new JLabel();
label3 = new JLabel();
label4 = new JLabel();
label5 = new JLabel();
label6 = new JLabel();
label7 = new JLabel();
label8 = new JLabel();
label9 = new JLabel();
background.setIcon(new ImageIcon("F:/Connect4Board.png"));
background.setBounds(0, 100, 1175, 650);
background.setOpaque(false);
panel.setLayout(null);
panel.add(background);
// label1-label7 are all the same image
label1.setIcon(new ImageIcon("F:/arrow.jpg"));
label1.setBounds(75, 20, 40, 40);
panel.add(label1);
label2.setIcon(new ImageIcon("F:/arrow.jpg"));
label2.setBounds(240, 20, 40, 40);
panel.add(label2);
label3.setIcon(new ImageIcon("F:/arrow.jpg"));
label3.setBounds(405, 20, 40, 40);
panel.add(label3);
label4.setIcon(new ImageIcon("F:/arrow.jpg"));
label4.setBounds(580, 20, 40, 40);
panel.add(label4);
label5.setIcon(new ImageIcon("F:/arrow.jpg"));
label5.setBounds(755, 20, 40, 40);
panel.add(label5);
label6.setIcon(new ImageIcon("F:/arrow.jpg"));
label6.setBounds(930, 20, 40, 40);
panel.add(label6);
label7.setIcon(new ImageIcon("F:/arrow.jpg"));
label7.setBounds(1109, 20, 40, 40);
panel.add(label7);
label8.setIcon(new ImageIcon("F:/yourturn.png"));
label8.setBounds(1175, 300, 190, 240);
panel.add(label8);
label9.setIcon(new ImageIcon("F:/blackturn.png"));
label9.setBounds(1175, 300, 190, 240);
panel.add(label9);
piecelabel = createLabels();
JButton row1 = new JButton();
JButton row2 = new JButton();
JButton row3 = new JButton();
JButton row4 = new JButton();
JButton row5 = new JButton();
JButton row6 = new JButton();
JButton row7 = new JButton();
panel.add(row1);
row1.setBounds(75, 20, 40, 40);
row1.setOpaque(false);
panel.add(row2);
row2.setBounds(240, 20, 40, 40);
row2.setOpaque(false);
panel.add(row3);
row3.setBounds(405, 20, 40, 40);
row3.setOpaque(false);
panel.add(row4);
row4.setBounds(580, 20, 40, 40);
row4.setOpaque(false);
panel.add(row5);
row5.setBounds(755, 20, 40, 40);
row5.setOpaque(false);
panel.add(row6);
row6.setOpaque(false);
row6.setBounds(930, 20, 40, 40);
panel.add(row7);
row7.setBounds(1109, 20, 40, 40);
row7.setOpaque(false);
row1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int x = 0;
turn(x, counter, board, panel, display);
}
});
row2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int x = 1;
turn(x, counter, board, panel, display);
}
});
row3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int x = 2;
turn(x, counter, board, panel, display);
}
});
row4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int x = 3;
turn(x, counter, board, panel, display);
}
});
row5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int x = 4;
turn(x, counter, board, panel, display);
}
});
row6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int x = 5;
turn(x, counter, board, panel, display);
}
});
row7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int x = 6;
turn(x, counter, board, panel, display);
}
});
}
public JLabel[] createLabels() {
JLabel[] labels = new JLabel[42];
for (int i = 0; i < 42; i++) {
labels[i] = new JLabel();
}
return labels;
}
public static void turn(int x, int counter[], String board[][], JPanel panel, JTextArea display) {
if (counter[x] < 6) {
display.setVisible(false);
if (turn_flag == false) {
board[x][counter[x]] = "red";
piecelabel[piececounter].setIcon(new ImageIcon("F:/redpiece.png"));
turn_flag = true;
label8.setVisible(false);
}
else if (turn_flag == true) {
board[x][counter[x]] = "black";
piecelabel[piececounter].setIcon(new ImageIcon("F:/blackpiece.jpg"));
turn_flag = false;
label8.setVisible(true);
}
piecelabel[piececounter].setBounds(25 + (x * 169), (650 - (counter[x] * 109)), 135, 100);
panel.add(piecelabel[piececounter]);
panel.setComponentZOrder(piecelabel[piececounter], 0);
// adding Z order so the buttons are in front of the board
panel.setComponentZOrder(background, 1);
panel.repaint(); // makes the first connect four piece visible
// (didn't appear for some reason)
if(piececounter==1)
counter[x] = counter[x] + 1;
piececounter++;
System.out.println(piececounter);
} else
display.setVisible(true);
}
}