「java窗口不显示颜色」java窗口背景颜色
今天给各位分享java窗口不显示颜色的知识,其中也会对java窗口背景颜色进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、为什么这个java程序创建出来的窗口颜色显示不出来?
- 2、JAVA面板背景颜色,设置了红色的黄色,显示出来只有黄色?请问红色去哪里了。怎么才能显示出来
- 3、java窗口背景颜色怎么设定?用setBackground()好像不行,请大侠指教!
- 4、java 中怎样设置窗口的颜色
- 5、java中设置了背景颜色,为什么不能显示?
- 6、java背景颜色设置,设置了但不显示
为什么这个java程序创建出来的窗口颜色显示不出来?
因为JFrame分为几层RootPane LayeredPane ContentPane GlassPane
其中GlassPane是透明的。
所以设置背景色,一般就设置在ContentPane上才能显示。
所以背景颜色代码修改成
getContentPane().setBackground(Color.lightGray);
效果图
JAVA面板背景颜色,设置了红色的黄色,显示出来只有黄色?请问红色去哪里了。怎么才能显示出来
因为你的面板(JPanel)对象p是完全不透明的,它遮住了下面的颜色。要使下面的颜色可见,需设置不透明度。
java窗口背景颜色怎么设定?用setBackground()好像不行,请大侠指教!
你好!
首先,你说的Java窗口是指JFrame或者Frame
其次,你说的窗口背景颜色是指直接调用JFrame或者Frame的setBackground(Color color)方法设置后显示出来的颜色。其实,你的想法是正确的,但是我想提醒你的是,你没搞明白JFrame的显示机制。在你直接调用这个方法后,你的确设置了背景颜色,而你看到的却不是直接的JFrame或者Frame,而是JFrame.getContentPane().而JFrame上的contentPane默认是Color.WHITE的,所以,无论你对JFrame或者Frame怎么设置背景颜色,你看到的都只是contentPane.
最后,讲解决办法:
办法A:在完成初始化,调用getContentPane()方法得到一个contentPane容器,然后将其设置为不可见,即setVisible(false)。这样,你就可以看到JFrame的庐山真面貌啦!
核心代码this.getContentPane().setVisible(false);//得到contentPane容器,设置为不可见
实例完整代码如下:
/*
* TestJFrameBGColor.java
*
* Created on 2011-5-8, 0:21:20
*/
package testjframebgcolor;
import java.awt.Color;
/**
*
* @author 叶科良
*/
public class TestJFrameBGColor extends javax.swing.JFrame {
/** Creates new form TestJFrameBGColor */
public TestJFrameBGColor() {
initComponents();
this.getContentPane().setVisible(false);//得到contentPane容器,设置为不可见
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// editor-fold defaultstate="collapsed" desc="Generated Code"
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(testjframebgcolor.TestJFrameBGColorApp.class).getContext().getResourceMap(TestJFrameBGColor.class);
setBackground(resourceMap.getColor("Form.background")); // NOI18N
setName("Form"); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// /editor-fold
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestJFrameBGColor().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
方法B:将contentPane的颜色设置为你想要的颜色,而不是对JFrame本身设置,
核心代码:this.getContentPane().setBackground(Color.red);//设置contentPane为红色
将核心代码替换方法A核心代码即可实现
方法C:为JFrame添加一个Panel或者JLabel等其他组件,设置其颜色为你想要的颜色,然后将其覆盖JFrame窗口即可
java 中怎样设置窗口的颜色
调用需要设置颜色的控件的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);// 添加面板到窗体内容面板
}
}
java中设置了背景颜色,为什么不能显示?
private JLabel jla=new JLabel("用户名");;
private JLabel jlb=new JLabel("主机名");
private JLabel jlc=new JLabel("IP地址");;
不要ADD到this而是ADD到jpList
如:
import java.awt.*;
import javax.swing.*;
public class T extends JFrame
{
private JLabel jla=new JLabel("用户名");;
private JLabel jlb=new JLabel("主机名");
private JLabel jlc=new JLabel("IP地址");;
JPanel jpList;
JScrollPane jsp;
public T()
{
//界面
JPanel jpList=new JPanel();
jpList.setBackground(Color.blue);
this.setLayout(null);
jla.setBounds(30,20,40,40);
jpList.add(jla);
jlb.setBounds(90,20,40,40);
jpList.add(jlb);
jlc.setBounds(150,20,40,40);
jpList.add(jlc);
jsp=new JScrollPane(jpList);
jsp.setBounds(5,20,280,150);
this.add(jsp);
this.setTitle("Java局域网文件传输器");
this.setBounds(100,100,300,400);
this.setBackground(Color.blue);
this.setVisible(true);
}
public static void main(String[] args)
{
new T();
}
}
java背景颜色设置,设置了但不显示
对于设置颜色的问题,因为设置的是内容窗格的颜色,故应该先获取内容窗格的对象在设置 如:
getContentPane().setBackground(new java.awt.Color(128,64,64));
java窗口不显示颜色的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java窗口背景颜色、java窗口不显示颜色的信息别忘了在本站进行查找喔。
发布于:2022-11-27,除非注明,否则均为
原创文章,转载请注明出处。