Java swing window frame
How开发者_Go百科 to manipulate the window frame with swing in java ? Like how to change the color, how to make it to be not the default style of the PC theme, but to be a image for example?
To change the color you can use setBackground(Color c) method on frame object or on panel object. example:
JFrame fr=new JFrame("Color demo");
fr.setBackground(Color.red); // color set to red
fr.setSize(900,200); // size changed
fr.setResizable(false); // you cant resize your window now
For further customizing your java frame you could follow this or this one. You can get a start on swing on this website
Everything to do with the way that windows look in Swing can be handled by the Look and Feel (LaF). See the LaF tag for more info (I wrote it). Most look-and-feels will style the contents of the window and all the components inside it.
There are a number of default LaFs that are supplied with Swing in the JDK. They include, Motif, Metal and Synth (which is a blank style that you customize yourself). There are many more that you can find and download online. You can use any of them by putting them into your classpath and running UIManager.setLookAndFeel(...)
in your main method, before you show your GUI.
It sounds like you are specifically looking for a way to customize the window frames, and this is not done often. I personally wouldn't recommend it, but if you really want to, you can turn the frame decorations off by calling setUndecorated(true)
on a Window
. You're then left with a borderless window. You must then decide how you want to re-implement the window features you want, like closing with a custom close button.
As fas as I know, there is not way to do it. You can create an own class for your customized windows which extends JWindow for example. You can create the style on you own then. It's just a little work to do.
// add pacakge and imports as needed...
/**
* Sample class implementation for windows with own frame style.
* Additional constructors may be added as needed. Default constructor used
* to show principle.
*/
public class MyWindow extends JWindow {
public MyWindow() {
super();
initUI();
}
private void initUI() {
setLayout(new BorderLayout());
add(new MyWindowFrameTop(this));
add(new MyWindowFrameLeft(this));
add(new MyWindowFrameRight(this));
add(new MyWindowFrameBottom(this));
}
}
The JWindow is a "first-class citizen" as the Java API doc tells. It's a full featured window within the desktop but misses all implementations for window actions like close, maximize, minimize and the window menu. The implementation of this features might be added like above by using the BorderLayout and putting own implementations of the frame objects into work. The MyWindowFrameXXX objects might be JPanels which draw the wanted frame box parts, the title image and so forth. It's also possible to add buttons for window actions and to implement mouse actions for resize. To implement this, the frame objects need the parent windows reference to perform all actions on the window.
It would go way to far here to write everything down here. I think, the principle is shown and I also do not know what the actual effect to be implemented really is.
精彩评论