当前位置: 首页 > 知识库问答 >
问题:

找不到适用于getText(String)的方法

孙光临
2023-03-14

我对java“I get this error”不熟悉,找不到适合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                   
}

共有2个答案

鞠晋
2023-03-14

你不能打电话

saisieTfd.getText(""); // <-- get doesn't take a String

我想你想要

saisieTfd.setText(""); // <-- like this
宫修贤
2023-03-14

这里,在方法私有无效jouer()中:

saisieTfd.getText("");

当此方法根本不需要任何参数时,您将发送一个空的字符串作为参数。只需删除其中的空字符串:

saisieTfd.getText();

或者您可能希望将文本设置为空字符串。如果这是您想要/需要的,请调用setText

saisieTfd.setText("");

 类似资料:
  • 我试图用java创建一个分数计算器,我使用drop box int Joptionpane来显示算术运算,但是每当我编译代码时,总是会出现错误。 这是我得到的全部错误。 .java:47:错误:找不到适合showInputDialog(,String,int,,String[],String)的方法String input=(String)JOptionPane。showInputDialog(n

  • 我想允许用户上传两个图像,封面和徽标。然后将它们保存在fi恢复中。我在行中出错 找不到适用于into(Uri)方法RequestCreator的方法。into(目标)不适用(参数不匹配;Uri无法转换为目标)方法RequestCreator。into(ImageView)不适用(参数不匹配;Uri无法转换为ImageView)

  • 我试图捕获并存储svg元素中出现的一些文本,例如 我有一个我正在使用的方法,它适用于其他元素,但Selenium WebElement类方法getText不会为上述svg元素返回任何文本。 下面是上述示例的xpath 我可以使用findElement(By.xpath(myXpath)),但是当我调用. getText()时,它不会返回任何值,也不会抛出任何错误。 我有没有做错什么,或者可能有其他

  • 我在让gettext在WindowsServer2008R2和PHP5.5上工作时遇到问题 我在php脚本中执行以下操作: 我的翻译文件位于以下目录中: C:/path_to_translations/翻译/NB/LC_MESSAGES/messages.poC:/path_to_translations/翻译/NB/LC_MESSAGES/messages.mo 这在使用PHP5.3的Windo

  • 问题内容: 我有一个奇怪的编译问题。我无法解决此问题。同样的代码在另一个项目中也能正常工作 我收到错误消息 但是,当我这样做时,不会出现任何错误。但是我没想到这一点。 我不确定这是否是Eclipse问题。感谢您的帮助。 问题答案: 由于是通用接口,因此您应该像这样调用:

  • 它给我一条错误消息,说 PrintStream类型中的printf(String, Object...)方法不适用于参数(String, val)