「java设置窗口背景」Java设置窗口背景图片

博主:adminadmin 2022-12-20 12:45:07 68

本篇文章给大家谈谈java设置窗口背景,以及Java设置窗口背景图片对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java窗口的背景颜色

public void setBackground(Color c)设置组件的背景色。

背景色对每个组件的影响各不相同,并且部分受背景色影响的组件在不同的操作系统之间可能有所不同。

用这个方法嘛,改变不了,可能是你在窗体上添加了别的容器什么的把当前要改变的给挡上了.仔细排查一下,相信你会改过来的.

JAVA中如何控制窗体背景与字体的颜色

如果就设置字体,即大小,加一句setFont就行了

import javax.swing.*;

import java.awt.*;

import java.awt.event.WindowEvent;

public class Q1{

JWindow window = new JWindow();

Q1(String pText){

//获取当前计算机屏幕尺寸

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

//定义标记显示信息

JLabel jl = new JLabel(pText,JLabel.CENTER);

Font d=new Font("楷体",Font.BOLD,10);

jl.setFont(d); //注意这句

//添加组件到窗口

window.getContentPane().add(jl, BorderLayout.CENTER);

// 设置窗口尺寸

window.setSize(300, 100);

//设置窗口显示位置

int w = window.getSize().width;

int h = window.getSize().height;

int x = (dim.width-w)/2;

int y = (dim.height-h)/2;

//移动窗口到X,Y坐标。这里时屏幕的中央

window.setLocation(x, y);

}

//隐藏窗口

public void hidden(){

window.setVisible(false);

}

//显示窗口

public void show(){

window.setVisible(true);

}

public static void main(String[] args){

//

Q1 jw = new Q1("沟通无距离!平行线聊天系统...o(∩_∩)o...");

jw.show();

//显示延时

try{

Thread.sleep(4000);

}catch(InterruptedException ie){

System.out.println("sleep error!!");

}

//调用隐藏

//jw.hidden();

}

public void windowClosing(WindowEvent windowEvent) {

System.exit(0);

}

}

如果要设置字体颜色,你可以自定义继承JLabel的类,在paintComponent(Graphics g)方法中,用g.setColor();g.drawString()做。

设置背景的话在setFont下加一句setBackground()方法,传Image类进去。

java向窗体添加背景图片的方法

import javax.swing.*;

import javax.imageio.*;

import java.awt.*;

import java.io.*;

public class AddImage {

public static void main(String[] args){

new AddImageFrame();

}

}

class AddImageFrame extends JFrame{

public AddImageFrame(){

super("添加背景图片");

setBounds(200,200,500,400);

setDefaultCloseOperation(this.EXIT_ON_CLOSE);

setVisible(true);

AddImagePanel aip= new AddImagePanel();

add(aip);

}

}

class AddImagePanel extends JPanel{

private Image backgroundimage=null;

public void paintComponent(Graphics g){

super.paintComponent(g);

try{

backgroundimage=ImageIO.read(new File("E:/picture/http_imgload.jpg")).getScaledInstance(getWidth(),getHeight(),Image.SCALE_FAST);

}catch(IOException e){

e.printStackTrace();

}

g.drawImage(backgroundimage,0,0,null);

image.flush();

}

}

这是设置背景图片的简单代码 你要的主要是 backgroundimage=ImageIO.read(new File("E:/picture/http_imgload.jpg")).getScaledInstance(getWidth(),getHeight(),Image.SCALE_FAST);

javax.image包里面有一个类 ImageIO有一个方法read(File string) 读取一个图片文件返回image对象,File会抛出异常,awt包里面的Image有个方法 getScaledstance(width,height,hints) 是缩放图片到多大,intnts是缩放的算法,取Image字段常量,有很多钟算法,你可以取一种,这个程序里图片的大小取的是容纳它的panel面板的大小,会随着面板的变化充满整个面板,这个变化过程要调用面板的 paintComponent()方法来监视,Graphics 中的drawImage(x,y,observer)方法来实现,observer是安全管理器,可以为null,最后绘画完后刷新就可以了,希望对你有帮助,俺也是初学者,交流为上 ,不过我个人还是认为下面的方法比较好

public void setBak() { //设置窗口背景

((JPanel) this.getContentPane()).setOpaque(false);

ImageIcon img = new ImageIcon(getClass().getResource("picture/http_imgload.jpg"));

JLabel background = new JLabel(img);

this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));

background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());

}构造器调用这个方法图片不会缩放,上面的例子 不用缩放的方法也可以做到,就看怎么看了

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中如何设置窗体的背景图像

在Java中设置窗体背景图像没有直接可用的方法,需要用很多代码来处理.总结起来两种方法可以完成这样的设置:一是用标签组件,二是用面板来设置.

关于java设置窗口背景和Java设置窗口背景图片的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-20,除非注明,否则均为首码项目网原创文章,转载请注明出处。