Created
February 21, 2019 12:16
-
-
Save theArjun/10ee7bf979e3b1adc68de97d8c5b0c8e to your computer and use it in GitHub Desktop.
Change Background of JTextField using Swing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.*; | |
| import javax.swing.*; | |
| import java.awt.event.*; | |
| import javax.swing.event.*; | |
| public class HaveFun extends JFrame{ | |
| JButton button; | |
| JTextField text; | |
| public HaveFun(String title) { | |
| super(title); | |
| button = new JButton("Have Fun"); | |
| text = new JTextField("Enter text here", 30); | |
| setSize(400,400); | |
| setVisible(true); | |
| setLayout(new FlowLayout()); | |
| add(text); | |
| add(button); | |
| button.addActionListener(new ActionListener(){ | |
| public void actionPerformed(ActionEvent ae){ | |
| text.setText(text.getText().toUpperCase()); | |
| text.setBackground(Color.RED); | |
| text.setForeground(Color.WHITE); | |
| } | |
| }); | |
| } | |
| public static void main(String[] args) { | |
| new HaveFun("Have Fun "); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment