我是java新手“我收到此错误 “没有为getText(String)找到合适的方法 方法JTextComponent.getText(int,int)不适用 (实际和正式的参数列表长度不同) 方法JTextComponent.getText()不适用
(Alt-Enter显示提示)“ 什么时候我尝试运行我的程序。请帮忙。我做错了什么?
package Introduction;
import java.awt.ComponentOrientation;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
public class chaineCaractereNombre extends javax.swing.JFrame {
private Object valeurSaisieTfd;
public chaineCaractereNombre() {
this.cptr = 0;
initComponents();
valeur = (int)(Math.random()*100);
int intervalle =auHasardEntre(valeur, valeur + 100);
Border cadre = BorderFactory.createTitledBorder(" أختار رقما بين " + intervalle +
" و " + (intervalle +100 ) + "، عليك أن تخمن ما هو؟... " );
TitrePnl.setBorder(cadre);
TitrePnl.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
/**
* 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() {
TitrePnl = new javax.swing.JPanel();
saisieTfd = new javax.swing.JTextField();
okBtn = new javax.swing.JButton();
InfoPnl = new javax.swing.JPanel();
resultatTfd = new javax.swing.JTextArea();
ScorePnl = new javax.swing.JPanel();
scoreLbl = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
TitrePnl.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "اخترت عددا، فما هو؟", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.DEFAULT_POSITION));
saisieTfd.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.DEFAULT_POSITION));
saisieTfd.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
saisieTfdKeyReleased(evt);
}
});
okBtn.setBackground(new java.awt.Color(255, 255, 255));
okBtn.setText("نعم");
okBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okBtnActionPerformed(evt);
}
});
javax.swing.GroupLayout TitrePnlLayout = new javax.swing.GroupLayout(TitrePnl);
TitrePnl.setLayout(TitrePnlLayout);
TitrePnlLayout.setHorizontalGroup(
TitrePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TitrePnlLayout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(saisieTfd, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(okBtn)
.addGap(42, 42, 42))
);
TitrePnlLayout.setVerticalGroup(
TitrePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, TitrePnlLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(TitrePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(okBtn)
.addComponent(saisieTfd, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(141, 141, 141))
);
InfoPnl.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "لمزيد من المعلومات", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.DEFAULT_POSITION));
resultatTfd.setColumns(20);
resultatTfd.setFont(new java.awt.Font("Simplified Arabic", 0, 9)); // NOI18N
resultatTfd.setRows(5);
resultatTfd.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.DEFAULT_POSITION), "", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.DEFAULT_POSITION), "", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.DEFAULT_POSITION), "", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.DEFAULT_POSITION));
resultatTfd.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
resultatTfd.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
resultatTfdPropertyChange(evt);
}
});
javax.swing.GroupLayout InfoPnlLayout = new javax.swing.GroupLayout(InfoPnl);
InfoPnl.setLayout(InfoPnlLayout);
InfoPnlLayout.setHorizontalGroup(
InfoPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(InfoPnlLayout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(resultatTfd, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
.addContainerGap())
);
InfoPnlLayout.setVerticalGroup(
InfoPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, InfoPnlLayout.createSequentialGroup()
.addContainerGap(28, Short.MAX_VALUE)
.addComponent(resultatTfd, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31))
);
ScorePnl.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "عداد", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.DEFAULT_POSITION));
scoreLbl.setBackground(new java.awt.Color(255, 255, 255));
scoreLbl.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
scoreLbl.setForeground(new java.awt.Color(100, 100, 100));
scoreLbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
scoreLbl.setText("10");
scoreLbl.setOpaque(true);
javax.swing.GroupLayout ScorePnlLayout = new javax.swing.GroupLayout(ScorePnl);
ScorePnl.setLayout(ScorePnlLayout);
ScorePnlLayout.setHorizontalGroup(
ScorePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 167, Short.MAX_VALUE)
.addGroup(ScorePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(ScorePnlLayout.createSequentialGroup()
.addContainerGap()
.addComponent(scoreLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
.addGap(19, 19, 19)))
);
ScorePnlLayout.setVerticalGroup(
ScorePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 132, Short.MAX_VALUE)
.addGroup(ScorePnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(ScorePnlLayout.createSequentialGroup()
.addContainerGap()
.addComponent(scoreLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
.addContainerGap()))
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 499, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
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()
.addComponent(TitrePnl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(InfoPnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(ScorePnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(29, 29, 29))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(TitrePnl, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(InfoPnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(ScorePnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
TitrePnl.getAccessibleContext().setAccessibleName("لقد اخترت عددا، ما هو؟");
pack();
}// </editor-fold>
private int auHasardEntre (int min, int max){
int resultat = (int) ((max-min) *Math.random() + min);
saisieTfd.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
return resultat;
}
private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//saisir la valeur au clavier
jouer();
}
private void resultatTfdPropertyChange(java.beans.PropertyChangeEvent evt) {
// TODO add your handling code here:
}
private void saisieTfdKeyReleased(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
System.out.println(evt.getKeyCode());
if(evt.getKeyCode() == 10){
jouer();
}
}
private void jouer(){
int valeurSaisie = Integer.parseInt(saisieTfd.getText());
//1: Effacer le contenu de champ de saisie
saisieTfd.getText("");
filDariane = filDariane + valeurSaisie + ">";
//2: Passer a la ligne au bout de 4 valeurs saisie
cptr++;
if(cptr > 4){
filDariane += "\n";
cptr = 0;
}
resultatTfd.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
score--;
scoreLbl.setText(Integer.toString(score));
if(valeur + 100 == valeurSaisie){
// Cas : L'utilisateur gagne
resultatTfd.setText(" أصبت!!! ");
}else{
// Cas : L'utilisateur a perdu
if(valeurSaisie > valeur + 100){
resultatTfd.setText("أكبر!! " + filDariane );
}else{
resultatTfd.setText("أصغر !! " + filDariane );
}
}
}
/**
* @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(chaineCaractereNombre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(chaineCaractereNombre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(chaineCaractereNombre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(chaineCaractereNombre.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 chaineCaractereNombre().setVisible(true);
}
});
}
private int valeur;
private int score=10;
private int cptr=0;
private String filDariane="";
// Variables declaration - do not modify
private javax.swing.JPanel InfoPnl;
private javax.swing.JPanel ScorePnl;
private javax.swing.JPanel TitrePnl;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton okBtn;
private javax.swing.JTextArea resultatTfd;
private javax.swing.JTextField saisieTfd;
private javax.swing.JLabel scoreLbl;
// End of variables declaration
}
答案 0 :(得分:1)
此处,方法private void jouer()
:
saisieTfd.getText("");
当此方法根本不需要任何参数时,您将发送一个空String
作为参数。只需从那里删除空字符串:
saisieTfd.getText();
或者您可能希望将文本设置为空字符串。如果这是您想要/需要的,请拨打setText
:
saisieTfd.setText("");
答案 1 :(得分:0)
您无法致电
saisieTfd.getText(""); // <-- get doesn't take a String
我想你想要
saisieTfd.setText(""); // <-- like this