Results 1 to 2 of 2
  1. #1
    Gamedisolver is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Displaying a JTextField?

    I am trying to make the JTextField turn its input to a string that i can use in other classes and display it in System.out.println();

    code:

    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Rectangle;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    public class Level1Expeirement1 extends JFrame {

    private static String title = "Expeirement1";
    private static int width = 700;
    private static int height = 700;

    //buttons
    private JPanel window = new JPanel();
    private JButton cancel, ok, expirement3;
    private Rectangle rcancel, rok, rexpirement3, ritem1;
    private String hypothisis;
    private JTextField item1 = new JTextField(hypothisis);


    public static Font font = new Font("Comic Sans MS", Font.BOLD, 12);


    public void Level1Expeirement1() {

    setVisible(true);
    setSize(width, height);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(window);
    setLocationRelativeTo(null);
    setTitle(title);
    setBackground(Color.GREEN);


    window.setLayout(null);

    Drawbutton();


    }

    private void Drawbutton() {

    //draws buttons
    //level 1 button
    ok = new JButton("ok");
    // location size
    // 20 50 80 100
    rok = new Rectangle(60, 50, 180, 40);
    ok.setBounds(rok);
    window.add(ok);
    //use to add graphics
    //singleplayer.setIcon(new ImageIcon("res/singleplayer.png"));

    cancel = new JButton("cancel");
    // location size
    // 20 50 80 100
    rcancel = new Rectangle(60, 100, 180, 40);
    cancel.setBounds(rcancel);
    window.add(cancel);

    item1 = new JTextField();
    // location size
    // 20 50 80 100
    ritem1 = new Rectangle(60, 150, 180, 40);
    item1.setBounds(ritem1);
    window.add(item1);

    //listener

    item1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    System.out.println(hypothisis);


    }
    });
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,928
    Rep Power
    16

    Default Re: Displaying a JTextField?

    Guide For New Members
    BB Code List - Java Programming Forum

    Go through the tutorial linked from the API of JTextField.

    Thread moved from New to Java.

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Similar Threads

  1. Replies: 3
    Last Post: 11-14-2011, 05:13 AM
  2. displaying a formatted output on a jTextField
    By techbossmb in forum AWT / Swing
    Replies: 1
    Last Post: 09-24-2009, 04:56 PM
  3. Replies: 8
    Last Post: 08-13-2009, 04:23 AM
  4. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  5. Replies: 5
    Last Post: 01-17-2008, 09:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •