用户界面Netbeans IDE 8.02 Java jFrame GUI bulder初始化组件错误
我目前正在为tic tak toe游戏构建一个GUI,我已经完成了所有的工作,但是当我试图运行它时失败了,所以我检查了代码以找到问题,目前我唯一能找到的是initComponents是红线,我很难找到原因
我目前是Java新手,最近才开始编写代码,因此非常感谢在这方面的任何帮助
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import java.awt.Component;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author matt
*/
public class TicTacToe extends javax.swing.JFrame {
/**
* Creates new form TicTacToe
*/
public TicTacToe() {
initComponents();
addAction();
}
public void winEffect(JButton b1, JButton b2, JButton b3)
{
b1.setBackground(Color.black);
b2.setBackground(Color.black);
b3.setBackground(Color.BLACK);
b1.setForeground(Color.WHITE);
b2.setForeground(Color.WHITE);
b3.setForeground(Color.WHITE);
String msg = b1.getText()+ " Is the winner";
jLabelMSG.setText(msg);
}
boolean win=false;
public void getthewinner(){
if(jButton1.getText().equals("")&& jButton1.getText().equals(jButton2.getText())&& jButton1.getText().equals(jButton3.getText())){
winEffect(jButton1, jButton2, jButton3);
win=true;
}
if(jButton4.getText().equals("")&& jButton4.getText().equals(jButton5.getText())&& jButton4.getText().equals(jButton6.getText())){
winEffect(jButton4, jButton5, jButton6);
win=true;
}
if(jButton7.getText().equals("")&& jButton7.getText().equals(jButton8.getText())&& jButton7.getText().equals(jButton9.getText())){
winEffect(jButton7, jButton8, jButton9);
win=true;
}
if(jButton1.getText().equals("")&& jButton1.getText().equals(jButton4.getText())&& jButton1.getText().equals(jButton7.getText())){
winEffect(jButton1, jButton4, jButton7);
win=true;
}
if(jButton2.getText().equals("")&& jButton2.getText().equals(jButton5.getText())&& jButton2.getText().equals(jButton9.getText())){
winEffect(jButton2, jButton5, jButton8);
win=true;
}
if(jButton3.getText().equals("")&& jButton3.getText().equals(jButton6.getText())&& jButton3.getText().equals(jButton9.getText())){
winEffect(jButton3, jButton6, jButton9);
win=true;
}
if(jButton1.getText().equals("")&& jButton1.getText().equals(jButton5.getText())&& jButton1.getText().equals(jButton9.getText())){
winEffect(jButton1, jButton5, jButton9);
win=true;
}
if(jButton3.getText().equals("")&& jButton3.getText().equals(jButton5.getText())&& jButton3.getText().equals(jButton7.getText())){
winEffect(jButton3, jButton5, jButton7);
win=true;
}
else if(allButtonTextLegnth()==9 && win==false){
jLabelMSG.setText("no one wins");
}
}
public int allButtonTextLegnth(){
String txt ="";
Component[]comps= jPanel3.getComponents();
for(Component comp :comps){
if(comp instanceof JButton){
JButton button = (JButton)comp;
txt = txt +button.getText();
}
}
return txt.length();
}
int x_or_o =0;
public ActionListener createAction(JButton button){
ActionListener al = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(button.getText().equals("")){
if((x_or_o %2)==0){
button.setText("x");
button.setForeground(Color.BLUE);
jLabelMSG.setText("o turn now");
getthewinner();
}else{
button.setText("o");
button.setForeground(Color.red);
jLabelMSG.setText("x turn now");
getthewinner();
}
x_or_o ++;
}
}
};
return al;
}
//function to add action to the buttons
public void addAction(){
Component[]comps= jPanel3.getComponents();
for(Component comp :comps){
if(comp instanceof JButton){
JButton button = (JButton)comp;
button.addActionListener(createAction(button));
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
jLabelMSG = new javax.swing.JLabel();
jButtonReplay = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton6.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton7.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton8.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jButton9.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jPanel3.setBackground(new java.awt.Color(255, 51, 51));
jLabelMSG.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabelMSG.setForeground(new java.awt.Color(255, 255, 255));
jLabelMSG.setText("jLabel1");
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(94, 94, 94)
.addComponent(jLabelMSG)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jLabelMSG)
.addContainerGap(19, Short.MAX_VALUE))
);
jButtonReplay.setText("replay");
jButtonReplay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonReplayActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 80, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jButtonReplay, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
.addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(94, 94, 94))))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
.addComponent(jButtonReplay)
.addGap(24, 24, 24))
);
pack();
}// </editor-fold>
}
private void jButtonReplayActionPerformed(java.awt.event.ActionEvent evt) {
win =false;
Component[]comps= jPanel3.getComponents();
for(Component comp :comps){
if(comp instanceof JButton){
JButton button = (JButton)comp;
button.setText("");
button.setBackground(Color.WHITE);
jLabelMSG.setText("play");
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TicTacToe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
`enter code here`java.util.logging.Logger.getLogger(TicTacToe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
`enter code here`java.util.logging.Logger.getLogger(TicTacToe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) { `enter code here`java.util.logging.Logger.getLogger(TicTacToe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TicTacToe().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JButton jButtonReplay;
private javax.swing.JLabel jLabelMSG;
private javax.swing.JPanel jPanel3;
// End of variables declaration
}
`
# 1 楼答案
在
initComponents
方法之后,有2个结束}
(在pack()
之后)。移除1个括号,并将其添加到addAction()
方法之后。这会解决你的问题替换为类名的代码: