当前位置: 首页>>代码示例>>Python>>正文


Python JFrame.addKeyListener方法代码示例

本文整理汇总了Python中javax.swing.JFrame.addKeyListener方法的典型用法代码示例。如果您正苦于以下问题:Python JFrame.addKeyListener方法的具体用法?Python JFrame.addKeyListener怎么用?Python JFrame.addKeyListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.swing.JFrame的用法示例。


在下文中一共展示了JFrame.addKeyListener方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: GraphicsWindow

# 需要导入模块: from javax.swing import JFrame [as 别名]
# 或者: from javax.swing.JFrame import addKeyListener [as 别名]
class GraphicsWindow(ActionListener, KeyListener, MouseInputListener):
    """
    Creates a :py:class:`~jygsaw.graphicswindow.GraphicsWindow` with a
    :py:class:`~jygsaw.graphicswindow.Canvas` object that can be drawn on.
    Takes a title, window width, and window height.
    An optional background color can be specified.
    """
    colors = [BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN,
              LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW]

    def __init__(self, title, w, h, bg_color=WHITE):
        assert w > 0, "GraphicsWindow width must be greater than zero"
        assert h > 0, "GraphicsWindow height must be greater than zero"

        self.objs = []  # List of GraphicsObjects
        self.bg_color = bg_color

        self.frame = JFrame(
            title, defaultCloseOperation=JFrame.EXIT_ON_CLOSE,
            size=(w, h))

        self.frame.setLocationRelativeTo(None)
        self.frame.contentPane = Canvas(self, self.objs, self.bg_color)
        self.frame.contentPane.setPreferredSize(Dimension(w, h))

        self.frame.addMouseListener(self)
        self.frame.addMouseMotionListener(self)
        self.frame.addKeyListener(self)

        # MouseEvent attributes
        self.mouse_x = 0
        self.mouse_y = 0
        self.mouse_buttons = Set()

        # Mouse callbacks
        self.on_mouse_clicked = None
        self.on_mouse_dragged = None
        self.on_mouse_moved = None
        self.on_mouse_pressed = None
        self.on_mouse_released = None
        self.on_mouse_exited = None
        self.on_mouse_entered = None

        # Key callbacks
        self.on_key_pressed = None
        self.on_key_released = None
        self.on_key_typed = None

        # Key values
        self.last_key_char = None
        self.last_key_code = None

        self.chars_pressed = Set()
        self.codes_pressed = Set()

        # Event queue
        self.event_queue = Queue()
        self.main_running = False

        # not needed, user_draw is called directly from on_draw
        self.on_draw = None

    def set_visible(self, visible):
        """Sets the window to visible."""
        assert isinstance(visible, bool), "Variable is not a boolean."
        self.frame.pack()
        self.frame.visible = visible

    def draw(self, *params):
        """
        Takes any number of :py:class:`~jygsaw.graphicsobject.GraphicsObject`
        or :py:class:`~jygsaw.shape.Shape` objects, or :py:class:`~jygsaw.group.Group`,
        and draws them on the Canvas. If a shape is drawn without specifying a color
        the default color is used. The default stroke option
        (:py:class:`True` or :py:class:`False`) and *stroke_color* is saved in each object.
        """
        for arg in params:
            if isinstance(arg, GraphicsObject) or isinstance(arg, Shape):
                if arg.color is None:
                    arg.color = self.frame.contentPane.default_color
                arg.stroke_color = self.frame.contentPane.stroke_color
                arg.stroke = self.frame.contentPane.stroke
                arg.filled = self.frame.contentPane.filled
                arg.stroke_width = self.frame.contentPane.stroke_width
                if isinstance(arg, Text):
                    arg.font = self.frame.contentPane.font
                    arg.size = self.frame.contentPane.text_size
                self.objs.append(arg)
            elif isinstance(arg, Group):
                for obj in arg.group:
                    if obj.color is None:
                        obj.color = self.frame.contentPane.default_color
                    obj.stroke_color = self.frame.contentPane.stroke_color
                    obj.stroke = self.frame.contentPane.stroke
                    obj.filled = self.frame.contentPane.filled
                    arg.stroke_width = self.frame.contentPane.stroke_width
                    if isinstance(arg, Text):
                        arg.font = self.frame.contentPane.font
                        arg.size = self.frame.contentPane.text_size
                    self.objs.append(obj)
#.........这里部分代码省略.........
开发者ID:talwai,项目名称:jygsaw,代码行数:103,代码来源:graphicswindow.py

示例2: GraphicsWindow

# 需要导入模块: from javax.swing import JFrame [as 别名]
# 或者: from javax.swing.JFrame import addKeyListener [as 别名]
class GraphicsWindow(ActionListener, KeyListener, MouseInputListener):
    """
    Creates a :py:class:`~jygsaw.graphicswindow.GraphicsWindow` with a :py:class:`~jygsaw.graphicswindow.Canvas`
    object that can be drawn on. Takes a title, window width, and window height.
    An optional background color can be specified.
    """
    def __init__(self, title, w, h, backgroundColor=white):
        assert w > 0, "GraphicsWindow width must be greater than zero"
        assert h > 0, "GraphicsWindow height must be greater than zero"

        self._SHAPELIST_MAX_LENGTH = 200

        self.objs = []  # List of GraphicsObjects
        self.backgroundColor = backgroundColor

        self.frame = JFrame(
            title, defaultCloseOperation=JFrame.EXIT_ON_CLOSE,
            size=(w, h))

        self.frame.setLocationRelativeTo(None)
        self.frame.contentPane = Canvas(self, self.objs, self.backgroundColor)
        self.frame.contentPane.setPreferredSize(Dimension(w, h))

        self.frame.addMouseListener(self)
        self.frame.addMouseMotionListener(self)
        self.frame.addKeyListener(self)

        # MouseEvent attributes
        self.mouseEventType = 0
        self.mouseX = 0
        self.mouseY = 0

        # Mouse callbacks
        self.onMouseClicked = None
        self.onMouseDragged = None
        self.onMouseMoved = None
        self.onMousePressed = None
        self.onMouseReleased = None
        self.onMouseDragged = None
        self.onMouseExited = None
        self.onMouseEntered = None

        # KeyEvent booleans keyPressed, keyTyped
        self.keyEventType = 0
        self.keyP = False
        self.keyT = False

        # Key callbacks
        self.onKeyPressed = None
        self.onKeyReleased = None
        self.onKeyTyped = None

        # Key values
        self.lastKeyChar = None
        self.lastKeyCode = None

        self.charsPressed = Set()

        # Event queue
        self.eventQueue = Queue()

        self.mainRunning = False

        # not needed, user_draw is /called directly from onDraw
        self.onDraw = None

    def setVisible(self, isVisible):
        """Sets the window to visible."""
        assert isinstance(isVisible, bool), "Variable is not a boolean."
        self.frame.pack()
        self.frame.visible = isVisible

    def draw(self, *params):
        """
        Takes any number of :py:class:`~jygsaw.graphicsobject.GraphicsObject`
        or :py:class:`~jygsaw.shape.Shape` objects, or :py:class:`~jygsaw.group.Group`,
        and draws them on the Canvas. If a shape is drawn without specifying a color
        the default color is used. The default stroke option
        (:py:class:`True` or :py:class:`False`) and *strokeColor* is saved in each object.
        """
        for arg in params:
            if isinstance(arg, GraphicsObject) or isinstance(arg, Shape):
                if arg.color is None:
                    arg.color = self.frame.contentPane.defaultColor
                arg.strokeColor = self.frame.contentPane.strokeColor
                arg.stroke = self.frame.contentPane.stroke
                arg.filled = self.frame.contentPane.filled
                arg.strokeWidth = self.frame.contentPane.strokeWidth
                if isinstance(arg, Text):
                    arg.font = self.frame.contentPane.font
                    arg.size = self.frame.contentPane.textSize
                self.objs.append(arg)

                if len(self.objs) > self._SHAPELIST_MAX_LENGTH:
                    warn("You have more than " + str(self._SHAPELIST_MAX_LENGTH) +
                         " to be drawn. You may want to add a clearHalf() call to your" +
                         " code to avoid slowing your system.")

            elif isinstance(arg, Group):
                for obj in arg.group:
#.........这里部分代码省略.........
开发者ID:jygsaw-graphics,项目名称:jygsaw,代码行数:103,代码来源:graphicswindow.py


注:本文中的javax.swing.JFrame.addKeyListener方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。