Skip to content

Commit 968c5f0

Browse files
hansonrhansonr
authored andcommitted
button/label y-translate AWT and Swing
1 parent ae4840c commit 968c5f0

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

sources/net.sf.j2s.java.core/src/test/TApp2.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.awt.BasicStroke;
66
import java.awt.Button;
77
import java.awt.Color;
8+
import java.awt.Component;
89
import java.awt.Font;
910
import java.awt.Graphics;
1011
import java.awt.Graphics2D;
@@ -21,18 +22,46 @@
2122
import java.awt.event.MouseEvent;
2223
import java.awt.event.MouseListener;
2324

25+
import javax.swing.JButton;
2426
import javax.swing.JLabel;
2527
import javax.swing.JPanel;
2628

2729
public class TApp2 extends Applet {
2830

2931
TextArea ta;
3032

33+
private void addButtonTest() {
34+
for (int i = 0; i < 4; i++) {
35+
for (int j = 0; j < 4; j++) {
36+
Button b = new Button("XyX");
37+
Label l = new Label("XyX", Label.CENTER);
38+
setLBBounds((Component) b, (Component) l, i, j);
39+
}
40+
}
41+
}
42+
43+
private void setLBBounds(Component b, Component l, int i, int j) {
44+
int x = 40 + i * 170;
45+
int y = 350 + j * 40;
46+
int w = 70 + i * 10;
47+
int h = 25 + j * 4;
48+
b.setBounds(x, y, w, h);
49+
l.setBounds(x + 105, y, w - 30, h);
50+
b.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 10 + i * 3));
51+
l.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 10 + i * 3));
52+
l.setBackground(Color.cyan);
53+
add(b);
54+
add(l);
55+
}
56+
3157
public void init() {
32-
setSize(400, 400);
58+
setSize(800, 600);
3359
setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
3460
setBackground(Color.yellow);
3561
setLayout(null);
62+
63+
addButtonTest();
64+
3665
JPanel panel = new JPanel();
3766
panel.setBounds(10, 10, 100, 150);
3867
add(panel);
@@ -113,7 +142,7 @@ public void adjustmentValueChanged(AdjustmentEvent e) {
113142
new TestGraphic(this).testGraphic();
114143
}
115144

116-
static class TestGraphic{
145+
static class TestGraphic{
117146
private TApp2 tApp2;
118147

119148
public TestGraphic(TApp2 tApp2) {
@@ -126,7 +155,7 @@ public TestGraphic(TApp2 tApp2) {
126155

127156
void testGraphic() {
128157
Button b = new Button("g");
129-
b.setBounds(300,300,20,20);
158+
b.setBounds(300,300,40,20);
130159
// images are created only when a button is displayable
131160
Image i1 = b.createImage(100, 100);
132161
System.out.println("b.isDisplayable " + b.isDisplayable() + " " + i1);

sources/net.sf.j2s.java.core/src/test/TApp2_Swing.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import java.awt.AlphaComposite;
44
import java.awt.BasicStroke;
5+
import java.awt.Button;
56
import java.awt.Color;
7+
import java.awt.Component;
68
import java.awt.Font;
79
import java.awt.Graphics;
810
import java.awt.Graphics2D;
911
import java.awt.GridLayout;
1012
import java.awt.Image;
13+
import java.awt.Label;
1114
import java.awt.event.AdjustmentEvent;
1215
import java.awt.event.AdjustmentListener;
1316
import java.awt.event.FocusEvent;
@@ -27,8 +30,34 @@ public class TApp2_Swing extends JApplet {
2730

2831
JTextArea ta;
2932

30-
public void init() {
31-
setSize(400, 400);
33+
private void addButtonTest() {
34+
for (int i = 0; i < 4; i++) {
35+
for (int j = 0; j < 4; j++) {
36+
JButton b = new JButton("XyX");
37+
JLabel l = new JLabel("XyX", JLabel.CENTER);
38+
setLBBounds((Component) b, (Component) l, i, j);
39+
}
40+
}
41+
}
42+
43+
private void setLBBounds(Component b, Component l, int i, int j) {
44+
int x = 40 + i * 170;
45+
int y = 350 + j * 40;
46+
int w = 70 + i * 10;
47+
int h = 25 + j * 4;
48+
b.setBounds(x, y, w, h);
49+
l.setBounds(x + 105, y, w - 30, h);
50+
b.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 10 + i * 3));
51+
l.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 10 + i * 3));
52+
l.setBackground(Color.cyan);
53+
add(b);
54+
add(l);
55+
}
56+
57+
58+
public void init() {
59+
setSize(800, 600);
60+
addButtonTest();
3261
setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
3362
setBackground(Color.yellow);
3463
setLayout(null);
@@ -125,7 +154,7 @@ public TestGraphic2(TApp2_Swing tApp2) {
125154

126155
void testGraphic() {
127156
JButton b = new JButton("g");
128-
b.setBounds(300,300,20,20);
157+
b.setBounds(300,300,40,20);
129158
// images are created only when a button is displayable
130159
Image i1 = b.createImage(100, 100);
131160
System.out.println("b.isDisplayable " + b.isDisplayable() + " " + i1);

0 commit comments

Comments
 (0)