gpt4 book ai didi

Java JTextPane 没有收到击键

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:53 25 4
gpt4 key购买 nike

我在 JWindow 下面有一小段代码,其中有一个 JTextPane。当我在 Netbeans 中运行完整代码时,它会创建一个漂亮的窗口,但 JTextPane 不会响应鼠标或键盘事件。当我将鼠标悬停在它上面时,会出现文本鼠标图标,但是当我单击它时,它会选择它后面的 Netbeans 窗口。我尝试用 true 调用 JTextPanesetFocusable 方法,并调用 grabFocus ..如何我可以让 JTextPane 响应鼠标和键盘事件吗?

我有两个文件。一个称为 MainClass.java,它只是创建一个新的 MainWindow 对象。 MainWindow.java 是问题所在。它有一个人工标题栏、两侧和居中的 JTextPanel,这给我带来了问题。

请注意,不能使用 JFrame

谢谢!

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

最佳答案

关于Java JTextPane 没有收到击键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18804748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com