Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;// Importing.
- import java.util.List;
- /*
- * FIT FOR PUBLIC CONSUMPTION....
- *
- * displayText(String input)
- * {Displays Text}
- *
- * switchInput()
- * {Switches between a button and a text field}
- *
- * boolean isText
- * //Are we in text mode?(True) or Button mode?(False)
- *
- * TO CONSTRUCT...
- * your_ui = Ui.NewUi(); //Default parameters
- * // or
- * your_ui = new Ui(String window_Title, int xStart, int yStart, int width, int height)
- *
- * TO CLOSE
- * Kill()
- * {Kills the Window, and cleans up resources}
- *
- * setOut(String input)
- * {Sets the text to your input.}
- *
- * addOut(String input)
- * {Adds the input to the existing text. Make sure to \newline if you want!}
- *
- * askQuestion(String input, String[] options)
- * {Display the text: if user input does not fit any of your options, it will loop and ask again!}
- *
- * Visible(boolean isVisible)
- * {Can we see the Window?}
- *
- * askInt(String input)
- * {Display the text: if a number is not returned, it will ask again!}
- */
- public class Ui extends JFrame implements ActionListener
- {
- /*I don't suggest reading this file. If you're interested though,
- * the more interesting and cool code is past the constructor!*/
- JTextArea out = new JTextArea();
- //JLabel lineDown = new JLabel("<html> <br> <br> <br> </html>");
- Button button = new Button("continue");
- public JTextField in = new JTextField(12);
- public String inText;// We need a lot of these. | Do we?
- static boolean pushed;
- public boolean isText;
- Container con;
- JPanel pane;
- public static Ui NewUi()
- {
- return new Ui("Window",400,400,400,400);
- }
- public void Kill()
- {
- this.dispose();
- }
- public Ui(String frameName, int x, int y, int lx, int ly)
- {
- super(frameName); setBounds(x,y,lx,ly);
- isText = true;
- //MAKING OUR WINDOW.
- out.setEditable(false);
- in.addActionListener(this);
- button.addActionListener(this);
- pane = new JPanel();
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- con = this.getContentPane();
- out.setBounds((lx/2)-((lx-25)/2),0,(lx-25),100);
- button.setBounds(100,50,400,350);
- this.getContentPane().getBounds();
- con.add(pane);
- pane.add(out);
- pane.add(in);// Layer our bounds.
- pane.add(button);
- out.setLineWrap(true);
- out.setWrapStyleWord(true);
- setVisible(true);
- in.setVisible(true);
- button.setVisible(false);
- }
- public void Visible(boolean inVis)
- {
- setVisible(inVis);
- }
- public void switchInput()
- {
- if ( isText )
- {
- in.setVisible(false);
- button.setVisible(true);
- isText = false;
- }
- else
- {
- in.setVisible(true);
- button.setVisible(false);
- isText = true;
- }
- }
- public String getInput()
- {
- if (!pushed)//WE HAVEN'T HIT OUR ACTION LISTENER.
- {
- return "_null";
- }
- else//WE HAVE
- {
- String s = inText;
- inText = null;
- pushed = false;
- return s;
- }
- }
- public void displayText(String s)
- {
- if (isText)
- switchInput();
- askQuestion(s,null);
- if (!isText)
- switchInput();
- }
- public String askQuestion(String s, String[] options)//The BACKBONE OF MY ARMY.
- {
- String input;
- System.out.format(s);
- setOut(s);
- Boolean b = false;//This is here because of reasons.
- while (1 == 1)
- {
- input = getInput();//Keep checking if something's updated.
- //bufferRead = new BufferedReader(new InputStreamReader(System.in)); // OLD
- //input = bufferRead.readLine(); // OLD AS BALLS
- if (!input.equals("_null"))
- {
- //input = inText;
- //pushed=false;
- if ( options != null )
- {
- for(String d : options)
- {
- if (input.equals(d))//Decide what to return.
- return input;
- }
- inputError();
- setOut(s);
- }
- else
- return input;
- }
- }
- }
- private void inputError()
- {
- clear();
- boolean isit = false;
- if (isText)
- {
- switchInput();
- isit = true;
- }
- System.out.println("Incorrect Input.");//BZZZZZZZZZ
- displayText("Incorrect Input");
- if (isit)
- switchInput();
- }
- protected void clear()
- {
- System.out.format("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");//Old too. Pre-GUI
- setOut("");
- }
- public void setOut(String s)
- {
- out.setText(s);
- }
- public void addOut(String s)
- {
- out.setText(out.getText()+s);
- }
- public String getOut()
- {
- return out.getText();
- }
- public void actionPerformed(ActionEvent e)
- {
- if (isText)
- {
- inText = in.getText();
- pushed = true;
- in.setText("");
- }
- else
- {
- inText = "";
- pushed = true;
- }
- }
- public int askInt(String s)//The BACKBONE OF MY ARMY.
- {
- String input;
- System.out.format(s);
- setOut(s);
- Boolean b = false;//This is here because of reasons.
- while (1 == 1)
- {
- input = getInput();//Keep checking if something's updated.
- //bufferRead = new BufferedReader(new InputStreamReader(System.in)); // OLD
- //input = bufferRead.readLine(); // OLD AS BALLS
- if (!input.equals("_null"))
- {
- //input = inText;
- //pushed=false;
- try
- {
- return Integer.parseInt(input);
- //return input;
- }
- catch(Exception e)
- {
- inputError();
- setOut(s);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement