Java程序设计——JTabbedPane选项卡面板(Swing UI )

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Swing extends JFrame {
    private JPanel panel1,panel2;
    private JTabbedPane tabbedPane;
    private JMenuBar menuBar;
    private JMenu menuFile,menuHelp;
    private JMenuItem itemNew,itemOpen,itemSave,itemExit,itemAbout,itemJava;
    private JToolBar toolBar;
    private JColorChooser colorChooser;
    private JButton btnSave,btnPre,btnDown,btnSel,btnDel,
    btnOk,btnMeg,btnOpt,btnInput,btnChange;
    private JOptionPane optionPane;
    private JDialog dialogColor;
    public Swing(){
        super("选项卡的使用");

        tabbedPane = new JTabbedPane();
        panel1 = new JPanel(new BorderLayout());
        panel2 = new JPanel(new FlowLayout());
        tabbedPane.add("选项卡1",panel1);
        tabbedPane.add("选项卡2",panel2);
        this.add(tabbedPane);

        //  菜单栏
        menuBar = new JMenuBar();
        menuFile = new JMenu("文件");
        menuHelp = new JMenu("帮助");
        itemNew = new JMenuItem("新建");
        itemOpen = new JMenuItem("打开");
        itemSave = new JMenuItem("保存");
        itemExit = new JMenuItem("退出");
        itemAbout = new JMenuItem("关于");
        itemJava = new JMenuItem("Java工程");
        menuBar.add(menuFile);
        menuBar.add(menuHelp);
        menuFile.add(itemNew);
        menuFile.add(itemOpen);
        menuFile.add(itemSave);
        menuFile.addSeparator();
        menuFile.add(itemExit);
        menuHelp.add(itemAbout);
        menuHelp.add(itemJava);
        this.setJMenuBar(menuBar);  //  给窗体设置菜单栏

        //  工具栏
        toolBar = new JToolBar();
        btnSave = new JButton("保存",new ImageIcon("images/5.jpg"));
        btnPre = new JButton("预览",new ImageIcon("images/3.jpg"));
        btnDown = new JButton("下载",new ImageIcon("images/4.jpg"));
        btnSel = new JButton("查询",new ImageIcon("images/2.jpg"));
        btnDel = new JButton("删除",new ImageIcon("images/1.jpg"));
        btnSave.setToolTipText("保存");
        btnPre.setToolTipText("预览");
        btnDown.setToolTipText("下载");
        btnSel.setToolTipText("查询");
        btnDel.setToolTipText("删除");
        toolBar.add(btnSave);
        toolBar.add(btnPre);
        toolBar.add(btnDown);
        toolBar.add(btnSel);
        toolBar.add(btnDel);
        this.add(toolBar, BorderLayout.NORTH);  // 将工具栏添加到窗体的顶部

        btnOk = new JButton("确认");
        btnMeg = new JButton("消息");
        btnOpt = new JButton("选项");
        btnInput = new JButton("输入");
        panel1.add(btnOk,BorderLayout.WEST);
        panel1.add(btnMeg, BorderLayout.SOUTH);
        panel1.add(btnOpt,BorderLayout.EAST);
        panel1.add(btnInput,BorderLayout.NORTH);
        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int val = JOptionPane.showConfirmDialog(btnOk,"您确定要删除吗?",
                        "确认框",JOptionPane.YES_NO_OPTION);
                if(val == JOptionPane.YES_OPTION){
                    System.out.println("删除成功!");
                }else{
                    System.out.println("删除失败!");
                }

            }
        });
        btnMeg.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(btnMeg,"今晚睡觉前要完成这个实验报告!",
                        "消息框",JOptionPane.INFORMATION_MESSAGE);
                System.out.println("收到!");
            }
        });
        btnOpt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object[] objects = {"red","blue","yellow"};
                int val = JOptionPane.showOptionDialog(btnOpt,"选择一种你喜欢的颜色",
                        "选项框",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,
                        null,objects,objects[1]);
                if(val != JOptionPane.CLOSED_OPTION){
                    System.out.println("你选择的颜色:"+objects[val]);
                }
            }
        });
        btnInput.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String val = JOptionPane.showInputDialog(btnInput,"请在框框输入内容:",
                        "输入框",JOptionPane.YES_NO_OPTION);
                System.out.println("你输入的内容是:"+val);
            }
        });

        colorChooser = new JColorChooser();
        dialogColor = JColorChooser.createDialog(this,"颜色面板",true,colorChooser,
                null,null);
        btnChange = new JButton("改变面板背景颜色");
        panel2.add(btnChange);
        btnChange.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialogColor.setVisible(true);
                panel2.setBackground(colorChooser.getColor());
            }
        });

        this.setSize(400,500);
        this.setLocation(200,100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new Swing();
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来得晚一些也行

观众老爷,请赏~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值