- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个程序,允许用户在 JTextField 中输入文件路径,并在单击“查看”JButton 或按下 Enter 键时在 JTextArea 中显示文件的内容。
问题:单击“查看”按钮即可(显示文件内容)。但是,回车键不起作用。我认为 southPanel.setFocusable(true)
很困惑。
import java.awt.BorderLayout;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.*;
public class CFrame extends JFrame{
JTextArea outputTextArea = new JTextArea(); // Create a TextArea on which the contents of the file will be displayed
JPanel southPanel = new JPanel(); // Create a panel to be placed at the bottom of the frame
JScrollPane output = new JScrollPane(outputTextArea); // Create a ScrollPane for outputTextArea
JButton view = new JButton("View"); // This button, when clicked, displays the file's contents in outputTextArea
JTextField fileNameField = new JTextField(); // File Path/Name is entered in this field
CFrame() {
southPanel.setFocusable(true);
southPanel.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == e.VK_ENTER) {
view.setEnabled(true);
}
}
});
southPanel.setLayout(new BorderLayout());
southPanel.add(fileNameField,BorderLayout.CENTER);
southPanel.add(new JLabel(" File Name: "), BorderLayout.WEST);
southPanel.add(view, BorderLayout.EAST);
outputTextArea.setEditable(false);
setLayout(new BorderLayout(0,1));
add(output,BorderLayout.CENTER);
add(southPanel,BorderLayout.SOUTH);
view.addActionListener(new ButtonListener());
}
public class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String fileName = fileNameField.getText();
String data = "";
File file = new File(fileName);
try {
Scanner input = new Scanner(file);
while(input.hasNextLine()) {
data = data + input.nextLine() + "\n\r";
}
outputTextArea.setText(data);
}
catch(FileNotFoundException ex) {
System.out.println("No such file");
}
southPanel.requestFocusInWindow();
}
}
}
最佳答案
enter file path in a JTextField and displays the contents of the file in a JTextArea when the "View" JButton is clicked or the Enter Key is pressed
你的设计是错误的。您不应尝试监听面板上的 Enter 键。
将 ActionListener 添加到文本字段。如果文本字段具有焦点并且您按 Enter 键,则会调用 ActionListener。
关于java - panel.setFocusable() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31420461/
我有一个小型计时器应用程序和名为 etH、etM 和 etS 的 EditText 小部件,它们允许输入并显示时间。单击开始按钮并启动计时器时,我通过执行以下操作来禁止交互: btnS.setOnCl
eT = (EditText) findViewById(R.id.searchAutoCompleteTextView_feed); eT.setFocusable(false);
我正在开发 Eclipse RCP 应用程序。在扩展 MultiPageEditorPart 的类中,我试图将焦点设置到文本字段。但是 setFocus 方法总是返回 false。 我做错了什么? M
我有一个包含多个自定义控件的程序。这些自定义控件之一是文本输入控件。由于单击窗口时窗口不会自动接收键盘焦点,因此我在我的程序中创建了一个鼠标 Hook ,当用户单击该窗口时,它会在该窗口上调用 Set
我正在尝试将焦点设置在文本框上。 SetFocus(txtUserName); 我在网页上没有看到任何变化。 .Focus()有什么区别和 SetFocus()每个的功能是什么? 最佳答案 您可以使用
我们发现此代码可以在具有 IE、Safari 和 FireFox 的 PC 上运行。然而,在装有 Safari 的 iPad 或 iPhone 上,它不会自动聚焦于文本框。我们不能触摸屏幕并单击文本框
我正在处理一个巨大的遗留源代码,其中在许多地方调用了多个SetFocus,但有时,缺少对控件是否可见或启用的检查。 由于时间有限,而且源代码数量巨大,我决定忽略这些错误,因为重点(在我们的例子中)不是
我正在制作一个程序,允许用户在 JTextField 中输入文件路径,并在单击“查看”JButton 或按下 Enter 键时在 JTextArea 中显示文件的内容。 问题:单击“查看”按钮即可(显
嗨,我的表格上有表格。当页面加载被触发时,我将焦点设置为第一个输入[type='text']。我用ajax发布表单,如果一切顺利,我会在表格顶部添加新行。现在我无法将焦点设置到新插入的行。这是我的 j
我正在使用 Ajax ModalPopupExtender 来显示弹出面板。该面板有一个 TextBox。我想在弹出面板时 setfocus 到那个 TextBox。是否有任何方法或机制可以将焦点设置
我有一个下拉列表,它在 OnLoad() 事件上从数据库加载记录,我想在页面加载时将焦点设置在下拉框上。我尝试了很多方法来设置焦点,但它不起作用。请看下面的代码。 HTML代码如下,
我对 setFocus() 有疑问。为什么它不适用于 textBox? 这是我的代码: package com.test.test1; import com.google.gwt.user.clien
环境:Visual Studio 2008、Visual Studio Feature Pack、MFC Dialog App、Windows XP、新的公共(public)控件。 我在使用恰好位于选
我正在尝试使用 VSTGUI 4.0 实现 VST。我的 DAW 使用 WM_KEYDOWN 和 WM_KEYUP 消息根据键盘按下发送 MIDI 音符,这样您就可以用键盘播放 VST。问题是,VST
我正在实现一个自定义上下文菜单,它基本上是一个名为 gripmenu 的小部件,其中包含其他几个小部件。如果用户左键单击,则应显示此菜单。如果用户在其外部单击,要隐藏或删除它,我需要以某种方式检查用户
本文整理了Java中am.widget.zxingscanview.ZxingScanView.setFocusable()方法的一些代码示例,展示了ZxingScanView.setFocusabl
我在下面有这段代码,一切都适用于 Android 9 RespM= FindViewById(Resource.Id.RespMedius); RespM.SetFocusable(ViewFocus
据我所知,zip 中的缩小文件只是基本代码,不包含库。因此我运行了 build.sh文件,它生成了另一个缩小的文件,我已将其包含在我的脚本中。 期望: 我尝试在单击时放大标记。我有一个在事件 onMa
现在,这可能看起来是重复的,但它不是。Internet 上的每个解决方案都向您展示了如何将焦点放在触发事件的文本框上。 但是如果用户按下 Tab 键呢?应该具有焦点的文本框是下一个。因此,我们采取了一
谁能告诉我为什么我会在下面的跟踪中收到错误? “SetFocus 只能在 PreRender 之前和期间调用。” 错误消息表明必须在 PreRender 之前或期间调用“SetFocus”,并且根据跟
我是一名优秀的程序员,十分优秀!