Advertisement
Constructor | Description |
---|---|
public JButton() | Creates a button with no text on it.. |
public JButton(String text) | Creates a button with a text on it. |
public JButton(Icon image) | Creates a button with an icon on it. |
public JButton(String text, Icon image) | Creates a button with a text and an icon on it. |
Methods | Description | public void setText(String text) | Sets a String message on the JButton. |
---|---|
public String getText() | Gets a String message of JButton. |
public void setIcon(Icon icon) | Sets an icon or image over the JButton. |
public Icon getIcon() | Gets the icon or image of the JButton. |
void setHorizontalTextPosition(int textPosition) | Sets the button message on the LEFT/RIGHT of its icon or image . |
void setVerticalTextPosition(int textPosition) | Sets the button message on the TOP/BOTTOM of its icon or image. |
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Btn1
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});
}//Closing the main method
}//Closing the class A
class A
{
JFrame jf;
JButton button1, button2, button3;
A()
{
jf= new JFrame();
button1= new JButton();
button2= new JButton("Click here");
button3= new JButton();
button3.setText("Button3");
jf.add(button1);
jf.add(button2);
jf.add(button3);
jf.setLayout(new FlowLayout());
jf.setSize(300,100);
jf.setVisible(true);
}
}
Advertisement
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;
public class Btn4
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});
}//Closing the main method
}//Closing the class A
class A
{
JFrame jf;
JButton button1, button2, button3, button4;
A()
{
jf= new JFrame();
//Setting an image over a JButton and limited its border space, so that image fits perfectly over JButton.
button1= new JButton(new ImageIcon("JButton1.jpg"));
button1.setBorder(new EmptyBorder(2,2,2,2));
//Setting an image over a JButton with and without text.
button2 = new JButton(new ImageIcon("Smiley3.jpg"));
button3 = new JButton("Smiley", new ImageIcon("Smiley3.jpg"));
//Setting a big image over a JButton
button4 = new JButton(new ImageIcon("Big Smiley", "BigSmiley.gif"));
button4.setHorizontalTextPosition(SwingConstants.LEFT);
jf.add(button1);
jf.add(button2);
jf.add(button3);
jf.add(button4);
jf.setLayout(new FlowLayout());
jf.setSize(300,300);
jf.setVisible(true);
}
}
When you run the code, you are presented a window shown in the Figure2 below -:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Btn5
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});
}//Closing the main method
}//Closing the class A
class A implements ActionListener
{
JFrame jf;
JButton button1, button2;
JLabel label;
A()
{
jf= new JFrame();
button1= new JButton("Yes");
button2= new JButton("No");
label = new JLabel();
jf.add(button1);
jf.add(button2);
jf.add(label);
button1.addActionListener(this);
button2.addActionListener(this);
jf.setLayout(new FlowLayout(FlowLayout.CENTER,60,10));
jf.setSize(250,150);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Yes"))
{
label.setText("You've clicked Yes");
jf.add(label);
jf.setVisible(true);
System.out.println("Yes button is clicked");
}
if(ae.getActionCommand().equals("No"))
{
label.setText("You've clicked No");
jf.add(label);
jf.setVisible(true);
System.out.println("No button is clicked");
}
}
}
When you run the code, you are presented a window shown in the Figure3 below -:
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement