Very new to coding and not one of my strengths in the slightest... Initially my text prompt would work saying that you need to "enter a name" but for some reason now it doesn't is there another way of writing this code?
Also would anyone be able to point me in the direction of how to do a JPanel with a JTextArea that When a button is pressed, the text area will display "name" is hungry ? I have had a look around various things but cant get my head around it. Thanks
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();
}
}
}
Aucun commentaire:
Enregistrer un commentaire