本文整理汇总了Python中javax.swing.JTextField.addKeyListener方法的典型用法代码示例。如果您正苦于以下问题:Python JTextField.addKeyListener方法的具体用法?Python JTextField.addKeyListener怎么用?Python JTextField.addKeyListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.JTextField
的用法示例。
在下文中一共展示了JTextField.addKeyListener方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ConsolePanel
# 需要导入模块: from javax.swing import JTextField [as 别名]
# 或者: from javax.swing.JTextField import addKeyListener [as 别名]
#.........这里部分代码省略.........
#create the input text box
self.inText = JTextField()
self.inText.setFocusTraversalKeysEnabled(False)
self.inText.setFont(font)
self.inText.setBackground(Color(0, 20, 0))
self.inText.setForeground(Color.WHITE)
self.inText.getCaret().setVisible(True)
self.inText.getCaret().setBlinkRate(500)
self.inText.setCaretColor(Color(200,255,200))
class InFocusAdapter(FocusAdapter):
def focusLost(adap, e):
self.inText.setVisible(True)
self.inText.addFocusListener(InFocusAdapter())
self.nestedInputPanel = Panel("Insets 0 0 0 0")
#create the directory text box
self.directoryText = JTextField()
self.directoryText.setEditable(False)
self.directoryText.setFont(font)
self.directoryText.setBackground(Color(0, 20, 0))
self.directoryText.setForeground(Color.WHITE)
#set up the console
sys.stdout = FakeOut(self.outText)
self.console = BashED_Console(stdout=sys.stdout)
self.directoryText.setText(self.console.get_prompt())
self.revalidate();
dirTex = self.directoryText;
#create the listener that fires when the 'return' key is pressed
class InputTextActionListener(ActionListener):
def __init__(self,parent,inp,out,console):
self.parent = parent
self.inp = inp
self.out = out
self.console = console
def actionPerformed(self, e):
#print self.getCommandText()
# print(self.console.get_prompt())
# self.console.onecmd(self.inp.getText())
# self.parent.write_out("\n" + self.inp.getText())
# dirTex.setText(self.console.get_prompt())
# self.inp.setText("")
self.parent.write_out(self.console.get_prompt() + self.inp.getText() + '\n')
if 'clear' in self.inp.getText().split(' ')[0]:
self.out.setText("") #clear the screen
else:
self.console.onecmd(self.inp.getText())
dirTex.setText(self.console.get_prompt())
self.inp.setText("")
#create the listener that fires whenever a user hits a key
class InputKeyActionListener(KeyAdapter):
def __init__(self,parent,inp,out,console):
self.parent = parent
self.inp = inp
self.out = out
self.console = console
def keyReleased(self, k):
inp = self.inp.getText()
if k.getKeyCode() == 9: #tab character
autos = self.console.tabcomplete(self.inp.getText())
if len(autos) == 1:
self.inp.setText(autos[0])
else:
i = 0
for option in autos:
self.parent.write_out(option)
if i % 3 == 0:
print('\n')
else:
print('\t')
hist = None
if k.getKeyCode() == 38:
hist = self.console.next_hist()
if k.getKeyCode() == 40:
hist = self.console.last_hist()
if hist:
self.inp.setText(hist.rstrip('\n'))#prevent from firing
self.inText.addActionListener(InputTextActionListener(self,self.inText,self.outText,self.console))
self.inText.addKeyListener(InputKeyActionListener(self,self.inText,self.outText,self.console))
def addUI(self):
self.add(self.outTextScroller, "cell 0 0, push, grow")
self.add(self.nestedInputPanel, "cell 0 1, pushx, growx")
self.nestedInputPanel.add(self.directoryText, "cell 0 0")
self.nestedInputPanel.add(self.inText, "cell 1 0, spanx, pushx, growx")