对编码来说非常陌生,一点也不是我的强项。。。最初,我的文本提示会说你需要"输入一个名字",但由于某种原因,现在没有了。有其他方法写这个代码吗?
还有谁能告诉我如何用JTextArea创建JPanel吗?当按下按钮时,文本区域会显示"name"is饥饿?我看了看各种各样的东西,但还是想不通。感谢
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);
您的主要问题之一是代码在此处抛出NullPointerException:
state.setText("You must enter a name");
你的问题没有告诉我们。
这是由于您没有初始化状态JLabel变量造成的。我猜它曾经在某个地方初始化过(即state = new JLabel();
),但出于某种原因,您删除了此代码。解决方案是初始化这个变量。。。为它分配一个新的JLabel,然后将它添加到GUI中。此外,在将来,当您有问题,并且是关于错误或异常时,请在您的问题中包含所有相关信息。
其他无关的问题,不要这样做:wholeGUI.setLayout(null);
。虽然在Swing新手看来,空布局和setBounds()
可能是创建复杂GUI的最简单、最好的方法,但创建的Swing GUI越多,在使用它们时就会遇到更严重的困难。当GUI调整大小时,它们不会调整组件的大小,它们是一个需要增强或维护的皇家女巫,当放在滚动窗格中时,它们会完全失败,当在所有平台上查看时,或者当屏幕分辨率与原始分辨率不同时,它们看起来非常糟糕。学习如何使用布局管理器,然后充分使用它们。