本文整理汇总了Python中javax.swing.JTextPane.getStyledDocument方法的典型用法代码示例。如果您正苦于以下问题:Python JTextPane.getStyledDocument方法的具体用法?Python JTextPane.getStyledDocument怎么用?Python JTextPane.getStyledDocument使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.JTextPane
的用法示例。
在下文中一共展示了JTextPane.getStyledDocument方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: OutputPane
# 需要导入模块: from javax.swing import JTextPane [as 别名]
# 或者: from javax.swing.JTextPane import getStyledDocument [as 别名]
class OutputPane(object):
"""Pane for outpout of interactive session"""
def __init__(self):
self.textpane = JTextPane()
self.doc = self.textpane.getStyledDocument()
self.textpane.editable = False
style_context = StyleContext.getDefaultStyleContext()
default_style = style_context.getStyle(StyleContext.DEFAULT_STYLE)
parent_style = self.doc.addStyle("parent", default_style)
StyleConstants.setFontFamily(parent_style, "Monospaced")
input_style = self.doc.addStyle("input", parent_style)
output_style = self.doc.addStyle("output", parent_style)
StyleConstants.setForeground(output_style, awtColor.BLUE)
error_style = self.doc.addStyle("error", parent_style)
StyleConstants.setForeground(error_style, awtColor.RED)
# Do a dance to set tab size
font = Font("Monospaced", Font.PLAIN, 12)
self.textpane.setFont(font)
fm = self.textpane.getFontMetrics(font)
tabw = float(fm.stringWidth(" "*4))
tabs = [
TabStop(tabw*i, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE)
for i in xrange(1, 51)
]
attr_set = style_context.addAttribute(
SimpleAttributeSet.EMPTY,
StyleConstants.TabSet,
TabSet(tabs)
)
self.textpane.setParagraphAttributes(attr_set, False)
#Dance done!
def addtext(self, text, style="input", ensure_newline=False):
doclen = self.doc.length
if ensure_newline and doclen:
if self.doc.getText(doclen - 1, 1) != '\n':
text = '\n' + text
self.doc.insertString(self.doc.length, text, self.doc.getStyle(style))
# Scroll down
self.textpane.setCaretPosition(self.doc.length)
def clear(self):
"""Remove all text"""
self.doc.remove(0, self.doc.length)
示例2: OutputPane
# 需要导入模块: from javax.swing import JTextPane [as 别名]
# 或者: from javax.swing.JTextPane import getStyledDocument [as 别名]
class OutputPane(object):
def __init__(self):
self.textpane = JTextPane()
self.doc = self.textpane.getStyledDocument()
self.textpane.editable = False
default_style = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE)
parent_style = self.doc.addStyle("parent", default_style)
StyleConstants.setFontFamily(parent_style, "Monospaced")
input_style = self.doc.addStyle("input", parent_style)
output_style = self.doc.addStyle("output", parent_style)
StyleConstants.setForeground(output_style, Color.BLUE)
error_style = self.doc.addStyle("error", parent_style)
StyleConstants.setForeground(error_style, Color.RED)
def addtext(self, text, style="input", ensure_newline=False):
doclen = self.doc.length
if ensure_newline and self.doc.getText(doclen - 1, 1) != '\n':
text = '\n' + text
self.doc.insertString(self.doc.length, text, self.doc.getStyle(style))
# Scroll down
self.textpane.setCaretPosition(self.doc.length)
示例3: menuItemClicked
# 需要导入模块: from javax.swing import JTextPane [as 别名]
# 或者: from javax.swing.JTextPane import getStyledDocument [as 别名]
def menuItemClicked(self, caption, messageInfo):
response = messageInfo[0].getResponse()
strResponse = ''.join([chr(c%256) for c in response])
frame = JFrame('DOM XSS',size = (300,300))
parentPanel = JPanel()
#printedCode = JTextPane(text = strResponse)
#'''
#colored code
printedCode = JTextPane()
styledDoc = printedCode.getStyledDocument()
style = printedCode.addStyle('ColoredCode',None)
self.filter2(strResponse,styledDoc,style)
#'''
#Scroll Bar
scrollPanel = JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)
scrollPanel.preferredSize = 1500,800
scrollPanel.viewport.view = printedCode
#Final Inclusion of Panels
parentPanel.add(scrollPanel)
frame.add(parentPanel)
frame.visible = True
示例4: ChatClient
# 需要导入模块: from javax.swing import JTextPane [as 别名]
# 或者: from javax.swing.JTextPane import getStyledDocument [as 别名]
#.........这里部分代码省略.........
## Event driven function to retrieve and send data to the server
def grabText(self, event):
'''Function to repeatedly grab new messages entered into the text
area and display them in the main text area. Resets the entry area
'''
# Grab the text from the text area
text=self.message.getText()
# Don't allow an empty string through
if text=='':
return
text=text.strip()
# Call the append text function
self.appendText('\nYou : '+text+'\n', self.username)
# Reset the text to be empty and grab focus so that it is ready for new text input
self.message.requestFocusInWindow()
self.message.setText('')
# Send the message to the server
data=text.encode()
self.tn.write(data+'\r\n')
## Function to handle appending of messages
def appendText(self, message, user=None):
'''This function takes care of appending any new messages to the content area
'''
message_label=JTextArea(message,2,3, font=self.label_2_font)
# If this is a message from the grab text function, create a new label, assign it's colours
if user!=None:
message_label.setBackground(Color(240,240,240))
message_label.setForeground(Color(129,129,129))
# Otherwise set the format for receive function (no user passed in)
else:
message_label.setBackground(Color(215,215,215))
message_label.setForeground(Color(40,153,153))
# Format and style options for the new message labels
message_label.setEditable(False)
message_label.setLineWrap(True)
message_label.setWrapStyleWord(True)
message_label.setBorder(BorderFactory.createLineBorder( Color(247,246,242),4))
# Sets the positioning of messages
self.main_content.setCaretPosition(self.main_content.getDocument().getLength())
doc = self.main_content.getStyledDocument()
attr=SimpleAttributeSet()
self.main_content.insertComponent(message_label)
# Essential for jtextarea to be able to stack message
doc.insertString( self.main_content.getDocument().getLength(),'\n ', attr)
# Not sure if needed
self.main_content.repaint()
### This is a late edit so it isn't included in the documentation. Basically trying to dynamically update the number
### of users label at runtime. Works for incrementing the value but not decrementing it.
print(message)
# Only split the message if there are enough values to split (greeting messages differ in format to chat messages)
try:
user, text=message.split(' : ')
except:
return
#print('Split values are %s %s'%(user, text))
user=str(user.strip())
#print(self.no_users)
#print(user+' : '+text)
# If the user already in the list, pass
if user in self.no_users:
if text == ('User %s amach sa teach !'%user):
self.no_users.remove(user)
print('User % removed'%user)
else:
#print('User %s not in list'%user)
if str(user) == 'You':
#print('User is equal to "You"')
return
self.no_users.append(user)
print('User appended')
self.number_users=len(self.no_users)
#print('Length of user list is '+str(self.number_users))
self.user_label2=JLabel(" Users online : %s "%str(len(self.no_users)),JLabel.RIGHT, font=self.label_2_font)
#print('Label created')
#print('Attempt to replace label')
self.client_layout.replace(self.user_label, self.user_label2)
self.user_label = self.user_label2
self.user_label.repaint()
self.user_label.revalidate()
print('Label updated')
## Function to control return button press in message field
def returnKeyPress(self,event):
'''This function creates an object for return key press when inside the message entry area,
creates an object of KeyAdapter and tests keycode for a match, responds with grab text callback
'''
key_object=Key()
key_value=key_object.keyPressed(event)
if key_value == 10:
self.grabText(event)