java if语句文本提示不起作用

时间:2016-04-04 21:05:32

标签: java swing user-interface if-statement

编码很新,而不是我最轻微的优势之一......最初我的文字提示会说你需要“输入一个名字”,但由于某种原因现在它不存在另一种写作方式这段代码?

也有人能够指出我如何使用JTextArea执行JPanel的方向当按下按钮时,文本区域将显示“name”是否饿了?我已经看过各种各样的东西但是无法绕过它。 谢谢

    import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GuiEnvironment extends JFrame implements ActionListener {
private static final long serialVersionUID = 8573501273671847629L;
private JPanel firstPanel, labelPanel, wholeGUI, buttonPanel, textPanel, frog, fly;
private JButton newResetButton, newHungryButton, newPetButton;
private JTextField newName;
private JLabel state;
private Frog guiFrog;
private Fly guiFly;




public JPanel newEnvironment() {

    wholeGUI = new JPanel();
    wholeGUI.setLayout(null);


    // area for pet to chase upon 
    firstPanel = new JPanel();
    firstPanel.setLocation(20, 20);
    firstPanel.setBackground(Color.WHITE);
    firstPanel.setSize(550, 550);
    firstPanel.setLayout(null);
    wholeGUI.add(firstPanel);

    //panel for buttons
    buttonPanel = new JPanel();
    GridLayout newLayout = new GridLayout();
    buttonPanel.setLayout(newLayout);
    buttonPanel.setLocation(50, 575);
    buttonPanel.setSize(520, 50);
    wholeGUI.add(buttonPanel);

    //name button
    newName = new JFormattedTextField("");
    newName.setLocation(140,0);
    newName.setSize(50,30);
    buttonPanel.add(newName);

    //create pet button
    newPetButton = new JButton("Create Pet");
    newPetButton.setLocation (150, 0);
    newPetButton.setSize(50,30);
    newPetButton.addActionListener(this);
    buttonPanel.add(newPetButton);

    // clear pet area
    newResetButton = new JButton("Reset");
    newResetButton.setLocation(420, 0);
    newResetButton.setSize(50, 30);
    newResetButton.addActionListener(this);
    buttonPanel.add(newResetButton);

    // make pets hungry 
    newHungryButton = new JButton("Make Hungry");
    newHungryButton.setLocation(280, 0);
    newHungryButton.setSize(50, 30);
    newHungryButton.addActionListener(this);
    buttonPanel.add(newHungryButton);

    //input panel
    textPanel = new JPanel();
    textPanel.setLayout(null);
    textPanel.setLocation(0, 550);
    textPanel.setSize(120, 120);
    firstPanel.add(textPanel);


    wholeGUI.setOpaque(true);

    return wholeGUI;
    }




private static void createAndShowGUI() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("[=***] The Hungry Cyber Pet. [***=]");

    GuiEnvironment test = new GuiEnvironment();
    frame.setContentPane(test.newEnvironment());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setSize(800,800);
    frame.setVisible(true);
}




public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}


public void actionPerformed(ActionEvent e){

    if(e.getSource() == newPetButton && newName.getText().length() != 0){
        String name = newName.getText();
        guiFrog = new Frog(name);
        guiFrog.getDisplayArea(frog);
        guiFrog.getTextArea();
        firstPanel.add(guiFrog);
        guiFrog.start();
        guiFly = new Fly();
        guiFly.getDisplayArea(fly);
        firstPanel.add(guiFly);
        guiFrog.insertFly(guiFly);
        guiFly.start();
        firstPanel.revalidate();
        firstPanel.repaint();
    }

    if (e.getSource() == newPetButton && newName.getText().length()==0)
     {
        state.setText("You must enter a name");      
     }
    else if(e.getSource() == newResetButton){
        firstPanel.removeAll();
        firstPanel.revalidate();
        firstPanel.repaint();
    }

    else if(e.getSource() == newHungryButton){
        guiFrog.setHungry();

    }
}

}

2 个答案:

答案 0 :(得分:3)

您是否尝试过使用JOptionPane作为提示,然后存储值?

if (e.getSource() == newPetButton && newName.getText().length()==0)
 {
    String name =  JOptionPane.showInputDialog(state, "You must enter a name");
    state.setText(name);      
 }

我对Swing做的不多,但我之前用过它来获取用户的输入。

至于你的另一个问题,当按下JButton时,下面的代码可能适用于在JTextArea中显示文本:

    // creates the JPanel that will hold JButton and JTextArea
    JPanel textAndButtonPanel = new JPanel();

    // the JButton the user will press to see if "name" is hungry
    JButton hungryButton = new JButton("Am I Hungry?");

    // the JTextArea that will initially be blank, but will display
    // "name" is hungry when the JButton is pressed
    JTextArea textArea = new JTextArea(10, 10);

    // sets up the event-handling for the JButton. When the button
    // is pressed, the JTextArea will display "name" is hungry.
    hungryButton.addActionListener(new ActionListener() {   
        @Override
        public void actionPerformed(ActionEvent e) {
            textArea.setText(String.format("%s is hungry", name));
        }
    });

    // adds the JButton and JTextArea to the JPanel
    textAndButtonPanel.add(hungryButton);
    textAndButtonPanel.add(textArea);

答案 1 :(得分:3)

你的一个主要问题是你的代码在这里抛出一个NullPointerException:

state.setText("You must enter a name");

你在问题​​中没有告诉我们。

这是由于您没有初始化状态JLabel变量引起的。我猜它曾在某处初始化(即state = new JLabel();),但出于某种原因你删除了这段代码。解决方案是初始化此变量,...为其分配一个新的JLabel,然后将其添加到GUI。此外,如果您有疑问并且出现错误或例外情况,请在问题中包含所有相关信息。

其他无关的问题,请勿执行此操作:wholeGUI.setLayout(null);。虽然null布局和setBounds()似乎是Swing新手,比如创建复杂GUI的最简单和最好的方法,但是你创建的Swing GUI越多,在使用它们时会遇到更严重的困难。当GUI调整大小时,它们不会调整组件大小,它们是增强或维护的皇室女巫,当它们放置在滚动窗格中时它们会完全失败,当它们在所有平台上观看时或者与原始平台不同的屏幕分辨率时它们看起来很糟糕。了解如何使用布局管理器,然后完全使用它们。