Results 1 to 3 of 3
  1. #1
    Maxly is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default Creating an Applet program using JTextFields, JLabel, Jbutton.

    Ok, here is another newbie in java. I want to create an Applet program. When the button “Show” is clicked, the information typed in the two JTextFields (if any), will be displayed on the JLabel. On the other hand, when the button “Clear” is clicked, the information typed in the two JTextField (if any), will be cleared.

    the complicated code(for newbie me) is right here:

    JLabel lblName = new JLabel("Name:");

    JLabel lblAge = new JLabel("Age:");

    textField = new JTextField();
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setColumns(10);

    JButton btnShow = new JButton("Show");
    btnShow.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    textField.setText("Max");
    textField_1.setText("25");
    }
    });

    JButton btnClear = new JButton("Clear");
    btnClear.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    textField.setText("");
    textField_1.setText("");
    }


    Here my whole code using Eclipse:


    package applet.oum;

    import java.awt.BorderLayout;
    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.border.TitledBorder;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.LayoutStyle.ComponentPlacement;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Toolkit;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.ImageIcon;

    public class Applet extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    Applet frame = new Applet();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    /**
    * Create the frame.
    */
    public Applet() {
    setIconImage(Toolkit.getDefaultToolkit().getImage( Applet.class.getResource("/buttons/img/KK.png")));
    setTitle("Applet for Me");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    JMenuItem mntmExit = new JMenuItem("Exit");
    mntmExit.setIcon(new ImageIcon(Applet.class.getResource("/buttons/img/Snap4-16.png")));
    mntmExit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    dispose();
    }
    });
    mnFile.add(mntmExit);

    JMenu mnAbout = new JMenu("About");
    menuBar.add(mnAbout);

    JMenuItem mntmAboutThis = new JMenuItem("About This");
    mntmAboutThis.setIcon(new ImageIcon(Applet.class.getResource("/buttons/img/Snap6-16.png")));
    mnAbout.add(mntmAboutThis);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    JPanel panelApplet = new JPanel();
    panelApplet.setBorder(new TitledBorder(null, "Applet", TitledBorder.LEADING, TitledBorder.TOP, null, null));
    GroupLayout gl_contentPane = new GroupLayout(contentPane);
    gl_contentPane.setHorizontalGroup(
    gl_contentPane.createParallelGroup(Alignment.LEADI NG)
    .addComponent(panelApplet, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
    );
    gl_contentPane.setVerticalGroup(
    gl_contentPane.createParallelGroup(Alignment.LEADI NG)
    .addComponent(panelApplet, GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
    );

    JLabel lblName = new JLabel("Name:");

    JLabel lblAge = new JLabel("Age:");

    textField = new JTextField();
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setColumns(10);

    JButton btnShow = new JButton("Show");
    btnShow.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    textField.setText("Max");
    textField_1.setText("25");
    }
    });

    JButton btnClear = new JButton("Clear");
    btnClear.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    textField.setText("");
    textField_1.setText("");
    }
    });
    GroupLayout gl_panelApplet = new GroupLayout(panelApplet);
    gl_panelApplet.setHorizontalGroup(
    gl_panelApplet.createParallelGroup(Alignment.TRAIL ING)
    .addGroup(Alignment.LEADING, gl_panelApplet.createSequentialGroup()
    .addGap(74)
    .addGroup(gl_panelApplet.createParallelGroup(Align ment.TRAILING)
    .addComponent(lblName)
    .addComponent(lblAge))
    .addGap(18)
    .addGroup(gl_panelApplet.createParallelGroup(Align ment.LEADING, false)
    .addComponent(textField_1)
    .addComponent(textField, GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE))
    .addGap(69))
    .addGroup(gl_panelApplet.createSequentialGroup()
    .addContainerGap(112, Short.MAX_VALUE)
    .addComponent(btnShow)
    .addGap(62)
    .addComponent(btnClear)
    .addGap(122))
    );
    gl_panelApplet.setVerticalGroup(
    gl_panelApplet.createParallelGroup(Alignment.LEADI NG)
    .addGroup(gl_panelApplet.createSequentialGroup()
    .addGap(19)
    .addGroup(gl_panelApplet.createParallelGroup(Align ment.BASELINE)
    .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    .addComponent(lblName))
    .addGap(18)
    .addGroup(gl_panelApplet.createParallelGroup(Align ment.BASELINE)
    .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    .addComponent(lblAge))
    .addPreferredGap(ComponentPlacement.RELATED, 91, Short.MAX_VALUE)
    .addGroup(gl_panelApplet.createParallelGroup(Align ment.BASELINE)
    .addComponent(btnClear)
    .addComponent(btnShow))
    .addGap(38))
    );
    panelApplet.setLayout(gl_panelApplet);
    contentPane.setLayout(gl_contentPane);
    }
    }

    Creating an Applet program using JTextFields, JLabel, Jbutton.-snap1.jpg

    any help would b appreciate.. TQ..

    There is no error about this code, it just that i want it to be like, when the button “Show” is clicked, the information typed in the two JTextFields (if any), will be displayed on the JLabel. On the other hand, when the button “Clear” is clicked, the information typed in the two JTextField (if any), will be cleared. The problem is, what ever names i enter will output name: Max, and age: 25. I want it to display different names and ages.
    Last edited by Maxly; 03-10-2012 at 02:29 PM. Reason: Unclear question.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Creating an Applet program using JTextFields, JLabel, Jbutton.

    What is your question?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,243
    Rep Power
    19

    Default Re: Creating an Applet program using JTextFields, JLabel, Jbutton.

    Don't double post and don't use the browser's back button to edit your posts, as that creates multiple submissions. The other thread started 5 minutes before this one has been removed.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 12-16-2013, 05:35 PM
  2. Help Creating a GUI applet window that compiles my program
    By ReclaimerGold in forum New To Java
    Replies: 3
    Last Post: 04-29-2011, 12:27 AM
  3. Jlabel,Jbutton,If, Trying to connect them all??
    By 2scoopdelux in forum New To Java
    Replies: 5
    Last Post: 12-11-2009, 06:15 PM
  4. Replies: 2
    Last Post: 04-19-2009, 05:39 AM

Posting Permissions

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