What is wrong with my Java application?
-
I made a simple game in Java which is text-based. The whole thing happens in a frame and I show text on a JTextArea. According to the user's input from a JTextField, the text should change. I have several methods for this. The problem is that the code compiles successfully but when I run my program there is something wrong. I can input something into the JTextField and get a different method run making different text appear in the JTextArea but when I try to do it twice, it goes back to the first text. Here's the code: when running the program, try this input: go out, go house, go my room... import java.io.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JFrame; import java.awt.GridBagLayout; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.geom.Rectangle2D; import java.awt.BorderLayout; import java.awt.Dimension; class game extends JFrame{ static JFrame g = new JFrame(); static JTextArea jta = new JTextArea(6, 2); static JPanel jp = new JPanel(); static JTextField jtf = new JTextField(); public game() { addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { setShape(new Rectangle2D.Double(0, 0, getWidth(), getHeight())); } }); setUndecorated(true); setSize(300, 115); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_... add(jp); jp.add(jta); jta.setPreferredSize(new Dimension(20, 20)); jta.setEditable(false); add(jta, BorderLayout.NORTH); add(jtf, BorderLayout.SOUTH); } public static void main(String[] args)throws IOException{ second(); } public static void second()throws IOException{ game g = new game(); g.setVisible(true); FileInputStream fstream = new FileInputStream("do_not_delete_saves.txt... DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String stLine = br.readLine(); if(stLine.equals("1")){ myroom(); }else if(stLine.equals("2")){ out(); }else if(stLine.equals("3")){ kitchen(); }else if(stLine.equals("4")){ toilet(); }else if(stLine.equals("5")){ livingroom(); }else if(stLine.equals("6")){ house(); } } public static void myroom()throws IOException{ jta.setText(""); jtf.setText(""); Writer output = null; File file = new File("do_not_delete_saves.txt"); output = new BufferedWriter(new FileWriter(file)); output.write("1"); output.close(); jta.append("YOUR ROOM:\n"); jta.append("\n"); jta.append("You are in your room. You could sleep if you wanted to."); jtf.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ String input = jtf.getText(); if(input.equals("go out")){ out(); }else if(input.equals("go kitchen")){ kitchen(); }else if(input.equals("go toilet")){ toilet(); }else if(input.equals("go living room")){ livingroom(); }else{ myroom(); } }catch(Exception f){} } }); } public static void out()throws IOException{ jta.setText(""); jtf.setText(""); Writer output = null; File file = new File("do_not_delete_saves.txt"); output = new BufferedWriter(new FileWriter(file)); output.write("2"); output.close(); jta.append("OUTSIDE:\n"); jta.append("\n"); jta.append("You go outside to see your road. Your neighbourhood\n"); jta.append("is big. There are cars passing by."); jtf.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent r){ try{ String input = jtf.getText(); if(input.equals("go house")){ house(); }else{ out();
-
Answer:
You have to learn java properly there are many weird things going on here.. I wouldn't make the methods public static, I would make the class public and I wouldn't make the class start with a lowercase letter. I would help, but you didn't post all of the code (word limit?). Maybe use a link?
Ivan at Yahoo! Answers Visit the source
Other answers
You have to learn java properly there are many weird things going on here.. I wouldn't make the methods public static, I would make the class public and I wouldn't make the class start with a lowercase letter. I would help, but you didn't post all of the code (word limit?). Maybe use a link?
Jamie
Related Q & A:
- What's wrong with this PHP Twitter API POST?Best solution by Stack Overflow
- What is wrong with this Laravel 5 response?Best solution by Stack Overflow
- What's wrong with my yahoo 360 page stat counter?Best solution by answers.yahoo.com
- What is wrong with my only tv?Best solution by Yahoo! Answers
- What is wrong with my itunes???Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.