简体   繁体   English

使用自定义纹理制作JFrame

[英]making JFrame with custom texture

How to set a custom window's frame header? 如何设置自定义窗口的框架标题?

Instead of this blue header I want to use a texture from image. 而不是这个蓝色的标题,我想使用图像的纹理。

code: 码:

final JFrame frame = new JFrame();
BufferedImage image = ImageIO.read(new File("d:/texture.bmp"));

default window: 默认窗口:

在此处输入图片说明

I do not think that you can do anything about the colors or images in the title bar of a JFrame, at least not without using native code to implement a platform-specific solution. 我认为您不能对JFrame标题栏中的颜色或图像做任何事情,至少不能不使用本机代码来实现特定于平台的解决方案。 That is because JFrame actually uses your native windowing system to create the window. 这是因为JFrame实际上使用您的本机窗口系统来创建窗口。

As for internal frames, it would make sense for you to be able to customize it, since it is a component rendered inside of a window that is controlled by Java. 至于内部框架,您可以对其进行自定义,因为它是在Java控制的窗口内部呈现的组件。 Indeed, there are plenty of JInternalFrame properties that you can set in your UI manager. 确实,您可以在UI管理器中设置许多JInternalFrame属性

You cannot place an image over there. 您不能在那儿放置图像。 However you can try JavaFX . 但是,您可以尝试JavaFX

Here is something i did by trying out looks and feel : 这是我通过尝试外观进行的操作:

import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.metal.*;
public class TitleColor
{
public static void main_helper (String args[])
{
JFrame f = new JFrame ();
f.setDefaultCloseOperation 
(
JFrame.DISPOSE_ON_CLOSE
);
f.setSize (300, 300);
f.setLocationRelativeTo (null);

f.setUndecorated ( true );
f.getRootPane ().setWindowDecorationStyle
(
JRootPane.FRAME
);

JPanel panel = new JPanel ();
panel.setBackground ( java.awt.Color.white );
f.setContentPane ( panel );

DefaultMetalTheme z =
new DefaultMetalTheme ()
{
//inactive title color
public ColorUIResource
getWindowTitleInactiveBackground() 
{ 
return new ColorUIResource 
(java.awt.Color.orange); 
}

//active title color
public ColorUIResource
getWindowTitleBackground() 
{ 
return new ColorUIResource 
(java.awt.Color.orange); 
}
//start ActiveBumps
public ColorUIResource 
getPrimaryControlHighlight() 
{ 
return new ColorUIResource 
(java.awt.Color.orange); 
}
public ColorUIResource 
getPrimaryControlDarkShadow() 
{ 
return new ColorUIResource 
(java.awt.Color.orange); 
}

public ColorUIResource 
getPrimaryControl() 
{ 
return new ColorUIResource 
(java.awt.Color.orange); 
}
//end ActiveBumps

//start inActiveBumps
public ColorUIResource 
getControlHighlight ()
{
return new ColorUIResource 
(java.awt.Color.orange); 
}

public ColorUIResource 
getControlDarkShadow ()
{
return new ColorUIResource 
(java.awt.Color.orange); 
}

public ColorUIResource 
getControl ()
{
return new ColorUIResource 
(java.awt.Color.orange); 
}
//end inActiveBumps



};



MetalLookAndFeel.setCurrentTheme
(
z
);

try
{
UIManager.setLookAndFeel
(
new MetalLookAndFeel ()
);
}
catch (Exception e)
{
e.printStackTrace ();
}   

SwingUtilities.updateComponentTreeUI (f);


f.setVisible (true);


}
public static void main (final String args[])
{
SwingUtilities.invokeLater
(
new Runnable ()
{
public void run ()
{
main_helper ( args );
}
}
);
}
}

You should try JavaFX to create awesome window on java. 您应该尝试使用JavaFX在Java上创建很棒的窗口。

http://www.oracle.com/technetwork/java/javafx/samples/index.html http://www.oracle.com/technetwork/java/javafx/samples/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM