3

我在 a 下面的一小段代码中JWindow带有 a JTextPane。当我在 Netbeans 中运行完整代码时,它会创建一个漂亮的窗口,但JTextPane不会响应鼠标或键盘事件。当我将鼠标悬停在它上面时,会出现文本鼠标图标,但是当我单击它时,它会选择它后面的 Netbeans 窗口。我试过调用 with 的方法setFocusable,也调用了。我怎样才能让响应鼠标和键盘事件?JTextPanetruegrabFocusJTextPane

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

请注意,使用 aJFrame不是一个选项。

谢谢!

主窗口.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);
    }
}

主类.java

package Window;

import javax.swing.SwingUtilities;

public class MainClass {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run(){
              new MainWindow();  
            }
        });
    }
}
4

1 回答 1

1
于 2013-09-14T20:06:18.777 回答