- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在为 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
}
`
最佳答案
在 initComponents
方法之后,有 2 个结束 }
(在 pack()
之后)。删除 1 个括号并将其添加到 addAction()
方法之后。这将解决您的问题
替换类名的代码:
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 Test extends javax.swing.JFrame {
/**
* Creates new form TicTacToe
*/
public Test() {
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();
}
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(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Test.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 Test().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
}
关于java - Netbeans IDE 8.02 Java jFrame GUI bulder initComponents 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48283534/
在 ExtJS 4 中构建应用程序时我很挣扎,其中一部分是关于何时在 initComponent() 中配置某些内容以及何时不... 例如在 Sencha 自己的MVC Application Arc
我在 initComponent() 中创建了一些项目 问题是,this.items不知何故指的是类变量,而不是实例变量。 因此,当我创建两个实例时,我最终会得到两个按钮。 items: [], in
我正在使用 Sencha Architect 2。我正在尝试使用文本搜索和显示结果的表格生成通用 UI 元素。通用意味着我想将它用于几种不同类型的搜索,例如对于用户,角色或其他内容。 所以我在这个关于
我在 ExtJs 工具栏中有一个按钮,如下所示 Ext.define('Member.view.members.MembersGrid', { extend: 'Ext.grid.Panel',
当我每次看到 initComponents(); 创建 JFrame 时在我的构造函数中。它是什么?如果我删除它会发生什么? 最佳答案 initcomponents() 是 NetBeans(我猜你正
我正在使用 Java 制作 JFrame 应用程序,我正在使用应用程序设计器在我的 JFrame 中插入组件。在 Jtextarea 中,我想显示一些文本,但该文本是由我在类中编写的函数返回的。所以我
我正在扩展 Ext 3.3 中的一个窗口,该窗口在许多地方被调用,并且自身有许多扩展。此窗口有一个可配置的字段。当这个字段为空,或者不包含任何项目时,我想显示一条错误消息,而不是继续显示窗口。这就是我
我正在 NetBeans 中构建 JApplet。当应用程序最初运行时,它需要先将一些文件下载到本地 PC 上才能正常运行。一旦完成下载这些文件,就应该绘制 GUI。如何暂停 JApplet 绘制 G
我正在使用 netBeans 编辑器创建桌面应用程序,我做错了生成initComponent() 方法中与数据库连接相关的三行代码。然后我通过在 txt 编辑器中打开 .java 文件删除了这些行,但
在 extjs 中,您始终可以通过 constructor() 扩展 extjs 类。对于从 Component 派生的类,您还可以通过 initComponent() 进行扩展。 我想知道为什么这么
MainFrame.java -JFrame public MainFrame() { initComponents(); Letters pl = new Lette
可能有更好的方法来做到这一点,如果可能的话,请为我提供如何实现它的指导,但我想知道是否有一种方法,在 Tab Click 上,显式调用该容器的 initComponent 函数?即,位于该选项卡面板内
我对使用 EXTJS 有点陌生,所以我正在使用一个教程(修改它以满足我的要求),但我遇到了一个错误。这是教程代码(我无法发布我的实际代码,但它具有相同的思想,只是方法名称不同): Ext.de
我需要在 mvc 模式的 sencha 中呈现一个模板,所以在 InitComponent 上我已经声明了一些变量,但我无法在 init 函数之外访问这些变量。我做了以下尝试 Ext.define('
我正在尝试运行 this ( http://jsfiddle.net/existdissolve/dkJb9/ ) 我的 Extjs 项目中的示例。但我读过我们可以替换使用 initComponent
我已经使用 ExtJS 一段时间了,但现在正在尝试转换我的项目以正确使用他们推荐的 MVC 风格。 我有一个 View 和一个 Controller 。我正在尝试为 boxready 声明一个事件监听
我有一个像这样的面板 var filterPanel = Ext.create('Ext.panel.Panel', { bodyPadding: 5, width:
我正在使用 Ext JS4 开发一个项目。在我们的一些类中,我们在 initComponent 函数中声明函数,然后可以将其设置为控件的处理程序。我将在下面包括一个例子。忽略类中的大部分内容,关键细节
免责声明:我对 ExtJS(版本 5.01)比较陌生。我希望能联系到一些 ExtJS 专家来为我指明正确的方向:在 items 配置内指定 initComponent 方法时,我收到错误。下面的代码生
我正在使用 Ext.define() 以 ExtJS 的新 MVC 方式编写所有组件。 我有点纠结是在 initComponent() 内部定义属性,还是简单地设置它们,如 property: 42,
我是一名优秀的程序员,十分优秀!