Swing是AWT的升级版。要使用Swing技术进行GUI编程的话,就要用到javax.swing包,该包下有很多类和接口,提供一组“轻量级”组件。
JFrame
public class JFrame
extends Frame
implements WindowConstants, Accessible, RootPaneContainer
JFrame是java.awt.Frame 的扩展版本,该版本添加了对 JFC/Swing 组件架构的支持。
根据JFrame类的继承关系可以看出,JFrame继承了Frame类,所以,Frame类的所有方法在JFrame中都可以使用,且JFrame也有自己特有的方法。
JFrame 类与 Frame 轻微不兼容。与其他所有 JFC/Swing 顶层容器一样,JFrame 包含一个 JRootPane 作为其唯一的子容器。不同于 AWT Frame: 根据规定,根窗格所提供的内容窗格应该包含 JFrame 所显示的所有非菜单组件。
在使用JFrame时,往其中添加组件时,是要添加到其内容窗格(容器)中即contentPane。
默认的内容窗格上会设置有 BorderLayout 管理器。
与 Frame 不同,当用户试图关闭窗口时,JFrame 知道如何进行响应。用户关闭窗口时,默认的行为只是简单地隐藏 JFrame。要更改默认的行为,可调用方法 setDefaultCloseOperation(int)。
Swing 不是线程安全的。
public class TestJFrame {
public void init(){
JFrame frame = new JFrame("TestJFrame");
JLabel label = new JLabel("欢迎学习JAVA");
frame.add(label);
frame.setVisible(true);
frame.setBounds(100,100,300,300);
frame.setBackground(Color.YELLOW);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJFrame().init();
}
}
JDialog
public class JDialog
extends Dialog
implements WindowConstants, Accessible, RootPaneContainer
JDialog类用于创建对话框窗口。JDialog 组件包含一个 JRootPane 作为其唯一子组件。contentPane 应该是所有 JDialog 子组件的父级。
将子级添加到 contentPane。默认的 contentPane 有一个 BorderLayout 管理器。
一般用JDialog来创建弹窗,该窗口默认是有关闭事件的,所以无须再去写关闭事件。
public class TestJDialog {
public void init(){
JFrame frame = new JFrame("TestJDialog");
Container container = frame.getContentPane();
container.setBackground(Color.pink);
container.setLayout(null);
JButton button = new JButton();
button.setText("点击弹出一个对话框");
button.setBounds(150,150,200,200);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
container.add(button);
frame.setVisible(true);
frame.setBounds(100,100,500,500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJDialog().init();
}
}
class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true);
this.setBackground(Color.WHITE);
this.setBounds(150,150,200,200);
this.setTitle("这是一个弹窗");
Container container = this.getContentPane();
container.setLayout(null);
container.setBackground(new Color(70,50,100));
JLabel label = new JLabel("欢迎学习JAVA");
container.add(label);
}
}
标签
public class JLabel
extends JComponent
implements SwingConstants, Accessible
该类用于短文本字符串或图像或二者的显示区。标签不对输入事件作出反应。因此,它无法获得键盘焦点。但是,标签可以为具有键盘替换功能却无法显示的邻近组件方便地显示其键盘替换功能。
JLabel 对象可以显示文本、图像或同时显示二者。可以通过设置垂直和水平对齐方式,指定标签显示区中标签内容在何处对齐。默认情况下,标签在其显示区内垂直居中对齐。默认情况下,只显示文本的标签是开始边对齐;而只显示图像的标签则水平居中对齐。
还可以指定文本相对于图像的位置。默认情况下,文本位于图像的结尾边上,文本和图像都垂直对齐。 还可以使用 setIconTextGap 方法指定文本和图像之间应该出现多少像素。默认情况下为 4 个像素。
此处创建一个带图标的标签。
public class TestJLabel extends JFrame implements Icon {
private int width;
private int height;
public TestJLabel(){
}
public TestJLabel(int width, int height) {
this.width = width;
this.height = height;
}
public void init(){
TestJLabel icon = new TestJLabel(15, 15);
// 将图标放在标签上
JLabel jLabel = new JLabel("icon", icon, SwingConstants.CENTER);
Container container = getContentPane();
container.add(jLabel);
this.setVisible(true);
this.setBounds(200,200,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJLabel().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return height;
}
}
创建带图像的标签
public class TestIcon extends JFrame{
public TestIcon() {
JLabel label = new JLabel("TestIcon");
// 获取图片的地址
URL url = TestIcon.class.getResource("StockSnap_YCPR5MQTRP.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
this.setVisible(true);
this.setBounds(200,200,500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestIcon();
}
}
JPanel
public class JPanel
extends JComponent
implements Accessible
JPanel 是一般轻量级容器。
public class TestJPanel extends JFrame {
public void init(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,5,5));
container.setBackground(Color.WHITE);
JPanel panel1 = new JPanel(new GridLayout(2,2,1,1));
JPanel panel2 = new JPanel(new GridLayout(1,2,2,2));
for (int i = 0; i < 4; i++) {
JButton button = new JButton("button" + i);
panel1.add(button);
}
for (int i = 0; i < 2; i++) {
JButton button = new JButton("button-" +i);
panel2.add(button);
}
container.add(panel1);
container.add(panel2);
this.setVisible(true);
this.setBounds(100,100,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJPanel().init();
}
}
JButton
public class JButton
extends AbstractButton
implements Accessible
实现图像按钮
public class TestJButton extends JFrame{
public void init(){
Container container = this.getContentPane();
container.setLayout(new BorderLayout());
container.setBackground(Color.WHITE);
URL url = TestJButton.class.getResource("StockSnap_YCPR5MQTRP.jpg");
ImageIcon imageIcon = new ImageIcon(url);
JButton button = new JButton();
button.setSize(100,100);
button.setIcon(imageIcon);
container.add(button,BorderLayout.CENTER);
this.setVisible(true);
this.setBounds(100,100,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJButton().init();
}
}
实现单选框
public class TestJButton01 extends JFrame {
public void init(){
Container container = this.getContentPane();
// 单选框
JRadioButton button01 = new JRadioButton("JRadioButton01");
JRadioButton button02 = new JRadioButton("JRadioButton02");
JRadioButton button03 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,分组,一个组中只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(button01);
group.add(button02);
group.add(button03);
container.add(button01,BorderLayout.CENTER);
container.add(button02,BorderLayout.NORTH);
container.add(button03,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJButton01().init();
}
}
实现复选框
public class TestJButton02 extends JFrame {
public void init(){
Container container = this.getContentPane();
// 由于是复选框,所以不用加到一个组里
JCheckBox checkBox01 = new JCheckBox("JCheckBox01");
JCheckBox checkBox02 = new JCheckBox("JCheckBox02");
JCheckBox checkBox03 = new JCheckBox("JCheckBox03");
container.add(checkBox01,BorderLayout.CENTER);
container.add(checkBox02,BorderLayout.NORTH);
container.add(checkBox03,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJButton02().init();
}
}
列表
列表有下拉框、列表框这两种。
下拉框,一般用于 选择(地区、国家),或者一些单个选项。
列表框,一般用于展示信息,一般是动态扩容。
下拉框
public class TestComboboxDemo01 extends JFrame {
public void init(){
Container container = this.getContentPane();
JComboBox comboBox = new JComboBox();
comboBox.addItem("排行榜");
comboBox.addItem("歌手磅");
comboBox.addItem("新歌榜");
container.add(comboBox);
this.setVisible(true);
this.setSize(202,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01().init();
}
}
列表框
public class TestComboboxDemo02 extends JFrame {
public void init(){
Container container = this.getContentPane();
Vector vector = new Vector();
JList list = new JList(vector);
vector.add("Tom");
vector.add("23");
vector.add("male");
container.add(list);
this.setVisible(true);
this.setBounds(100,100,200,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02().init();
}
}
文本框
文本框分为好几种,有普通的文本框、密码框、文本域等。
文本框,只有一行,不能换行。
public class TestTextField01 extends JFrame {
public void init(){
Container container = this.getContentPane();
JTextField textField = new JTextField("hello");
JTextField textField1 = new JTextField("world",10);
container.add(textField,BorderLayout.NORTH);
container.add(textField1,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(200,200,500,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextField01().init();
}
}
文本域,多行,可以换行。
public class TestTextField02 extends JFrame {
public void init(){
Container container = this.getContentPane();
JTextArea textArea = new JTextArea();
textArea.setText("hello world");
textArea.setColumns(20);
container.add(textArea);
this.setVisible(true);
this.setBounds(200,200,500,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextField02().init();
}
}
密码框
public class TestTextField03 extends JFrame {
public void init(){
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setBounds(200,200,500,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextField03().init();
}
}