日期:2014-05-20  浏览次数:20747 次

关于Swing 滚动容器包装 JTextPane的问题 往高手解答
Java code
import java.awt.Color;

public class Demo02 {
    public static void main(String[] args) {

        Icon icon = new ImageIcon("d:Demo\\素材\\阿狸壁纸.jpg");
        Font font = new Font("宋体", Font.BOLD, 20);

        final JFrame jf = new JFrame("写着玩的记事本");
        jf.addWindowListener(new WindowAdapter() {
            @Override
            public void windowActivated(WindowEvent arg0) {
                System.out.println("打开了窗口");
            }

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("关闭了窗口");
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
                System.out.println("最小化了窗口");
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
                System.out.println("最大化了窗口");
            }
        });

        final JTextPane textPane = new JTextPane();// 设置输入栏
        textPane.setBounds(0, 24, 632, 422);
        textPane.setOpaque(false); // 设置控件背景为透明
        // jf.getContentPane().add(textPane);
        textPane.setCaretColor(new Color(12, 253, 100));
        textPane.setSelectedTextColor(Color.BLACK);
        textPane.setForeground(Color.WHITE);// 设置文字颜色
        textPane.setFont(font);
        textPane.setLayout(null);

        JMenuBar menuBar = new JMenuBar();
        menuBar.setBounds(0, 0, 792, 27);
        jf.getContentPane().add(menuBar);

        JMenu menu = new JMenu("文件");
        menuBar.add(menu);
        // jf.getContentPane().add(lblNewLabel);

        final JMenuItem mntmNewMenuItem_2 = new JMenuItem("新建");
        mntmNewMenuItem_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == mntmNewMenuItem_2) {
                    System.out.println("选择了新建_快捷键为CTRL+N");
                    // textPane.setText("选择了新建_快捷键为CTRL+N");

                }
            }
        });
        mntmNewMenuItem_2.setMnemonic('N');
        mntmNewMenuItem_2.setAccelerator(KeyStroke.getKeyStroke('N',// 取得快捷键
                java.awt.Event.CTRL_MASK));
        menu.add(mntmNewMenuItem_2);

        final JMenuItem mntmNewMenuItem_1 = new JMenuItem("打开");
        mntmNewMenuItem_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == mntmNewMenuItem_1) {
                    System.out.println("选择了打开_快捷键为CTRL+O");
                    // textPane.setText("选择了打开_快捷键为CTRL+O");
                }
            }
        });
        mntmNewMenuItem_1.setMnemonic('S');
        mntmNewMenuItem_1.setAccelerator(KeyStroke.getKeyStroke('O',// 取得快捷键
                java.awt.Event.CTRL_MASK));
        menu.add(mntmNewMenuItem_2);
        menu.add(mntmNewMenuItem_1);

        final JMenuItem mntmNewMenuItem = new JMenuItem("保存");
        mntmNewMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == mntmNewMenuItem) {
                    System.out.println("选择了保存_快捷键为CTRL+S");
                    // textPane.setText("选择了保存_快捷键为CTRL+S");
                }
            }
        });
        mntmNewMenuItem.setMnemonic('S');
        mntmNewMenuItem.setAccelerator(KeyStroke.getKeyStroke('S',// 取得快捷键
                java.awt.Event.CTRL_MASK));
        menu.add(mntmNewMenuItem_2);
        menu.add(mntmNewMenuItem);

        final JMenuItem menuItem = new JMenuItem("另存为");
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == menuItem) {
                    System.out.println("选择了另存为快捷键为CTRL+Z");
                    // textPane.setText("选择了另存为_快捷键为CTRL+Z");
                }
            }
        });
        menuItem.setMnemonic('Z');
        menuItem.setAccelerator(KeyStroke.getKeyStroke('Z',// 取得快捷键
                java.awt.Event.CTRL_MASK));
        menu.add(mntmNewMenuItem_2);
        menu.add(menuItem);

        JMenu mnNewMenu = new JMenu("编辑");
        menuBar.add(mnNewMenu);

        JMenu menu_1 = new JMenu("格式");
        menuBar.add(menu_1);

        JMenu menu_2 = new JMenu("查看");
        menuBar.add(menu_2);

        JMenu menu_3 = new JMenu("帮助");
        menuBar.add(menu_3);
        // jf.add(lblNewLabel);

        JPanel jp1 = new JPanel();

        JLabel lblNewLabel = new JLabel(icon);// 设置背景
        lblNewLabel.setBounds(-11, 24, 643, 422);
        jp1.add(textPane);

        JScrollPane scrollPane = new JScrollPane(jp1,//设置滚动条
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jf.getContentPane().add(scrollPane);

        jf.setLayout(null);//设置JPrame 布局为空 否则会覆盖之前的
        jf.setSize(640, 480);
        jf.setLocation(300, 400);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭资源
    }
}