- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we set the background color to a JPanel in Java?
A JPanel is a container and it is an invisible component in Java. The FlowLayout is a default layout for a JPanel. We can add most of the components like buttons, text fields, labels, table, list, tree and etc. to a JPanel. We can set a background color to JPanel by using the setBackground() method.
Example
import java.awt.* import javax.swing.*; public class JPanelBackgroundColorTest extends JFrame { private JPanel panel; public JPanelBackgroundColorTest() { setTitle("JPanelBackgroundColor Test"); panel = new JPanel(); panel.add(new JLabel("Welcome to Tutorials Point")); panel.setBackground(Color.green); add(panel, BorderLayout.CENTER); setSize(375, 250); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new JPanelBackgroundColorTest(); } }
Output
- Related Articles
- How can we set a background color to JSplitPane in Java?
- How can we set the foreground and background color to JComboBox items in Java?
- How can we set the background/foreground color for individual column of a JTable in Java?
- How can we implement a scrollable JPanel in Java?
- How can I set a table in a JPanel in Java?
- How can we change the background and foreground color of a JTooltip in Java?
- How to set default background color for JTextPane in Java?
- How can we implement the paintComponent() method of a JPanel in Java?\n
- How to set background color in jQuery?
- How to set background color in HTML?
- How to set the background color of a ttk.Combobox in tkinter?
- How to set the background color of a single tab in a JTabbedPane Container with Java?
- How to set the background color of a column in a matplotlib table?
- How to set background color of a View in iOS App?
- How to set background color of a view in Android App

Advertisements