How to Put an HTML code in a Button in Java
Details: Put an HTML code
HTML is a specification language used to create web pages. In web development, HTML is used to format and create user interfaces (UI). HTML buttons are used to perform specific actions when clicked by the user. To create a button in HTML, you must first code the button’s name, which is typically an acronym that represents the action to perform when the button is clicked. Next, you code the button’s text and its state—active or inactive—and its appearance—bold or not—using HTML tags. To associate an action with a button, you code how the system responds when that button is clicked.
Guide: Put an HTML code
In this post, we will learn how to put an HTML code in a button in Java. To display text in the JButton, The HTML tags will be insert. In other to achieve this, the following steps can be adhered to:
1 – Create a Java program with the file name, htmlInButton.java in JCreator or NetBeans.
2 – Proceed to import javax.swing.* package library; used to access the JButton and JFrame class.
import javax.swing.*; //used to access the JButton, and JFrame class.
3 – Now, initiate the variable in your main;
– variable frame for JFrame
– variable button for JButton.
JFrame frame = new JFrame("HTML Code in a Button");
JButton button = new JButton();
4 – Use the setText method of the button to make an HTML code inside the button.
– The HTML code can be identified by the setText method.
– It gives an advantage of calling a text with a font, alignment, colour, and font style without utilizing the classes of awt library like colour class, font class, alignment, and the styles of the font. Use the code as given below:
button.setText ("This text has an HTML Code
Sourcecodester");
html – an html tag
font face=’Tahoma’” + ” color=blue – A font tag with a textual style of Tahoma and text style colour of blue
b– Start the bold tag
This text has an HTML Code – the text inside the html with a specific font of Tahoma and blue color of font
font face=’courier new’” + ” color=red – a font tag with a font style of courier new and font color of red
center – a center alignment tag
Sourcecodester – the text inside the html with a specific font of courier new and red color of font
Use the add method to add the button to the frame.
frame.getContentPane().add(button);
5 – To conclude, et the size, visibility to true, and the close operation of the frame. Utilize the code underneath:
frame.setSize(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: Put an HTML code
Complete Source Code: How to Put an HTML code in a Button in Java
import javax.swing.*; //used to access the JButton, and JFrame class.
public class htmlInButton{
public static void main(String[] a) {
JFrame frame = new JFrame("HTML Code in a Button");
JButton button = new JButton();
button.setText ("This text has an HTML Code
Sourcecodester");
frame.getContentPane().add(button);
frame.setSize(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}