Set Text Color for JTextField – Java Swing Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import javax.swing.JTextField; import javax.swing.JFrame; import java.awt.Color; import java.awt.FlowLayout; public class SetJTextFieldTextColor { public static void main(String[]args) { //Create text field using JTextField JTextField textField = new JTextField(10); //Create JFrame with title (Set JTextField text color) JFrame frame = new JFrame("Set JTextField text color"); //Set JFrame layout to FlowLayout frame.setLayout(new FlowLayout()); //Set color base on RGB //You can get RGB value for your color at "Color picker" at above //R=255 //G=0 //B=0 Color color = new Color(255,0,0); //Set JTextField text color to color that you choose textField.setForeground(color); //Add JTextField into JFrame frame.add(textField); //Set default close operation for JFrame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set JFrame size frame.setSize(500,300); //Make JFrame visible frame.setVisible(true); } } |