Loading

Input fields in Java Swing

Including a JButton:

 

// Name of this file should be simplejbutton.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class simplejbutton extends JApplet
{
JButton B = new JButton(“Click”);

public void init()
{
Container cp = getContentPane();
cp.setLayout(null);

cp.add(B);
B.setBounds(10,10,100,20);
B.setActionCommand(“B”);
B.addActionListener(mylistener);
}
// ActionListener for Button
private ActionListener mylistener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == “B”)
B.setText(“Clicked”);
}
};// End of ActionListener
} // End of the Class

Including a JLabel:

 

// This file should have name simplejlabel.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class simplejlabel extends JApplet
{
JLabel L = new JLabel(“Label”);
JButton B = new JButton(“Click”);

public void init()
{
Container cp = getContentPane();
cp.setLayout(null);

cp.add(L);
L.setBounds(10,10,100,20);

cp.add(B);
B.setBounds(120,10,100,20);
B.setActionCommand(“B”);
B.addActionListener(mylistener);
}
// ActionListener for Button
private ActionListener mylistener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == “B”)
L.setText(“Clicked”);
}
};// End of ActionListener
} // End of the Class

Including a JTextField

// This file should have name simplejtext.java
// Type something in Text Box and press Enter
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class simplejtext extends JApplet
{
JTextField T = new JTextField(“”);
JLabel L = new JLabel(“Label”);

public void init()
{
Container cp = getContentPane();
cp.setLayout(null);

cp.add(T);
T.setBounds(10,10,100,20);
T.setActionCommand(“T”);
T.addActionListener(mylistener);

cp.add(L);
L.setBounds(120,10,100,20);

}
// ActionListener for Button
private ActionListener mylistener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()==”T”)
L.setText(T.getText());
}
};// End of ActionListener
} // End of the Class

Including a JCheckBox:

// This file should have name simplejcheckbox.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class simplejcheckbox extends JApplet {

JCheckBox C = new JCheckBox(“Click”);
JLabel L = new JLabel(“Label”);

public void init()
{
Container cp = getContentPane();
cp.setLayout(null);

cp.add(C);
C.setBounds(10,10,100,20);
C.setActionCommand(“C”);
C.addActionListener(mylistener);

cp.add(L);
L.setBounds(120,10,100,20);

}
// ActionListener for Button
private ActionListener mylistener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand()==”C”)
{
if (C.isSelected() == true)
L.setText(“True”);
else
L.setText(“False”);
}
}
};// End of ActionListener
} // End of the Class

The above code is highlighted using Google Code Prettify.




blog comments powered by Disqus

There's 0 Comment So Far

Share your thoughts, leave a comment!