码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA学习笔记(四十九)- Swing相关组件

时间:2015-04-04 09:10:14      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:jframe组件   swing   

JFrame组件

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;

/*
 * Swing是在AWT基础上的扩展
 * javax.swing包及其扩展包,组件的命名多以J开关
 * 
 * JFrame组件
 */
public class Test08 {
    public static void main(String[] args) {
        JFrame frame=new JFrame("我的窗体");
        frame.setSize(200,200);
        frame.setLocationRelativeTo(null);
        //frame.getLayeredPane().setBackground(Color.red);
        frame.setBackground(Color.blue);
        JPanel pnl=(JPanel) frame.getContentPane();
        pnl.setOpaque(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

JButton组件

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

/*
 * JButton组件
 */
public class Test09 extends JFrame {

    public Test09(String title){
        super(title);
        init();
    }

    public void init(){
        JButton btn1=new JButton("按钮1");
        JButton btn2=new JButton("按钮2");
        JButton btn3=new JButton("按钮3");
        add(btn1);
        add(btn2);
        add(btn3);
        setSize(200,200);
        setLocationRelativeTo(null);
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //setExtendedState(MAXIMIZED_BOTH);//最大化窗体
        setResizable(false); //禁止改变窗体大小
        setVisible(true);
    }

    public static void main(String[] args) {
        new Test09("我的窗体");
    }
}

JLabel组件

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/*
 * JLabel组件
 */
public class Test10 {
    public static void main(String[] args) {
        JFrame frame=new JFrame("我的窗体");
        JLabel lbl=new JLabel("你是我的小呀小苹果");
        JButton btn1=new JButton("按钮1");
        JButton btn2=new JButton("按钮2");
        JButton btn3=new JButton("按钮3");

        frame.setLayout(null);
        frame.setSize(300,300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(lbl);
        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        lbl.setBounds(3, 5, 200, 30);
        btn1.setBounds(20, 30, 80, 30);
        btn2.setBounds(60, 60, 80, 30);
        btn3.setBounds(100, 100, 80, 30);

        frame.setVisible(true);
    }
}

JMenuBar JMenu JMenuItem

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;

/*
 * JMenuBar  JMenu JMenuItem
 */
public class Test11 {
    public static void main(String[] args) {
        JFrame frame=new JFrame("我的窗体");
        //创建菜单栏
        JMenuBar bar=new JMenuBar();
        //创建菜单
        JMenu menu1=new JMenu("文件");
        JMenu menu2=new JMenu("编辑");
        //创建菜单项
        JMenuItem item1=new JMenuItem("新建");
        JMenuItem item2=new JMenuItem("关闭");
        JMenuItem item3=new JMenuItem("保存");
        JMenuItem item4=new JMenuItem("复制");
        JMenuItem item5=new JMenuItem("粘贴");
        menu1.add(item1);
        menu1.add(item2);
        menu1.add(item3);
        menu2.add(item4);
        menu2.add(item5);
        bar.add(menu1);
        bar.add(menu2);
        frame.add("North",bar);

        JTextArea txt=new JTextArea();
        frame.add(txt);

        frame.setSize(300,300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}

ImageIcon 设置图标

import java.awt.Dimension;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/*
 * 设置图标
 * 相关的类
 * java.awt.Image图像类,抽象类
 * javax.swing.Icon 图标,接口
 * javax.swing.ImageIcon 图标类,实现Icon接口
 */
public class Test12 {
    public static void main(String[] args) {
        JFrame frame=new JFrame("My Frame");

        /*
         * 1.设置窗体图标
         */
        //创建ImageIcon图标
        ImageIcon icon=new ImageIcon("images/qq.jpg");
        //由ImageIcon获得Image
        Image image=icon.getImage();
        //设置窗体的图标
        frame.setIconImage(image);

        /*
         * 设置按钮图标
         */
        Icon icon2=new ImageIcon("images/heihei.gif");
        JButton btn=new JButton("点我", icon2);
        btn.setSize(200, 50);
        frame.add("North",btn);

        /*
         * 设置JLabel显示图像
         */
        Icon icon3=new ImageIcon("images/dog.jpg");
        JLabel lbl=new JLabel("好可爱的小狗啊", icon3, JLabel.LEFT);
        frame.add(lbl,"South");


        frame.setSize(300,300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

JTextfied JPasswordField

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/*
 * JTextfied JPasswordField
 * 
 * setSize()和setPreferredSize()
 * 如果使用布局管理器,则setSize()无效,此时可以使用setPreferredSize()
 * 如果未使用布局管理器,则可以使用setSize()
 */
public class Test13 extends JFrame{
    JPanel[] pnls=new JPanel[3];

    public Test13(String title){
        super(title);
        init();
    }

    public void init(){
        for(int i=0;i<3;i++){
            pnls[i]=new JPanel();
            add(pnls[i]);
        }
        JLabel lblUsername=new JLabel("用户名:");
        JTextField txtUsername=new JTextField(10);
        txtUsername.setSize(100, 30);
        txtUsername.setPreferredSize(new Dimension(100, 30));
        pnls[0].add(lblUsername);
        pnls[0].add(txtUsername);

        JLabel lblPwd=new JLabel("密码:");
        JPasswordField txtPwd=new JPasswordField(10);
        pnls[1].add(lblPwd);
        pnls[1].add(txtPwd);

        JButton btnLogin=new JButton("登陆");
        JButton btnCancel=new JButton("取消");
        pnls[2].add(btnLogin);
        pnls[2].add(btnCancel);

        //pnls[0].setSize(200,50);
        pnls[0].setPreferredSize(new Dimension(100, 30));



        setLayout(new GridLayout(3, 0));
        setSize(300,200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Test13("用户登陆");
    }
}

JCheckBox、JRadioButton

import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/*
 * JCheckBox、JRadioButton
 */
public class Test01 extends JFrame{
    //定义复选框
    JCheckBox hobby1=new JCheckBox("学习JAVA");
    JCheckBox hobby2=new JCheckBox("吃月饼");
    JCheckBox hobby3=new JCheckBox("泡妞",true);

    //定义单选按钮
    JRadioButton male=new JRadioButton("男");
    JRadioButton female=new JRadioButton("女", true);

    //定义标签
    JLabel lblHobby=new JLabel("您的爱好:");
    JLabel lblSex=new JLabel("您的性别:");

    public Test01(String title){
        super(title);
        //将单选按钮添加到同一组中,实现互斥
        ButtonGroup groupSex=new ButtonGroup();
        groupSex.add(male);
        groupSex.add(female);

        setLayout(new GridLayout(8, 0));
        add(lblHobby);
        add(hobby1);
        add(hobby2);
        add(hobby3);
        add(lblSex);
        add(male);
        add(female);

        JPanel pnl=new JPanel();
        pnl.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
        JButton btnOK=new JButton("确定");
        JButton btnCancel=new JButton("取消");
        pnl.add(btnOK);
        pnl.add(btnCancel);
        add(pnl);

        setSize(200, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Test01("我的窗体");
    }
}

Swing窗体由不同的层组成

import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

/*
 * 设置背景图片
 * 
 * Swing窗体由不同的层组成
 * 由深到浅依次为:RootPane、LayoutPane、ContentPane、MenuBar、GlassPane
 */
public class Test02 {
    public static void main(String[] args) {
        JFrame frame=new JFrame("我的窗体");

        //1.创建背景图片
        ImageIcon bg=new ImageIcon("images/bg.jpg");
        //2.将背景图片放到JLable中
        JLabel lblBg=new JLabel(bg);
        lblBg.setSize(bg.getIconWidth(), bg.getIconHeight());//设置面板的宽和高
        //3.将JLable添加到LayoutPane面板中
        JLayeredPane layoutPane=frame.getLayeredPane();//获取窗体的布局面板
        //将背景Label添加到布局面板中,将放置于布局面板的最底层
        layoutPane.add(lblBg, new Integer(Integer.MIN_VALUE));
        //4.将ContentPane设置为透明,否则背景出不来
        //frame.setBackground(Color.red);
        JPanel pnlContent=(JPanel) frame.getContentPane();//获取窗体的内容面板
        pnlContent.setOpaque(false);//设置为透明

        frame.setSize(300, 300);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

JTabbedPane选项卡

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

/*
 * JTabbedPane选项卡
 */
public class Test03 {
    public static void main(String[] args) {
        JFrame frame=new JFrame("Java API");
        JTabbedPane tab=new JTabbedPane();//创建一个选项卡
        frame.add(tab);

        JPanel pnl1=new JPanel();
        JTextField txt1=new JTextField(10);
        JButton btn1=new JButton("按钮1");
        pnl1.add(txt1);
        pnl1.add(btn1);

        JPanel pnl2=new JPanel();
        JTextField txt2=new JTextField(10);
        JButton btn2=new JButton("按钮2");
        pnl2.add(txt2);
        pnl2.add(btn2);

        JPanel pnl3=new JPanel();
        JTextField txt3=new JTextField(10);
        JButton btn3=new JButton("按钮3");
        pnl3.add(txt3);
        pnl3.add(btn3);

        tab.add("目录", pnl1);
        tab.add("索引", pnl2);
        //tab.add("搜索", pnl3);
        tab.addTab("搜索", pnl3);

        frame.setSize(300, 300);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

JTable、JScrollPane、DefaultTableModel

/*
 * JTable、JScrollPane、DefaultTableModel
 * JOptionPane
 * 
 * JComboBox下拉列表
 * ItemListener监听器
 */
public class Test04 extends MouseAdapter implements ActionListener{

    JFrame frame = new JFrame("学生信息");
    String[] titles = { "姓名", "性别", "年龄", "地址", "爱好" };
    DefaultTableModel dtm=null;
    JTable table=null;
    JLabel lblUsername=new JLabel("用户名:");
    JLabel lblAddress=new JLabel("地址:");
    JTextField txtUsername=new JTextField(10);
    JTextField txtAddress=new JTextField(10);

    public static void main(String[] args) {
        Test04 test=new Test04();
        //test.test1();
        test.test3();
    }

    public static void test1(){
        JFrame frame = new JFrame("学生信息");
        String[] titles = { "姓名", "性别", "年龄", "地址", "爱好" };
        Object[][] userInfo = { { "赵小涛", "男", 20, "南京", "女人" },
                { "赵中涛", "男", 20, "南京", "男人" }, { "赵大涛", "男", 20, "南京", "人妖" } };
        //创建JTable用来显示或编辑二维表数据
        JTable table = new JTable(userInfo, titles);
        // 创建JScrollPane视图,用来封装显示JTable中的数据
        JScrollPane scrollpane = new JScrollPane(table,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); //为表格添加滚动条

        frame.add(scrollpane);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    //使用DefaultTableModel动态更新数据
    public void test2(){
        JFrame frame = new JFrame("学生信息");
        Object[][] userInfo = { { "赵小涛", "男", 20, "南京", "女人" },
                { "赵中涛", "男", 20, "南京", "男人" }, { "赵大涛", "男", 20, "南京", "人妖" } };
        //创建DefaultTableModel,用来存储JTable中的数据
        dtm=new DefaultTableModel(userInfo, titles);
        JTable table = new JTable(dtm);
        JScrollPane scrollpane = new JScrollPane(table);

        JButton btnChange=new JButton("变");
        btnChange.addActionListener(this);

        frame.add("South",btnChange);
        frame.add(scrollpane);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }


    //获取选中的记录
    public void test3(){
        Object[][] userInfo = { { "赵小涛", "男", 20, "南京", "女人" },
                { "赵中涛", "男", 20, "南京", "男人" }, { "赵大涛", "男", 20, "南京", "人妖" } };
        //创建DefaultTableModel,用来存储JTable中的数据
        dtm=new DefaultTableModel(userInfo, titles);
        table = new JTable(dtm);
        JScrollPane scrollpane = new JScrollPane(table);

        JButton btnChange=new JButton("变");
        JButton btnDelete=new JButton("删除");
        JPanel pnlSouth=new JPanel();
        pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
        pnlSouth.add(btnChange);
        pnlSouth.add(btnDelete);

        //绑定事件
        btnChange.addActionListener(this);
        btnDelete.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int choice=JOptionPane.showConfirmDialog(frame, "确定要删除吗?", "提示", JOptionPane.OK_CANCEL_OPTION);
                if(choice==0){  //0表示确定
                    System.out.println("执行删除操作");
                }
            }
        });

        JPanel pnlUserInfo=new JPanel();
        pnlUserInfo.setLayout(new GridLayout(2, 2));

        txtUsername.setEnabled(false);
        txtAddress.setEnabled(false);
        pnlUserInfo.add(lblUsername);
        pnlUserInfo.add(txtUsername);
        pnlUserInfo.add(lblAddress);
        pnlUserInfo.add(txtAddress);


        //为JTable绑定事件
        table.addMouseListener(this);

        frame.add("North",pnlUserInfo);
        frame.add("Center",scrollpane);
        frame.add("South",pnlSouth);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    //点击按钮更新表格中的数据
    @Override
    public void actionPerformed(ActionEvent e) {
        Object[][] userInfo2 = { { "tom", "男", 20, "南京", "女人" },
                { "jack", "男", 20, "南京", "男人" }};
        dtm.setDataVector(userInfo2, titles);
    }

    //将当前选中行的记录显示在文本框中
    @Override
    public void mouseClicked(MouseEvent e) {
        int row=table.getSelectedRow();
        String username=(String) table.getValueAt(row, 0);
        String address=(String) table.getValueAt(row, 3);
        txtUsername.setText(username);
        txtAddress.setText(address);
    }
}

JAVA学习笔记(四十九)- Swing相关组件

标签:jframe组件   swing   

原文地址:http://blog.csdn.net/wangzi11322/article/details/44870753

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!