Java Swing 程序设计02
Posted xingweikun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Swing 程序设计02相关的知识,希望对你有一定的参考价值。
常用布局管理器
绝对布局
package swing;
import java.awt.*;
import javax.swing.*;
public class AbsolutePosition extends JFrame {
public AbsolutePosition() {
setTitle("本窗体使用绝对布局");
setLayout(null);
setBounds(0, 0, 200, 150);
Container c = getContentPane();
JButton b1 = new JButton("按钮1");
JButton b2 = new JButton("按钮2");
b1.setBounds(10, 30, 80, 30);
b2.setBounds(60, 70, 100, 20);
c.add(b1);
c.add(b2);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new AbsolutePosition();
}
}
流布局管理器
package swing;
import java.awt.*;
import javax.swing.*;
public class FlowLayoutPosition extends JFrame {
public FlowLayoutPosition() {
setTitle("本窗体使用流布局管理器");
Container c = getContentPane();
setLayout(new FlowLayout(2, 10, 10));
for (int i = 0; i < 10; i++) {
c.add(new JButton("button" + i));
}
setSize(300, 200);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new FlowLayoutPosition();
}
}
边界布局管理器
package swing;
import java.awt.*;
import javax.swing.*;
public class BorderLayoutPosition extends JFrame {
String[] border = { BorderLayout.CENTER, BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.WEST,
BorderLayout.EAST };
String[] buttonName = { "center button", "north button", "south button", "west button", "east button" };
public BorderLayoutPosition() {
setTitle("这个窗体使用边界布局管理器");
Container container = getContentPane();
setLayout(new BorderLayout());
for (int i = 0; i < border.length; i++) {
container.add(border[i], new JButton(buttonName[i]));
}
setSize(350, 200);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new BorderLayoutPosition();
}
}
网格布局管理器
package swing;
import java.awt.*;
import javax.swing.*;
public class GridLayoutPosition extends JFrame {
public GridLayoutPosition() {
Container c = getContentPane();
setLayout(new GridLayout(7, 3, 5, 5));
for (int i = 0; i < 20; i++) {
c.add(new JButton("button" + i));
}
setSize(300, 300);
setTitle("这是一个使用网格布局管理器的窗体");
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new GridLayoutPosition();
}
}
常用面板
JPanel面板
package swing;
import java.awt.*;
import javax.swing.*;
public class JPanelTest extends JFrame{
public JPanelTest() {
Container c=getContentPane();
c.setLayout(new GridLayout(2,1,10,10));
JPanel p1=new JPanel(new GridLayout(1,3,10,10));
JPanel p2=new JPanel(new GridLayout(1,2,10,10));
JPanel p3=new JPanel(new GridLayout(1,2,10,10));
JPanel p4=new JPanel(new GridLayout(2,1,10,10));
p1.add(new JButton("1")); // 在面板中添加按钮
p1.add(new JButton("1"));
p1.add(new JButton("2"));
p1.add(new JButton("3"));
p2.add(new JButton("4"));
p2.add(new JButton("5"));
p3.add(new JButton("6"));
p3.add(new JButton("7"));
p4.add(new JButton("8"));
p4.add(new JButton("9"));
c.add(p1); // 在容器中添加面板
c.add(p2);
c.add(p3);
c.add(p4);
setTitle("在这个窗体中使用了面板");
setSize(420, 200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelTest();
}
}
JScrollPane面板
package swing;
import java.awt.*;
import javax.swing.*;
public class JScrollPaneTest extends JFrame {
public JScrollPaneTest() {
Container c = getContentPane(); // 创建容器
JTextArea ta = new JTextArea(20, 50); // 创建文本区域组件
JScrollPane sp = new JScrollPane(ta); // 创建JScrollPane面板对象
c.add(sp); // 将该面板添加到该容器中
setTitle("带滚动条的文字编译器");
setSize(200, 200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollPaneTest();
}
}
以上是关于Java Swing 程序设计02的主要内容,如果未能解决你的问题,请参考以下文章