Java编程使用箱式布局管理器示例【基于swing组件】

时间:2022-09-22 16:54:57

本文实例讲述了java编程使用箱式布局管理器。分享给大家供大家参考,具体如下:

先来看看运行效果:

Java编程使用箱式布局管理器示例【基于swing组件】

完整代码如下:

?
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
package awtdemo;
import java.awt.borderlayout;
import java.awt.container;
import javax.swing.box;
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.jscrollpane;
import javax.swing.jtextarea;
import javax.swing.jtextfield;
/**
 * 使用箱式布局管理器
 *
 * @author han
 *
 */
public class boxlayout_1 extends jframe {
    /**
   *
   */
    private static final long serialversionuid = 6896925750596855002l;
    public boxlayout_1() {
        // todo auto-generated constructor stub
        container container = getcontentpane();
        box box = box.createverticalbox();
        container.add(box, borderlayout.north);
        box.add(box.createverticalstrut(5));
        box topicbox = box.createhorizontalbox();
        box.add(topicbox);
        topicbox.setalignmentx(1);
        topicbox.add(box.createhorizontalstrut(5));
        jlabel topiclabel = new jlabel("主题:");
        topicbox.add(topiclabel);
        topicbox.add(box.createhorizontalstrut(5));
        jtextfield topictextfield = new jtextfield(30);
        topicbox.add(topictextfield);
        box box2 = box.createverticalbox();
        container.add(box2, borderlayout.center);
        box contentbox = box.createhorizontalbox();
        contentbox.setalignmentx(1);
        box2.add(box.createverticalstrut(5));
        box2.add(contentbox);
        contentbox.add(box.createhorizontalstrut(5));
        jlabel contentlabel = new jlabel("内容:");
        contentlabel.setalignmenty(0);
        contentbox.add(contentlabel);
        contentbox.add(box.createhorizontalstrut(5));
        stringbuilder stringbuilder = new stringbuilder();
        string contentstring = new string("利用箱式布局管理器实现组件的右对齐"
                + "和上对齐,以及控制组件之间的间距!");
        stringbuilder.append(contentstring);
        stringbuilder.append("\n");
        stringbuilder.append(contentstring);
        contentstring = stringbuilder.tostring();
        jtextarea contenttextarea = new jtextarea(contentstring, 3, 30);
        contenttextarea.setlinewrap(true);
        jscrollpane scrollpane = new jscrollpane();
        scrollpane.setalignmenty(0);
        scrollpane.setviewportview(contenttextarea);
        contentbox.add(scrollpane);
        contentbox.add(box.createhorizontalstrut(5));
        // system.out.println(contenttextarea.requestfocusinwindow());
        box2.add(box.createverticalstrut(5));
        jbutton submitbutton = new jbutton("确定");
        box2.add(submitbutton);
        submitbutton.setalignmentx(1);
        box2.add(box.createverticalstrut(5));
    }
    /**
     * @param args
     */
    public static void main(string[] args) {
        // todo auto-generated method stub
        boxlayout_1 frame = new boxlayout_1();
        frame.settitle("www.zzvips.com - 使用箱式布局管理器");
        frame.setvisible(true);
        frame.setdefaultcloseoperation(jframe.exit_on_close);
        frame.pack();
    }
}

希望本文所述对大家java程序设计有所帮助。

原文链接:http://blog.csdn.net/gaowen_han/article/details/7738642