Advertisement
Guest User

Have fun reading your old code. :^)

a guest
Nov 25th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.66 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;// Importing.
  4. import java.util.List;
  5.  
  6. /*
  7.  * FIT FOR PUBLIC CONSUMPTION....
  8.  *
  9.  *     displayText(String input)
  10.  *       {Displays Text}
  11.  *
  12.  *     switchInput()
  13.  *       {Switches between a button and a text field}
  14.  *
  15.  *     boolean isText
  16.  *       //Are we in text mode?(True) or Button mode?(False)
  17.  *
  18.  *     TO CONSTRUCT...
  19.  *     your_ui = Ui.NewUi(); //Default parameters
  20.  *       // or
  21.  *     your_ui = new Ui(String window_Title, int xStart, int yStart, int width, int height)
  22.  *
  23.  *     TO CLOSE
  24.  *     Kill()
  25.  *       {Kills the Window, and cleans up resources}
  26.  *
  27.  *     setOut(String input)
  28.  *       {Sets the text to your input.}
  29.  *
  30.  *     addOut(String input)
  31.  *       {Adds the input to the existing text. Make sure to \newline if you want!}
  32.  *
  33.  *     askQuestion(String input, String[] options)
  34.  *       {Display the text: if user input does not fit any of your options, it will loop and ask again!}
  35.  *
  36.  *     Visible(boolean isVisible)
  37.  *       {Can we see the Window?}
  38.  *
  39.  *     askInt(String input)
  40.  *       {Display the text: if a number is not returned, it will ask again!}
  41.  */
  42.  
  43. public class Ui extends JFrame implements ActionListener
  44. {  
  45.    /*I don't suggest reading this file. If you're interested though,
  46.     * the more interesting and cool code is past the constructor!*/
  47.  
  48.  JTextArea out = new JTextArea();
  49.  //JLabel lineDown = new JLabel("<html> <br> <br> <br> </html>");
  50.  Button button = new Button("continue");
  51.  public JTextField in = new JTextField(12);
  52.  public String inText;// We need a lot of these. | Do we?
  53.  static boolean pushed;
  54.  public boolean isText;
  55.  
  56.  Container con;
  57.  JPanel pane;
  58.  
  59.  public static Ui NewUi()
  60.  {
  61.   return new Ui("Window",400,400,400,400);
  62.  }
  63.  
  64.  public void Kill()
  65.  {
  66.    this.dispose();
  67.  }
  68.  
  69.  public Ui(String frameName, int x, int y, int lx, int ly)
  70.  {  
  71.   super(frameName); setBounds(x,y,lx,ly);
  72.   isText = true;
  73.   //MAKING OUR WINDOW.
  74.   out.setEditable(false);
  75.   in.addActionListener(this);
  76.   button.addActionListener(this);
  77.  
  78.   pane = new JPanel();
  79.  
  80.   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  81.   con = this.getContentPane();
  82.   out.setBounds((lx/2)-((lx-25)/2),0,(lx-25),100);
  83.   button.setBounds(100,50,400,350);
  84.   this.getContentPane().getBounds();
  85.  
  86.  
  87.   con.add(pane);
  88.   pane.add(out);
  89.   pane.add(in);// Layer our bounds.
  90.   pane.add(button);
  91.   out.setLineWrap(true);
  92.   out.setWrapStyleWord(true);
  93.  
  94.   setVisible(true);
  95.  
  96.   in.setVisible(true);
  97.   button.setVisible(false);
  98.  }
  99.  
  100.  public void Visible(boolean inVis)
  101.  {
  102.    setVisible(inVis);
  103.  }
  104.  
  105.  public void switchInput()
  106.  {
  107.   if ( isText )
  108.   {
  109.    in.setVisible(false);
  110.    button.setVisible(true);
  111.    isText = false;
  112.   }
  113.   else
  114.   {
  115.    in.setVisible(true);
  116.    button.setVisible(false);
  117.    isText = true;
  118.   }
  119.  }
  120.  
  121.  public String getInput()
  122.  {
  123.    if (!pushed)//WE HAVEN'T HIT OUR ACTION LISTENER.
  124.    {
  125.      return "_null";
  126.    }
  127.    else//WE HAVE
  128.    {
  129.      String s = inText;
  130.      inText = null;
  131.      pushed = false;
  132.      return s;
  133.    }
  134.  }
  135.  
  136.    public void displayText(String s)
  137.    {
  138.      if (isText)
  139.        switchInput();
  140.      askQuestion(s,null);
  141.      if (!isText)
  142.        switchInput();
  143.    }
  144.  
  145.    public String askQuestion(String s, String[] options)//The BACKBONE OF MY ARMY.
  146.   {
  147.     String input;
  148.     System.out.format(s);
  149.     setOut(s);
  150.     Boolean b = false;//This is here because of reasons.
  151.     while (1 == 1)
  152.     {
  153.       input = getInput();//Keep checking if something's updated.
  154.       //bufferRead = new BufferedReader(new InputStreamReader(System.in)); // OLD
  155.       //input = bufferRead.readLine(); // OLD AS BALLS
  156.       if (!input.equals("_null"))
  157.       {
  158.         //input = inText;
  159.         //pushed=false;
  160.         if ( options != null )
  161.         {      
  162.           for(String d : options)
  163.           {
  164.             if (input.equals(d))//Decide what to return.
  165.               return input;
  166.           }
  167.           inputError();
  168.           setOut(s);
  169.         }
  170.         else
  171.           return input;
  172.       }
  173.     }
  174.   }
  175.    
  176.      private void inputError()
  177.   {
  178.    clear();
  179.    boolean isit = false;
  180.    if (isText)
  181.    {
  182.      switchInput();
  183.      isit = true;
  184.    }
  185.    System.out.println("Incorrect Input.");//BZZZZZZZZZ
  186.    displayText("Incorrect Input");
  187.    if (isit)
  188.      switchInput();
  189.   }
  190.        protected void clear()
  191.   {
  192.    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
  193.    setOut("");
  194.   }
  195.  
  196.  public void setOut(String s)
  197.  {
  198.   out.setText(s);
  199.  }
  200.  
  201.  public void addOut(String s)
  202.  {
  203.    out.setText(out.getText()+s);
  204.  }
  205.  
  206.  public String getOut()
  207.  {
  208.    return out.getText();
  209.  }
  210.  
  211.  public void actionPerformed(ActionEvent e)
  212.  {
  213.    if (isText)
  214.    {
  215.     inText = in.getText();
  216.     pushed = true;
  217.     in.setText("");
  218.    }
  219.    else
  220.    {
  221.     inText = "";
  222.     pushed = true;
  223.    }
  224.      
  225. }
  226.     public int askInt(String s)//The BACKBONE OF MY ARMY.
  227.   {
  228.     String input;
  229.     System.out.format(s);
  230.     setOut(s);
  231.     Boolean b = false;//This is here because of reasons.
  232.     while (1 == 1)
  233.     {
  234.       input = getInput();//Keep checking if something's updated.
  235.       //bufferRead = new BufferedReader(new InputStreamReader(System.in)); // OLD
  236.       //input = bufferRead.readLine(); // OLD AS BALLS
  237.       if (!input.equals("_null"))
  238.       {
  239.         //input = inText;
  240.         //pushed=false;
  241.             try
  242.             {
  243.               return Integer.parseInt(input);
  244.               //return input;
  245.              }
  246.             catch(Exception e)
  247.             {
  248.           inputError();
  249.           setOut(s);
  250.             }
  251.         }
  252.       }
  253.     }
  254.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement