第十八章Swing

3.Swing
3.1、窗口&面板
代码:

package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 上午 08:31
 * @desc Swing
 **/
public class JFrameDemo1 {
    public static void main(String[] args) {
        new JFrameDemo1().init();
    }
//    init()初始化。
    public void init(){
        JFrame jFrame = new JFrame("这是我们的JFrame窗口。");
        jFrame.setVisible(true);
        jFrame.setBackground(Color.BLUE);
        jFrame.setBounds(377, 377, 277, 277);

//        设置文字。 JLabel
        JLabel jLabel = new JLabel("Welcome to EdwinD's Way.");
        jFrame.add(jLabel);

//        关闭事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
 

框框2:标签居中。

package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 上午 09:05
 * @desc Swing
 **/
public class JFrameDemo2 {
    public static void main(String[] args) {
        MyJFrame2 myJFrame2 = new MyJFrame2();
        myJFrame2.setTitle("这是我们的JFrame窗口2.0");
        myJFrame2.init();
    }
}
class MyJFrame2 extends JFrame {
    public void init(){
        setBounds(377, 377, 277, 277);
        setVisible(true);
        JLabel jLabel = new JLabel("Welcome to EdwinD's Way-2.0.");
        this.add(jLabel);
//        label居中,设置水平对齐
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//        获得一个容器,
        Container contentPane = this.getContentPane();
        contentPane.setBackground(new Color(77,77,77));
//        关闭事件
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
 

3.2、弹窗
代码:

package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 * @author EdwinD
 * @create 2020.08.19 上午 09:46
 * @desc 弹窗
 **/
//主窗口
public class DialogDemo1 extends JFrame {
    public static void main(String[] args) {
        new DialogDemo1();
    }

    public DialogDemo1() {
        this.setVisible(true);
        this.setBounds(277, 277, 577, 577);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

//        JFrame 存放东西,容器
        Container container = this.getContentPane();
//        绝对布局
        container.setLayout(null);
//        按钮
        JButton jButton = new JButton("弹窗显灵!");
        jButton.setBounds(100, 100, 100, 70);
//       点击此按钮,会有弹窗。
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                弹窗
                new MyDialogDemo();
            }
        });
        container.add(jButton);
    }
}

//弹窗
class MyDialogDemo extends JDialog {
    public MyDialogDemo() {
        this.setVisible(true);
        this.setBackground(Color.BLACK);
        this.setBounds(100, 100, 300, 200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = this.getContentPane();
        contentPane.setLayout(null);
        contentPane.add(new Label("跟着老秦学Java"));
    }
}
 

3.3、标签

Label

new JLabel("……");
  • 1

图标’s Icon

代码:

package com.edwin.lesson04;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 上午 10:48
 * @desc 标签。图标需要实现类,Frame继承等。
 **/
public class IconDemo1 extends JFrame implements Icon {
    private int width;
    private int height;

    public static void main(String[] args) {
        new IconDemo1().init();
    }

    //无参构造
    public IconDemo1() {
    }
    //有参构造
    public IconDemo1(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public void init() {
        IconDemo1 iconDemo1 = new IconDemo1(17, 17);
        JLabel label = new JLabel("icontest", iconDemo1, SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(label);

        this.setVisible(true);
        this.setBounds(277, 277, 677, 677);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    @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;
    }
}
 

3.4、面板
JPanel

试用Scroll面板,带滚轮,可伸缩。

代码:

package com.edwin.lesson05;

import javax.swing.*;
import java.awt.*;

/**
 * @author EdwinD
 * @create 2020.08.19 下午 05:29
 * @desc
 **/
public class JScrollDemo extends JFrame {
    public static void main(String[] args) {
        new JScrollDemo();
    }

    public JScrollDemo() {
        Container container = this.getContentPane();

//        文本域
        JTextArea textArea = new JTextArea();
        textArea.setText("Welcome to EdwinD's Java way.It's really a long and hard way for us to go." +
                "But we must hand on it and try our best to do.");
//        Scroll面板,带滚轮,可伸缩。
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

        this.setVisible(true);
        this.setBounds(277, 277, 300, 300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}

3.5、按钮
图片按钮:

将按钮的内容设置为一个图片,点击后是按钮的效果:

package com.edwin.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
 * @author EdwinD
 * @create 2020.08.19 下午 05:47
 * @desc 按钮
 **/
public class JButton1 extends JFrame {
    public static void main(String[] args) {
        new JButton1();
    }

    public JButton1() {
        Container container = this.getContentPane();
//        将一个图片,变为图标
        URL url = JButton1.class.getResource("Study.jpg");
        Icon icon = new ImageIcon(url);

//        把图片放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");

//        add
        container.add(button);

        this.setVisible(true);
        this.setBounds(277, 277, 500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

3.6、列表
下拉框

(1) JComoBox类
初次使用 Swing 中的下拉列表框时,会感觉到 Java 中的下拉列表框与 Windows 操作系统中的下拉列表框类似,但是实质上两者并不相同,Swing 中的下拉列表框不仅支持用户从中选择选项,同时还支持用户编辑项目中的内容。

下拉列表框是一个带条状的显示区,它具有下拉功能。在下拉列表框的右边存在一个倒三角形的按钮,当用户单击该按钮时,下拉列表框中的选项将会以列表的形式显示出来。

Swing 中的下拉列表框使用 JComboBox 类对象来表示,它是 javax.swing.JComponent 类的子类。

JComboBox 的常用构造方法如下。
① public JComboBox()
② public JComboBox(ComboBoxMode dataModel)
③ public JComboBox(Object[] arrayData)
④ public JComboBox(Vector vector)

在初始化下拉列表框时,既可以选择同时指定下拉列表框中的选项内容,也可以在程序中使用其他方法设置下拉列表框中的内容。下拉列表框中的内容可以被封装在 ComboBoxModel 类型、数组或者 Vector 类型中。

(2) JCcomboBox 模型
在开发程序时,一般将下拉列表框中的选项封装在 ComboBoxModel 类型中。ComboBoxModel 类型为接口,它代表一般模型,可以自定义一个类实现该接口,然后在初始化 JComboBox 对象时向上转型为 ComboBoxModel 接口类型,但是必须实现下面的两个方法:

① public void setSelectedItem(Object item)
② public Object getSelectedItem()

其中,setSelectedItem() 方法是设置下拉列表框的选中项,getSelectedItem() 方法用于返回下拉列表框中的选中项,有了这两个方法,我们就可以轻松地对下拉列表框中的项目进行操作。

自定义的这个类除了可以实现该接口外,还可以继承 AbstractListModel 类,在该类中也有两个操作下拉列表框的重要方法:

① getSize():返回列表的长度。
② getElementAt(in index):返回指定索引处的值。

下面让我们通过一个实例来更好地理解和使用下拉列表框。
要求:在项目中创建 JListDemoFrame 类,该类继承 JFrame 类成为窗体组件,然后向该窗体中添加下拉列表框组件。
 

代码:

package com.edwin.lesson6;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 下午 06:27
 * @desc 下拉框
 **/
public class TextCombobox1 extends JFrame {
    public static void main(String[] args) {
        new TextCombobox1();
    }

    public TextCombobox1() {
        Container container = this.getContentPane();

        JComboBox status = new JComboBox();
        status.addItem("起始默认字");
        status.addItem("正在热映");
        status.addItem("即将上映");
        status.addItem("已下架");

        container.add(status);

        this.setVisible(true);
        this.setBounds(277, 277, 500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

3.7、文本框
文本框


代码:

package com.edwin.lesson6;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 下午 07:15
 * @desc 文本框
 **/
public class TextText1 extends JFrame {
    public static void main(String[] args) {
        new TextText1();
    }

    public TextText1() {
        Container container = this.getContentPane();

        JTextField textField1 = new JTextField("Hello");
        JTextField textField2 = new JTextField("World!",20);//规定最多20个字符。

        container.add(textField1,BorderLayout.NORTH);
        container.add(textField2,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(277, 277, 500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

文本域
同Lesson5里面的文本域

代码:

package com.edwin.lesson05;
import javax.swing.*;
import java.awt.*;
/**
 * @author EdwinD
 * @create 2020.08.19 下午 05:29
 * @desc
 **/
public class JScrollDemo extends JFrame {
    public static void main(String[] args) {
        new JScrollDemo();
    }

    public JScrollDemo() {
        Container container = this.getContentPane();

//        文本域
        JTextArea textArea = new JTextArea();
        textArea.setText("Welcome to EdwinD's Java way.It's really a long and hard way for us to go." +
                "But we must hand on it and try our best to do.");
//        Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

        this.setVisible(true);
        this.setBounds(277, 277, 300, 300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值