-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJBox.java
177 lines (166 loc) · 6.33 KB
/
JBox.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import javax.swing.*;
import java.awt.*;
/** */
public class JBox extends JPanel {
/** [Internal] */
public JBox( int orientation ) {
BoxLayout boxLayout = new BoxLayout( this, orientation );
setLayout( boxLayout );
this.orientation=orientation;
}
/** [Internal] */
public JBox( int orientation ,float alignment) {
this(orientation);
doalign=true; this.alignment=alignment;
}
private boolean doalign=false; private float alignment=0;
private int orientation=0;
//
/** */
public static final int LINE_AXIS=BoxLayout.LINE_AXIS;
/** */
public static final int PAGE_AXIS=BoxLayout.PAGE_AXIS;
/** */
public static final int X_AXIS=BoxLayout.X_AXIS;
/** */
public static final int Y_AXIS=BoxLayout.Y_AXIS;
/** */
public static final float BOTTOM_ALIGNMENT =Component.BOTTOM_ALIGNMENT;
/** */
public static final float CENTER_ALIGNMENT=Component.CENTER_ALIGNMENT ;
/** */
public static final float LEFT_ALIGNMENT=Component.LEFT_ALIGNMENT;
/** */
public static final float RIGHT_ALIGNMENT=Component.RIGHT_ALIGNMENT;
/** */
public static final float TOP_ALIGNMENT=Component.TOP_ALIGNMENT ;
/** */
public static final float BOTTOM =Component.BOTTOM_ALIGNMENT;
/** */
public static final float CENTER=Component.CENTER_ALIGNMENT ;
/** */
public static final float LEFT=Component.LEFT_ALIGNMENT;
/** */
public static final float RIGHT=Component.RIGHT_ALIGNMENT;
/** */
public static final float TOP=Component.TOP_ALIGNMENT ;
/** Create a hbox */
public static JBox createHorizontalJBox(){ return new JBox(LINE_AXIS );}
/** Create a vbox */
public static JBox createVerticalJBox() { return new JBox( PAGE_AXIS );}
/** Create a hbox */
public static JBox hbox(){ return new JBox(LINE_AXIS,BOTTOM_ALIGNMENT );}
/** Create a vbox */
public static JBox vbox(){ return new JBox( PAGE_AXIS,LEFT_ALIGNMENT );}
/** Create a hbox with bottom aligned content */
public static JBox hbox(Component... bb){
JBox b= new JBox(LINE_AXIS,BOTTOM_ALIGNMENT );
for(Component c:bb)b.add(c);return b;}
/** Create a vbox with left aligned content */
public static JBox vbox(Component... bb) {
JBox b= new JBox(PAGE_AXIS,LEFT_ALIGNMENT );
for(Component c:bb)b.add(c);return b;}
/** Create a hbox component */
public static JBox hbox(float a,Component... bb){
JBox b= new JBox(LINE_AXIS,a);
for(Component c:bb)b.add(c);return b;}
/** Create a vbox component */
public static JBox vbox(float a,Component... bb) {
JBox b= new JBox(PAGE_AXIS,a);
for(Component c:bb)b.add(c);return b;}
/** Create a hbox component */
public static JBox createHorizontalJBox(float a){ return new JBox(LINE_AXIS,a );}
/** Create a vbox component */
public static JBox createVerticalJBox(float a) { return new JBox( PAGE_AXIS,a );}
/** Create a hbox component */
public static JBox hbox(float a){ return new JBox(LINE_AXIS,a );}
/** Create a vbox component */
public static JBox vbox(float a) { return new JBox( PAGE_AXIS,a );}
//public static Component glue(){ return Box.createGlue(); }
/** Create a hglue component */
public static Component hglue(){ return Box.createHorizontalGlue(); }
/** Create a vglue component */
public static Component vglue(){ return Box.createVerticalGlue(); }
/** Create a hspace component */
public static Component hspace(int n){
return Box.createRigidArea(new Dimension( n, 1));}
/** Create a vspace component */
public static Component vspace(int n){
return Box.createRigidArea(new Dimension( 1, n));}
/** set size of a component */
public static JComponent setSize(JComponent c,int w,int h){
Dimension d= new Dimension(w,h);
c.setMinimumSize(d);
c.setPreferredSize(d);
c.setMaximumSize(d);
return c;
}
/** set strechable size of a component */
public static JComponent setSize(JComponent c,int w,int h,int dw,int dh){
c.setMinimumSize(new Dimension(Math.max(0,w-dw),Math.max(0,h-dh)));
c.setPreferredSize(new Dimension(w,h));
c.setMaximumSize(new Dimension(w+dw,h+dh));
return c;
}
/** set font of components in a box */
public void setFont(Font f){
Component[] cs=getComponents();
for(Component c:cs)c.setFont(f);
}
/** set foreground color of a box */
public void setForeground(Color cl){
Component[] cs=getComponents();
for(Component c:cs)c.setForeground(cl);
}
/** set background color of a box */
public void setBackground(Color cl){
super.setBackground(cl);
super.setOpaque(true);
Component[] cs=getComponents();
for(Component c:cs)c.setBackground(cl);
}
/** make the box opaque */
public void setOpaque(boolean b){
super.setOpaque(b);
Component[] cs=getComponents();
for(Component c:cs)
if(c instanceof JComponent)
((JComponent)c).setOpaque(b);
}
/** add component to a box */
public Component add(Component c){
Component c1=super.add(c);
if(doalign&&c instanceof JComponent){
if(orientation==LINE_AXIS||orientation==X_AXIS)
((JComponent) c).setAlignmentY(alignment);
else
((JComponent) c).setAlignmentX(alignment);
}
return c1;
}
/** Return height of the screen */
public static int getScreenHeight(){
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get size
Dimension dimension = toolkit.getScreenSize();
return dimension.height;
}
/** Return width of the screen */
public static int getScreenWidth(){
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get size
Dimension dimension = toolkit.getScreenSize();
return dimension.width;
}
/** Layout components in a box */
public void revalidate(){
Component[] body=getComponents();
if(body!=null)
for(Component c:body)
if(c!=null&&c instanceof JComponent)
((JComponent)c).revalidate();
super.revalidate();
}
/** [Internal] */
public static final long serialVersionUID = 42L;
}