Skip to content

Instantly share code, notes, and snippets.

@theArjun
Created February 21, 2019 12:16
Show Gist options
  • Select an option

  • Save theArjun/10ee7bf979e3b1adc68de97d8c5b0c8e to your computer and use it in GitHub Desktop.

Select an option

Save theArjun/10ee7bf979e3b1adc68de97d8c5b0c8e to your computer and use it in GitHub Desktop.
Change Background of JTextField using Swing
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