Java的GUI编程---Swing介绍
Swing窗口,面板package com.akita.lesson03;import javax.swing.*;import java.awt.*;public class JFrameDemo02 {public static void main(String[] args) {new MyJFrame02().init();}}class MyJFrame02 extends JFrame
·
Swing
窗口,面板
package com.akita.lesson03;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJFrame02().init();
}
}
class MyJFrame02 extends JFrame {
public void init() {
this.setBounds(200, 200, 500, 500);
//获得一个容器,只有这样设置颜色才会生效
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.CYAN);
//设置文字
JLabel jLabel = new JLabel("akita");
this.add(jLabel);
//让文本标签居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
setVisible(true);
}
}
弹窗
package com.akita.lesson03;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo() {
this.setSize(500, 350);
this.setLocation(300, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西容器
Container contentPane = this.getContentPane();
//绝对布局
contentPane.setLayout(null);
//按钮
JButton jButton = new JButton("点击弹出对话框");
jButton.setBounds(30, 30, 200, 50);
//点击这个按钮弹出一个弹窗
jButton.addActionListener(new ActionListener() { //监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDD();
}
});
contentPane.add(jButton);
this.setVisible(true);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogDD extends JDialog {
public MyDialogDD() {
this.setBounds(100, 100, 500, 500);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
Label akita = new Label("akita");
akita.setSize(50, 50);
contentPane.add(akita);
this.setVisible(true);
}
}
标签
JLabel
JLabel jLabel = new JLabel("标签");
- Icon
package com.akita.lesson03;
import javax.swing.*;
import java.awt.*;
//图标是一个接口,需要实现类,并继承JFrame
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo() {
}
public IconDemo(int width, int height) {
this.width = width;
this.height = height;
}
public void init() {
IconDemo iconDemo = new IconDemo(15, 15);
//图标要放在标签上
JLabel jLabel = new JLabel("icon_test", iconDemo, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
this.setBounds(200,200,400,340);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x, y, width, height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
ImageIcon
package com.akita.lesson03;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo() {
JLabel akita = new JLabel("akita");
//获取图片地址
URL url = ImageIconDemo.class.getResource("icon.png");
ImageIcon imageIcon = new ImageIcon(url);
akita.setIcon(imageIcon);
akita.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(akita);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(200, 200, 500, 500);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
面板
JPanel
package com.akita.lesson04;
import com.akita.lesson03.JFrameDemo;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo() {
this.setBounds(100, 100, 400, 400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(new GridLayout(2, 2, 10, 10));
JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
JPanel jPanel2 = new JPanel(new GridLayout(1, 2));
JPanel jPanel3 = new JPanel(new GridLayout(2, 1));
JPanel jPanel4 = new JPanel(new GridLayout(3, 2));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("2"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
container.add(jPanel1);
container.add(jPanel2);
container.add(jPanel3);
container.add(jPanel4);
this.setVisible(true);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
运行结果
JScrollPane
package com.akita.lesson04;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container contentPane = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("akita");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
jScrollPane.setBackground(new Color(1, 1, 1));
contentPane.add(jScrollPane);
this.setVisible(true);
this.setBounds(100, 100, 300, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
运行效果
按钮
带图片的按钮
package com.akita.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
//把图片转化成图标
URL url = JButtonDemo01.class.getResource("icon.png");
ImageIcon icon = new ImageIcon(url);
//把这个图标放置在按钮上
JButton jButton = new JButton();
jButton.setIcon(icon);
jButton.setToolTipText("akita");
//add
container.add(jButton);
setVisible(true);
setBounds(100, 100, 200, 200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
运行效果
单选按钮
package com.akita.lesson04;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo02 extends JFrame {
public JButtonDemo02() {
Container container = this.getContentPane();
//单选框
JRadioButton radioButton1 = new JRadioButton("radioButton1");
JRadioButton radioButton2 = new JRadioButton("radioButton2");
JRadioButton radioButton3 = new JRadioButton("radioButton3");
//由于单选框只能选择一个,所以我们通常对他们进行分组
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
buttonGroup.add(radioButton3);
//add
container.add(radioButton1, BorderLayout.CENTER);
container.add(radioButton2, BorderLayout.NORTH);
container.add(radioButton3, BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(200, 200, 500, 350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
运行效果
多选按钮
package com.akita.lesson04;
import javax.swing.*;
import java.awt.*;
public class JButtonDemo03 extends JFrame {
public JButtonDemo03() {
Container container = this.getContentPane();
//多选框
JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
container.add(jCheckBox1, BorderLayout.NORTH);
container.add(jCheckBox2, BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(200, 200, 400, 250);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
运行效果
列表
下拉框
package com.akita.lesson05;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() {
Container container = this.getContentPane();
container.setBackground(new Color(1, 1, 1));
JComboBox jComboBox = new JComboBox();
jComboBox.addItem("love");
jComboBox.addItem("lee");
jComboBox.addItem("wzz");
container.add(jComboBox);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(200, 200, 400, 250);
this.setVisible(true);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
运行效果
列表框
package com.akita.lesson05;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02() {
Container container = this.getContentPane();
//生成列表内容
String[] contents = {"akita", "wzz", "lee"};
JList jList = new JList(contents);
container.add(jList);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(200, 200, 500, 350);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
运行效果
应用场景
- 对于下拉框可以设置选择地区,或者一些单个选项
- 列表框,用于展示信息,一般是动态扩容
文本框
文本框
package com.akita.lesson05;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container = this.getContentPane();
JTextField jTextField1 = new JTextField("hello");
JTextField jTextField2 = new JTextField("world", 20);
container.add(jTextField1, BorderLayout.NORTH);
container.add(jTextField2, BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(200, 200, 400, 250);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
运行效果
密码框
package com.akita.lesson05;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo02 extends JFrame {
public TestTextDemo02() {
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(200, 200, 400, 350);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
运行效果
文本域
package com.akita.lesson05;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo03 extends JFrame {
public TestTextDemo03() {
Container container = this.getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("hello world!");
//scroll面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(300, 300, 400, 250);
}
public static void main(String[] args) {
new TestTextDemo03();
}
}
运行效果
更多推荐
已为社区贡献2条内容
所有评论(0)