我为大家整理了AWT之后的swing包的使用
1. 初步构建了一个基础的swing框架
package com.swing;
import javax.swing.*;
import java.awt.*;
public class JframeDemo01 {
public static void main(String[] args) {
new MyJframe().init();
}
}
class MyJframe extends JFrame{
public void init(){
this.setBounds(10,10,500,500);
this.setVisible(true);
super.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel label = new JLabel("你好");
this.add(label);
label.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container container = getContentPane();
container.setBackground(Color.yellow);
}
}
2 .弹框的显示
JDialog,用来被弹出,默认就有关闭事件!
package com.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogDemo {
public static void main(String[] args) {
new MyJframe1();
}
}
//定义初始窗口
class MyJframe1 extends JFrame{
public MyJframe1(){
super("会有一个弹窗");
this.setVisible(true);
this.setSize(700,500);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西,容器
Container container = this.getContentPane();
container.setLayout(null);
//创建一个按钮
JButton button = new JButton("点击我会有一个弹窗");
button.setBounds(30,30,100,50);
//点击这个按钮,弹出一个窗口
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
container.add(button);
}
}
//定义弹窗
class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true);
this.setBounds(100,100,500,500);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//定义一个容器,里面放一段话
Container container = this.getContentPane();
//绝对定位
container.setLayout(null);
container.add(new Label("你好,很高兴认识你"));
}
}
3.标签以及图片的显示
package com.swing;
import javax.swing.*;
import java.awt.*;
//图标,需要实现类,Frame继承
public class LabelTest extends JFrame implements Icon {
private int width;
private int height;
public LabelTest() {} //无参构造
public LabelTest(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
//设置图标样式
g.fillOval(20,20,500,500);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
public void init(){
LabelTest labelTest = new LabelTest(10,15);
JLabel jLabel = new JLabel("icontest",labelTest,SwingConstants.CENTER);
Container container =this.getContentPane();
container.add(jLabel);
setVisible(true);
setBounds(20,20,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new LabelTest().init();
}
}
4. 面板 JPanel
package com.swing;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo {
public static void main(String[] args) {
new MyImage();
}
}
class MyImage extends JFrame{
public MyImage() throws HeadlessException {
super("我的图片");
//获取图片地址
JLabel jLabel = new JLabel("myIcon");
URL url = MyImage.class.getResource("OIP.jpg");
ImageIcon imageIcon = new ImageIcon(url);
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(jLabel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(500,500);
}
}
package com.swing;
import javax.swing.*;
import java.awt.*;
public class JPanelTest {
public static void main(String[] args) {
new JPane1Demo();
}
}
class JPane1Demo extends JFrame {
public JPane1Demo() {
super("划分区域");
//定义一个容器
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));
//定义面板和面板内部的划分
JPanel panel1 = new JPanel(new GridLayout(1,3));
JPanel panel2 = new JPanel(new GridLayout(1,2));
JPanel panel3 = new JPanel(new GridLayout(2,2));
JPanel panel4 = new JPanel(new GridLayout(2,2));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
setVisible(true);
setBounds(10,10,200,200);
}
}
滚动体 JScroll
package com.swing;
import javax.swing.*;
import java.awt.*;
public class JScrollTest extends JFrame {
public static void main(String[] args) {
new JScrollTest();
}
public JScrollTest(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,10);
textArea.setText("Hello world");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(textArea);
container.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
6. 按钮
单选框按钮
package com.swing;
import javax.swing.*;
import java.awt.*;
public class ButtonText extends JFrame {
public static void main(String[] args) {
//单选框按钮
new ButtonText();
}
public ButtonText(){
JRadioButton jButton1 = new JRadioButton("按钮1");
JRadioButton jButton2 = new JRadioButton("按钮2");
JRadioButton jButton3 = new JRadioButton("按钮3");
//设置按钮组
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jButton1);
buttonGroup.add(jButton2);
buttonGroup.add(jButton3);
Container container = this.getContentPane();
container.add(jButton1,BorderLayout.CENTER);
container.add(jButton2,BorderLayout.SOUTH);
container.add(jButton3,BorderLayout.NORTH);
this.setVisible(true);
this.setBounds(10,10,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
多选框按钮
package com.swing;
import javax.swing.*;
import java.awt.*;
public class ButtonText2 extends JFrame {
//多选按钮
public static void main(String[] args) {
new ButtonText2();
}
public ButtonText2(){
Container container = this.getContentPane();
//定义多选框
JCheckBox jCheckBox1 = new JCheckBox("按钮1");
JCheckBox jCheckBox2 = new JCheckBox("按钮2");
container.setLayout(new FlowLayout(FlowLayout.CENTER));
container.add(jCheckBox1);
container.add(jCheckBox2);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(500,500);
}
}
7. 列表
package com.swing;
import javax.swing.*;
import java.awt.*;
public class TestComboboxDemo01 extends JFrame {
public static void main(String[] args) {
//下拉框
new TestComboboxDemo01();
}
public TestComboboxDemo01() {
Container container = this.getContentPane();
JComboBox status =new JComboBox();
status.addItem(null);
status.addItem("状态1");
status.addItem("状态2");
status.addItem("状态3");
container.add(status);
this.setVisible(true);
this.setBounds(10,10,500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
package com.swing;
import javax.swing.*;
import java.awt.*;
public class ListDemo1 extends JFrame {
public static void main(String[] args) {
//单选框按钮
new ListDemo1();
}
public ListDemo1(){
Container container = this.getContentPane();
//生成列表的内容
String[] contents = {"1","2","3","4"};
//列表中需要放入内容
JList jList = new JList(contents);
container.add(jList);
this.setVisible(true);
this.setBounds(10,10,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
8.密码框
package com.swing;
import javax.swing.*;
import java.awt.*;
public class PasswordDemo01 extends JFrame {
public static void main(String[] args) {
new PasswordDemo01();
}
public PasswordDemo01(){
//密码框
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
//默认的密码符号是圆点,我们将其设置为*
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setBounds(10,10,500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}