在 JTextField 中创建用户无法在 java 中编辑的文本

Create Text in JText Field that user can not edit in java

我正在制作一款类似于星球大战游戏 sabacc 的游戏。我正在尝试创建一个 Jtextfield,它已经在屏幕上显示了三个卡片套件。用户将按下一个按钮,根据他们按下的按钮,纸牌花色将变为不同的花色。如果他们得到三个相同的套房,他们就赢了。不过,我无法将文本显示在屏幕上。截至目前,我一直收到一条错误消息,指出静态内容无法引用非静态方法。

这是我的主应用程序代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CardApp extends JFrame implements ActionListener {

    private JButton oneButton, 
                    twoButton, 
                    threeButton;  
    private int width = 25;
    private int height = 15;



    public CardApp() {
        //JPanel boardPanel = new JPanel(new GridLayout(height,width));
        JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
        JTextField TextField = new JTextField(30); 

        Hand settingTheText = new Hand();

        TextField.setText(settingTheText.ListOfCards());

        oneButton = new JButton("1");
        twoButton = new JButton("2");
        threeButton = new JButton("3");


        // Listen for events on each button
        oneButton.addActionListener(this);
        twoButton.addActionListener(this);
        threeButton.addActionListener(this);

        // Add each to the panel of buttons
        buttonPanel.add(oneButton); 
        buttonPanel.add(twoButton); 
        buttonPanel.add(threeButton); 
       // Add everything to a main panel attached to the content pane
        JPanel mainPanel = new JPanel(new BorderLayout());
        getContentPane().add(mainPanel);
        mainPanel.add(TextField, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        setTitle("Sabacc Example by Angela Rucci");
        setSize(375, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    public void actionPerformed(ActionEvent e) {
                int pressed = 0;
                if (e.getSource() == oneButton){
                        pressed = 1;}
                if (e.getSource() == twoButton){
                        pressed = 2;}
                if (e.getSource() == threeButton){
                        pressed = 3;}
                 Hand handObject = new Hand();

///This IS WHERE IM GETTING MY ERROR!//

                String screenText = handObject.ListOfCards();
                TextField.setText(screenText);
    }


public static void main(String[] args) {

    CardApp c = new CardApp();


}

}

这是我获取花色列表的另一个文件

package cardapp;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Hand {
String [] Suits = {"C", "H", "S", "D"};
String [] probability = {"C","H","R","D"};
Random randomInt = new Random ();
String RandomSuit;
String RandomShuffle;
String ThreeSuits;
String LeftSuit;
String MiddleSuit;
String RightSuit;
int pressed = 0;



       public int Discards(int pressedNumber){

              return pressed;

             }



       public void Randomizer (){

           int RandomSuitNumber = randomInt.nextInt(4);//this is generator a random number

           //------------------Decide what hand to randomize --------------------------//
           if (pressed==1){
                  LeftSuit= Suits[RandomSuitNumber];
                  }

              if (pressed==2){
                 MiddleSuit=Suits[RandomSuitNumber];
                  }

              if (pressed==3){
                  RightSuit=Suits[RandomSuitNumber];
                    }
          //----------------20% chance of new random set------------------------------------//
            int ProabilityRandomNum = randomInt.nextInt(5);//this will create a random number for probability array
            RandomShuffle= probability[ProabilityRandomNum];//this will pick a random letter in proability array


          //------------If proability array equals R then change all of the suits----------//  
            if (RandomShuffle.equals("R")){
                JOptionPane.showMessageDialog(null, "Randomized Hand!");
                int leftNumber = randomInt.nextInt(4);
                int middleNumber = randomInt.nextInt(4);
                int rightNumber = randomInt.nextInt(4);
                LeftSuit= Suits[leftNumber];
                MiddleSuit= Suits[middleNumber];
                RightSuit= Suits[rightNumber];}

            ThreeSuits = (LeftSuit + MiddleSuit + RightSuit); 
       }


       public String ListOfCards (){
              return ThreeSuits;
             }




         public void GameOver(){
                  if (LeftSuit == MiddleSuit && MiddleSuit == RightSuit &&    


    RightSuit== LeftSuit){
                      JOptionPane.showMessageDialog(null, "WINNER!!");
                     }
             }
    }

变量是方法的局部变量。 JTextField TextField 仅对 CardApp() 可见。如果您希望它对整个 class 可用,请将其作为私有 class 成员:

public class CardApp extends JFrame implements ActionListener {

    private JButton oneButton, 
                    twoButton, 
                    threeButton;  
    private int width = 25;
    private private int height = 15;
    // available to all methods
    // better naming convention was JTextfield tf = new JTextField(30);
    // even Whosebug thinks its a class name :)
    // see the color highlighting
    private JTextField TextField = new JTextField(30);

    public CardApp() {
        //JPanel boardPanel = new JPanel(new GridLayout(height,width));
        JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
        //JTextField TextField = new JTextField(30); 

        Hand settingTheText = new Hand();

        TextField.setText(settingTheText.ListOfCards());
     }
     //
     // code continues here ...
     //
}