gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:21:17 27 4
gpt4 key购买 nike

对编码非常陌生,一点也不是我的强项...最初,我的文本提示会说你需要“输入名称”,但由于某种原因,现在没有其他书写方式了这段代码?

还有人能告诉我如何用 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();

}
}

}

最佳答案

您是否尝试过使用 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);

关于java if语句文本提示不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36412848/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com