JTextfield.getText() 不返回任何值

问题描述 投票:0回答:1

我对java比较陌生,所以我不是最擅长的。在我的程序中,我有一个带有 JTextfields 数组的板,玩家在其中输入值,当他们按下按钮时,如果它是有效的输入,则这些值将存储在另一个数组中。这对于红色代码块来说非常有效,但由于某种原因,即使我在蓝色代码块中编写了一些内容,JTextfield.getText() 方法也不会返回任何值。我到处都看过了。

面板类:

package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class Panel extends JPanel implements Runnable{
    
    
    public JLabel board = new JLabel();
    public JLabel rules = new JLabel();
    public JLabel redsetup = new JLabel("RED SETUP");
    public JLabel bluesetup = new JLabel("BLUE SETUP");
    public JLabel redinvalid = new JLabel("INVALID SETUP");
    public JLabel blueinvalid = new JLabel("INVALID SETUP");
    public JLabel graveyard = new JLabel();
    public JLabel brand = new JLabel();
    public JButton accept = new JButton("OK");
    public JButton acceptII = new JButton("OK");
    public JButton acceptIII = new JButton("OK");
    public JTextField[] redfield = new JTextField[40];
    public JTextField[] bluefield = new JTextField[40];
    public Font font = new Font("Cambria", Font.ITALIC, 28);
    public String turn;
    public String[] redval = new String[40];
    public String[] blueval = new String[40];
    public boolean startGame = false;
    public int posY = 144;
    public int posX = 41;
    public int redtwo = 8;
    public int redone = 1;
    public int redthree = 5;
    public int redfour = 4;
    public int redfive = 4;
    public int redsix = 4;
    public int redseven = 3;
    public int redeight = 2;
    public int rednine = 1;
    public int redten = 1;
    public int redbomb = 6;
    public int redflag = 1;
    public int blueflag =1;
    public int bluebomb = 6;
    public int blueone = 1;
    public int bluetwo = 8;
    public int bluethree = 5;
    public int bluefour = 4;
    public int bluefive = 4;
    public int bluesix = 4;
    public int blueseven = 3;
    public int blueeight = 2;
    public int bluenine = 1;
    public int blueten = 1;
    

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
public Panel() {
        
        this.setPreferredSize(new Dimension(320, 240));
        this.setBackground(Color.black);
        this.setDoubleBuffered(true);
        this.setFocusable(true);
        this.setLayout(null);
        
        for(int i =0; i<40; i++) {
            redfield[i] = new JTextField();
            redfield[i].setBounds(posX, posY, 24, 24);
            redfield[i].setBackground(Color.decode("#a67c52"));
            redfield[i].setForeground(Color.red);
            redfield[i].setBorder(new LineBorder(Color.decode("#77593b"), 1));
            redfield[i].setVisible(false);
            this.add(redfield[i]);
            if(posX == 257) {
                posX = 41;
                posY += 24;
            }else {
                posX+=24;
            }
        }
        
        
        redsetup.setBounds(90, 60, 200, 28);
        redsetup.setFont(font);
        redsetup.setForeground(Color.red);
        redsetup.setVisible(false);
        
        
        this.add(redsetup);
        
        bluesetup.setBounds(90, 60, 200, 28);
        bluesetup.setFont(font);
        bluesetup.setForeground(Color.blue);
        bluesetup.setVisible(false);
        
        
        this.add(bluesetup);
        
        redinvalid.setBounds(70, 10, 200, 28);
        redinvalid.setFont(font);
        redinvalid.setForeground(Color.RED);
        redinvalid.setVisible(false);
        
        
        this.add(redinvalid);
        
        
        blueinvalid.setBounds(70, 10, 200, 28);
        blueinvalid.setFont(font);
        blueinvalid.setForeground(Color.blue);
        blueinvalid.setVisible(false);
        
        
        this.add(blueinvalid);
        
        
        acceptII.setBounds(145,110,30,20);
        acceptII.setBackground(Color.red);
        acceptII.setForeground(Color.BLACK);
        acceptII.setBorder(new LineBorder(Color.BLACK, 2));
        acceptII.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                
                for(int i =0; i<40; i++) {
                    redval[i] = redfield[i].getText();
                    if(redval[i].equals("b") || redval[i].equals("B")) {
                        redbomb--;
                    }else if(redval[i].equals("f") || redval[i].equals("F") ){
                        redflag--;
                    }else if(redval[i].equals("1")){
                        redone--;
                    }else if(redval[i].equals("2")){
                        redtwo--;
                    }else if(redval[i].equals("3")){
                        redthree--;
                    }else if(redval[i].equals("4")){
                        redfour--;
                    }else if(redval[i].equals("5")){
                        redfive--;
                    }else if(redval[i].equals("6")){
                        redsix--;
                    }else if(redval[i].equals("7")){
                        redseven--;
                    }else if(redval[i].equals("8")){
                        redeight--;
                    }else if(redval[i].equals("9")){
                        rednine--;
                    }else if(redval[i].equals("10")){
                        redten--;
                    }
                    if(redbomb != 0 || redflag != 0 || redone != 0 || redtwo != 0 || redthree != 0 || redfour != 0 || redfive != 0 || redfive != 0 || redsix != 0 || redseven != 0 || redeight != 0 || rednine != 0 || redten != 0 ) {
                        redinvalid.setVisible(true);
                    }else {
                        redinvalid.setVisible(false);
                        acceptII.setVisible(false);
                        redsetup.setVisible(false);
                        for(int ii =0; ii<40; ii++) {
                             redfield[ii].setVisible(false);
                             blueSetUpTime();
                         }
                    }
                    
                    
            }
        }});
        this.add(acceptII);
        acceptII.setVisible(false);
        
        acceptIII.setBounds(145,110,30,20);
        acceptIII.setBackground(Color.blue);
        acceptIII.setForeground(Color.BLACK);
        acceptIII.setBorder(new LineBorder(Color.BLACK, 2));
        this.add(acceptIII);
        acceptIII.setVisible(false);
acceptIII.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                //copy 'verify setup' code
                blueVerification();
                
            }
        });
        
        
        ImageIcon bigboard = new ImageIcon("Board.png");
        board.setIcon(bigboard);
        board.setBounds(40, 0, 240,240);
        this.add(board);
        
        board.setVisible(false);
        
        ImageIcon grave = new ImageIcon("Graveyard.png");
        graveyard.setIcon(grave);
        graveyard.setBounds(0, 0, 40,240);
        this.add(graveyard);
        
        graveyard.setVisible(false);
        
        
        ImageIcon branding = new ImageIcon("Brand.png");
        brand.setIcon(branding);
        brand.setBounds(280, 0, 40,240);
        this.add(brand);
        
        brand.setVisible(false);
        
        accept.setBounds(290,0,30,20);
        accept.setBackground(Color.WHITE);
        accept.setForeground(Color.BLACK);
        accept.setBorder(new LineBorder(Color.BLACK, 2));
        accept.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                rules.setVisible(false);
                board.setVisible(true);
                accept.setVisible(false);
                redsetup.setVisible(true);
                acceptII.setVisible(true);
                graveyard.setVisible(true);
                brand.setVisible(true);
                start();
            }
        });
        this.add(accept);
        
        ImageIcon bigrules = new ImageIcon("Rules.png");
        rules.setIcon(bigrules);
        rules.setBounds(0, 0, 320,240);
        this.add(rules);
        
        rules.setVisible(true);
        
        
        
}
public void blueSetUpTime() {
    posY = 144;
    posX = 41;
    for(int i =0; i<40; i++) {
        
        bluefield[i] = new JTextField();
        bluefield[i].setBounds(posX, posY, 24, 24);
        bluefield[i].setBackground(Color.decode("#a67c52"));
        bluefield[i].setForeground(Color.blue);
        bluefield[i].setBorder(new LineBorder(Color.decode("#77593b"), 1));
        bluefield[i].setVisible(true);
        this.add(bluefield[i]);
        if(posX == 257) {
            posX = 41;
            posY += 24;
        }else {
            posX+=24;
        }
    }
    
    bluesetup.setVisible(true);
    acceptIII.setVisible(true);
    
}

public void blueVerification() {
    System.out.println("performing action");
    for(int i =0; i<40; i++) {
        System.out.println("for looping...");
        blueval[i] = bluefield[i].getText();
        if(blueval[i].equals("b") || blueval[i].equals("B")) {
            bluebomb--;
        }else if(blueval[i].equals("f") || blueval[i].equals("F") ){
            blueflag--;
        }else if(blueval[i].equals("1")){
            blueone--;
        }else if(blueval[i].equals("2")){
            bluetwo--;
        }else if(blueval[i].equals("3")){
            bluethree--;
        }else if(blueval[i].equals("4")){
            bluefour--;
        }else if(blueval[i].equals("5")){
            bluefive--;
        }else if(blueval[i].equals("6")){
            bluesix--;
        }else if(blueval[i].equals("7")){
            blueseven--;
        }else if(blueval[i].equals("8")){
            blueeight--;
        }else if(blueval[i].equals("9")){
            bluenine--;
        }else if(blueval[i].equals("10")){
            blueten--;
        }
        if(bluebomb != 0 || blueflag != 0 || blueone != 0 || bluetwo != 0 || bluethree != 0 || bluefour != 0 || bluefive != 0 || bluefive != 0 || bluesix != 0 || blueseven != 0 || blueeight != 0 || bluenine != 0 || blueten != 0 ) {
            blueinvalid.setVisible(true);
            System.out.println("invalid setup");
        }else{
            System.out.println("valid setup, clearing...");
            blueinvalid.setVisible(false);
            acceptIII.setVisible(false);
            bluesetup.setVisible(false);
            for(int ii =0; ii<40; ii++) {
                 bluefield[ii].setVisible(false);
                 
             }
            System.out.println("BEGIN GAME");
            
        }
        
        
}
    System.out.println(Arrays.toString(blueval));
}
    


public void start() {
     startGame = true;
     new Thread(this).start();
 }

public void run() {
    
 for(int i =0; i<40; i++) {
     redfield[i].setVisible(true);
 }

//System.exit(0);
}

}

当我运行它时,红色代码工作正常,但蓝色代码始终是“无效设置”

java arrays string jtextfield
1个回答
0
投票

那么,您的主要问题就在这里......

for(int ii =0; ii<40; ii++) {
    redfield[ii].setVisible(false);
    blueSetUpTime();
}

您调用

blueSetUpTime
40 次,这会向您的屏幕添加 1600 个文本字段...祝您好运,找出实际分配给您的数组的文本字段。

还有许多其他问题。您真的不应该使用

null
布局,花时间学习和使用布局管理器。请记住,您不会局限于单个布局管理器,而是可以根据您的需要使用具有单独布局的复合容器。

当您在红色和蓝色之间切换时,我可能会考虑使用

CardLayout
或简单地重新播种模型,但这取决于您。

我还会考虑将您的用户界面分解为各个职责区域。由于红色和蓝色的设置基本相同,因此它可能是其自己的组件,您可以利用数据模型来分离两者。这是一个通常称为“模型-视图-控制器”的概念。 我会考虑提前初始化您的组件,这样您就不会再次遇到这样的问题。

© www.soinside.com 2019 - 2024. All rights reserved.