本文介绍了无法更改默认背景颜色的jframe表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我有一个小问题。我使用Java SWING包从头开发了一个JFrame表单。 外观还可以。我没有使用布局管理器,所以我手动给出了位置和放大器。尺寸。 它还连接到MySQL数据库,也没有任何问题。 现在我想更改JFrame表单的默认背景颜色。我无法做到这一点。另外我发现initComponents()还没有被我自己声明。 因此,当我尝试声明getContentPane()时,我也不能这样做。 b $ b 请参阅以下代码片段并帮助我解决问题。 我使用带有JavaSE的NetBeans 。 下面的代码片段在我的开发环境中运行正常。 我有什么试过:Hi guys,I have a small question. I have developed a JFrame Form from scratch using Java SWING package.The appearance is ok. I have not used a layout manager so that I have manually given the locations & the dimensions.It also connect to a MySQL database and there aren't any issues regarding that also.Now I want to change the default background color of the JFrame Form. I am unable to do that. Also I found that initComponents() has not declared by my self.So that when I tried to declare getContentPane() but I couldn't do that also.Please refer the below snippet and help me to solve the issue.I am using NetBeans with JavaSE.The below snippet is working fine in my development environment.What I have tried:/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package javaswing;import java.awt.Color;import java.awt.Container;import javax.swing.*;import java.awt.event.*;import java.sql.*;import java.util.logging.Level;import java.util.logging.Logger;/** * * @author Chiranthaka */public class JavaSwingFirstExample { JFrame jFirstForm = new JFrame(); JLabel jLabFirstName = new JLabel("First Name"); JLabel jLabLastName = new JLabel("Last Name"); JLabel jLabAddress = new JLabel("Address"); JLabel jLabAge = new JLabel("Age"); JTextField jTxtFirstName = new JTextField(); JTextField jTxtLastName = new JTextField(); JTextField jTxtAddress = new JTextField(); JTextField jTxtAge = new JTextField(); JButton jButAdd = new JButton("Add"); JButton jButUpdate = new JButton("Update"); JButton jButDelete = new JButton("Delete"); JButton jButEdit = new JButton("Edit"); JButton jButCancel = new JButton("Cancel"); JavaSwingFirstExample(){ jFirstForm.setSize(400,400); jFirstForm.setLayout(null); jFirstForm.setVisible(true); //Adding controls to the form. jFirstForm.add(jLabFirstName); jFirstForm.add(jLabLastName); jFirstForm.add(jLabAddress); jFirstForm.add(jLabAge); jFirstForm.add(jTxtFirstName); jFirstForm.add(jTxtLastName); jFirstForm.add(jTxtAddress); jFirstForm.add(jTxtAge); jFirstForm.add(jButAdd); jFirstForm.add(jButUpdate); jFirstForm.add(jButDelete); jFirstForm.add(jButEdit); jFirstForm.add(jButCancel); //Setting the size of the controls and placing them. jLabFirstName.setBounds(20, 10, 150, 35); jLabLastName.setBounds(20, 55, 150, 35); jLabAddress.setBounds(20, 100, 150, 35); jLabAge.setBounds(20, 145, 150, 35); jTxtFirstName.setBounds(170, 10, 200, 35); jTxtLastName.setBounds(170, 55, 200, 35); jTxtAddress.setBounds(170, 100, 200, 35); jTxtAge.setBounds(170, 145, 200, 35); jButAdd.setBounds(30, 200, 150, 35); jButUpdate.setBounds(200, 200, 150, 35); jButDelete.setBounds(30, 255, 150, 35); jButEdit.setBounds(200, 255, 150, 35); jButCancel.setBounds(120, 310, 150, 35); //Addind an action when clicked the 'OK' button. jButAdd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(jFirstForm, "Eggs are not supposed to be green."); } }); //Addind an action when clicked the 'Cancel' button. jButCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { clearText(); } }); //Setting the default close action. jFirstForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new JavaSwingFirstExample(); String dbURL = "jdbc:mysql://localhost:3306/basicinfo"; String dbUser = "root"; String dbPassword = "1234@com"; try { Connection dbConn = DriverManager.getConnection(dbURL, dbUser, dbPassword); JOptionPane.showMessageDialog(null, "Sucessful. My SQL Database is now conneted!"); } catch (SQLException ex) { Logger.getLogger(JavaSwingFirstExample.class.getName()).log(Level.SEVERE, null, ex); JOptionPane.showMessageDialog(null, "Database is not connecting!"); } } private void clearText(){ jTxtFirstName.setText(""); jTxtLastName.setText(""); jTxtAddress.setText(""); jTxtAge.setText(""); }} 请参考代码并向我推荐一个解决方案。 谢谢, ChiranSJPlease refer the code and suggest me a solution.Thanks,ChiranSJ推荐答案使用这个Use thisjFirstForm.getContentPane().setBackground(Color.BLUE); 这篇关于无法更改默认背景颜色的jframe表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-06 02:15