java 中怎样设置窗口的颜色

java 中怎样设置窗口的颜色,第1张

调用需要设置颜色的控件的setBackgroud()方法就可以了。

但是设置JFrame和JLabel的背景色,一般就是下面的做法

JFrame frame = new JFrame()

frame.setBackground(Color.Red)

JLabel l = new JLabel()

l.setBackground(Color.Yellow)

frame.add(l)

结果根本就没有反应。这是由于Swing跟AWT有千丝万缕的联系,它既要支持AWT又要有自己新的体系,所以呢,这个如果对于AWT中的Frame是可以直接通过setBackground来设置背景色,但是对于JFrame则不可以,应该采用下面的方法:

JFrame frame = new JFrame()

frame.getContentPane().setBackground(Color.Red)

而对于JLabel来说则要设置JLabel为不透明的才行,即

JLabel comp = new JLabel(value)

comp.setBackground(color)

comp.setOpaque(true)

这句代码frame.setBackground(Color.Red)

改变的是框架的颜色,框架的上面还有窗格,所以你要改变窗格的颜色才可以侧低改变框架的颜色

在主函数里加Containerframe.getContentPane()意思是获得窗格

             setBackground(Color.Red)                       改变窗格颜色

另外再附一段背景颜色渐变的代码

运行示意图如下:

import java.awt.Color

import java.awt.GradientPaint

import java.awt.Graphics

import java.awt.Graphics2D

import javax.swing.JPanel

import java.awt.BorderLayout

import java.awt.EventQueue

import javax.swing.JFrame

import javax.swing.JPanel

class ShadePanel extends JPanel {

   

    private static final long serialVersionUID = -2644424271663261406L

   

    public ShadePanel() {

        super()

        setLayout(null)

    }

   

    @Override

    protected void paintComponent(Graphics g1) {// 重写绘制组件外观

        Graphics2D g = (Graphics2D) g1

        super.paintComponent(g)// 执行超类方法

        int width = getWidth()// 获取组件大小

        int height = getHeight()

        // 创建填充模式对象

        GradientPaint paint = new GradientPaint(0, 0, Color.CYAN, 0, height,

                Color.MAGENTA)

        g.setPaint(paint)// 设置绘图对象的填充模式

        g.fillRect(0, 0, width, height)// 绘制矩形填充控件界面

    }

}

 

 

 

public class ShadeBackgroundImage extends JFrame {

   

   

    private static final long serialVersionUID = 4693799019369193520L

    private JPanel contentPane

   

   

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            public void run() {

                try {

                    ShadeBackgroundImage frame = new ShadeBackgroundImage()

                    frame.setVisible(true)

                } catch (Exception e) {

                    e.printStackTrace()

                }

            }

        })

    }

   

   

    public ShadeBackgroundImage() {

        setTitle("背景为渐变色的主界面")// 设置窗体标题

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

        setBounds(100, 100, 450, 300)

        contentPane = new JPanel()// 创建内容面板

        contentPane.setLayout(new BorderLayout(0, 0))

        setContentPane(contentPane)

        ShadePanel shadePanel = new ShadePanel()// 创建渐变背景面板

        contentPane.add(shadePanel, BorderLayout.CENTER)// 添加面板到窗体内容面板

    }

}

下拉框和textfield,textarea这些控件都有setforeground方法,这个函数是设置前景色的,设置为红色就行了.

例如:textfield

txt=new

textfield("请输入姓名")

txt.setforeground(color.red)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12204198.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存