π Introduction
In GUI development, functionality alone is not enough β appearance matters too! π―
π A well-designed interface improves:
- User experience π
- Readability π
- Professional look π»
In this blog, we will learn how to customize:
- π¨ Colors (background & text)
- π€ Fonts
- π§± Borders (including border color)
π For both AWT and Swing
π§± PART 1: Customization in AWT
π¨ 1. Colors in AWT
β Complete Runnable Program
import java.awt.*;
public class AWTColorDemo {
public static void main(String[] args) {
Frame frame = new Frame("AWT Colors");
Button btn = new Button("Click Me");
btn.setBackground(Color.BLUE); // background
btn.setForeground(Color.WHITE); // text color
frame.setLayout(new FlowLayout());
frame.add(btn);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
π€ 2. Font in AWT
β Program
import java.awt.*;
public class AWTFontDemo {
public static void main(String[] args) {
Frame frame = new Frame("AWT Font");
Label label = new Label("Styled Text");
label.setFont(new Font("Arial", Font.BOLD, 18));
frame.setLayout(new FlowLayout());
frame.add(label);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
π§± 3. Border in AWT (Including Border Color)
π AWT does NOT have built-in border support like Swing β
π But we can simulate borders using Panel + background color trick β
β Complete Runnable Program
import java.awt.*;
public class AWTBorderDemo {
public static void main(String[] args) {
Frame frame = new Frame("AWT Border");
frame.setLayout(new FlowLayout());
// Outer panel (acts as border)
Panel outer = new Panel();
outer.setBackground(Color.RED); // border color π₯
outer.setLayout(new FlowLayout());
// Inner panel (actual content)
Panel inner = new Panel();
inner.setBackground(Color.WHITE);
Label label = new Label("AWT Border Example");
inner.add(label);
outer.add(inner);
frame.add(outer);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
π Explanation
- Outer panel β acts as border
- Background color β border color π¨
- Inner panel β content
π This is a workaround, not a true border
β οΈ AWT Limitation
π No direct border API β
π Styling is limited
π§± PART 2: Customization in Swing (Powerful π₯)
π¨ 1. Colors in Swing
β Complete Runnable Program
import javax.swing.*;
import java.awt.*;
public class SwingColorDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Colors");
JButton btn = new JButton("Click Me");
btn.setBackground(Color.BLUE);
btn.setForeground(Color.WHITE);
frame.setLayout(new FlowLayout());
frame.add(btn);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
π§± 2. Borders in Swing (Including Border Color)
β Complete Runnable Program
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class SwingBorderDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Border");
JButton btn = new JButton("Styled Button");
// Border with color
Border border = BorderFactory.createLineBorder(Color.RED, 3);
btn.setBorder(border);
frame.setLayout(new FlowLayout());
frame.add(btn);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
π Explanation
createLineBorder(Color, thickness)- Color defines border color π¨
β¨ 3. Advanced Border Types
β Program
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class AdvancedBorderDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Advanced Borders");
JButton btn = new JButton("Fancy Button");
Border border = BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.BLUE, 2),
"My Border"
);
btn.setBorder(border);
frame.setLayout(new FlowLayout());
frame.add(btn);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
π€ 4. Fonts in Swing
β Program
import javax.swing.*;
import java.awt.*;
public class SwingFontDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Font");
JLabel label = new JLabel("Styled Text");
label.setFont(new Font("Verdana", Font.BOLD, 20));
frame.setLayout(new FlowLayout());
frame.add(label);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
βοΈ AWT vs Swing (Styling Perspective)
| Feature | AWT | Swing |
|---|---|---|
| Colors | Basic | Advanced |
| Fonts | Limited | Flexible |
| Borders | Manual workaround β | Direct support β |
| Border Color | Trick-based | Direct property π¨ |
β οΈ Common Mistakes
β Expecting AWT border like Swing
π AWT does not support it directly
β Forgetting BorderFactory import
import javax.swing.border.*;
β Button color not visible
π Depends on system Look & Feel
β Mixing AWT & Swing
π Causes UI inconsistency
π€― Common Confusions
π€ Why AWT borders are difficult?
π Because AWT relies on OS components
π€ Why Swing borders are easy?
π Fully Java-controlled rendering
πΌ Interview Questions
β How to set border color in Swing?
π BorderFactory.createLineBorder(Color.RED)
β Does AWT support borders?
π Not directly β requires workaround
β Which is better for styling?
π Swing
π― Best Practices
βοΈ Use Swing for modern UI
βοΈ Use consistent color scheme
βοΈ Keep UI clean
βοΈ Avoid over-styling
π Conclusion
Now you can make your GUI:
π Functional βοΈ
π Interactive βοΈ
π Beautiful π¨β¨
π‘ Final Thought
π Good UI = Good impression π―
π₯ One-Line Summary
π βSwing provides direct and powerful styling features, while AWT has limited customization options.β
π» Happy Coding! π
Next step: Event Delegation Model (deep dive) π₯