Java JTextPane没有接收击键

时间:2013-09-14 17:58:35

标签: java swing netbeans mouseevent jtextpane

我在JWindow下方的一小段代码中加了一个JTextPane。当我在Netbeans中运行完整代码时,它会创建一个漂亮的窗口,但JTextPane将不会响应鼠标或键盘事件。当我将鼠标悬停在它上面时,会出现文本鼠标图标,但是当我单击它时,它会选择它后面的Netbeans窗口。我尝试使用setFocusable调用JTextPane的{​​{1}}方法并调用true。我怎样才能让grabFocus回复鼠标和键盘事件?

我有两个文件。一个名为JTextPane,只创建一个新的MainClass.java对象。 MainWindow是问题所在。它有一个人工标题栏,两面,以及给我带来问题的居中MainWindow.java

请注意,使用JTextPanel不是一种选择。

谢谢!

MainWindow.java

JFrame

MainClass.java

package Window;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JWindow;
import javax.swing.SpringLayout;

public class MainWindow extends JWindow{
    private Color TitleColor = new Color(18, 15, 65);
    private Color BGColor = new Color(50, 50, 50);

    public MainWindow(){
        GraphicsEnvironment env =
        GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle MaxBounds = env.getMaximumWindowBounds();

        SpringLayout Layout = new SpringLayout();
        Container ContentPane = this.getContentPane();
        this.setLayout(Layout);

        JPanel TitleBar = new JPanel();
        TitleBar.setBackground(TitleColor);
        TitleBar.setPreferredSize(new Dimension(0, 30));
        this.add(TitleBar);

        JPanel LeftPanel = new JPanel();
        LeftPanel.setBackground(BGColor);
        this.add(LeftPanel);

        JPanel RightPanel = new JPanel();
        RightPanel.setBackground(BGColor);
        this.add(RightPanel);

        JTextPane TextPane = new JTextPane();
        TextPane.setBackground(Color.WHITE);
        TextPane.setForeground(Color.BLACK);
        TextPane.setPreferredSize(new Dimension(700, 0));
        TextPane.setFocusable(true);
        TextPane.setMargin(new Insets(20,20,20,20));
        TextPane.grabFocus();
        this.add(TextPane);

        Layout.putConstraint(SpringLayout.EAST, TitleBar, 0, SpringLayout.EAST, ContentPane);
        Layout.putConstraint(SpringLayout.WEST, TitleBar, 0, SpringLayout.WEST, ContentPane);
        Layout.putConstraint(SpringLayout.NORTH, TitleBar, 0, SpringLayout.NORTH, ContentPane);

        Layout.putConstraint(SpringLayout.NORTH, TextPane, 0, SpringLayout.SOUTH, TitleBar);
        Layout.putConstraint(SpringLayout.SOUTH, TextPane, 0, SpringLayout.SOUTH, ContentPane);
        Layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, TextPane, 0, SpringLayout.HORIZONTAL_CENTER, ContentPane);

        Layout.putConstraint(SpringLayout.NORTH, LeftPanel, 0, SpringLayout.SOUTH, TitleBar);
        Layout.putConstraint(SpringLayout.SOUTH, LeftPanel, 0, SpringLayout.SOUTH, ContentPane);
        Layout.putConstraint(SpringLayout.EAST, LeftPanel, 0, SpringLayout.WEST, TextPane);
        Layout.putConstraint(SpringLayout.WEST, LeftPanel, 0, SpringLayout.WEST, ContentPane);

        Layout.putConstraint(SpringLayout.NORTH, RightPanel, 0, SpringLayout.SOUTH, TitleBar);
        Layout.putConstraint(SpringLayout.SOUTH, RightPanel, 0, SpringLayout.SOUTH, ContentPane);
        Layout.putConstraint(SpringLayout.EAST, RightPanel, 0, SpringLayout.EAST, ContentPane);
        Layout.putConstraint(SpringLayout.WEST, RightPanel, 0, SpringLayout.EAST, TextPane);

        this.setBounds(MaxBounds);
        this.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:1)