1

Jython のクラスにボタンを配置しようとしました。SWT コンポーネントを使用して GUI を作成しています。ボタンは表示されませんが、ウィンドウは正常に表示されます。私はすでにこのクラスをJavaで作成しており、うまく機能しています。

これは私のコードです。何が問題なのですか?

from java.lang import Thread as JThread, InterruptedException
from org.eclipse.swt import widgets, layout, SWT 
from org.eclipse.swt.layout import GridLayout, GridData, FillLayout
from org.eclipse.swt.widgets import Composite, Listener#, Event
from org.yakindu.sct.runtime.java.new64 import New64CycleBasedStatemachine
import time

class Cycle(object):
    def __init__(self):

        self.display = widgets.Display()
        self.shell = widgets.Shell(self.display, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL)
        self.shell.setLayout(layout.RowLayout())
        self.shell.setText("Message Window")
        self.shell.setLocation(300, 300)
        self.shell.setSize(300, 150)
        self.thread = JThread()
        self.shell.open()

    def run(self):
        self.statemachine = New64CycleBasedStatemachine()
        self.statemachine.enter()
        while not self.thread.isInterrupted():
            self.statemachine.getInterfaceNewTest().getVarMessage()
            self.statemachine.runCycle()
            try: 
                time.sleep(100)
            except InterruptedException: 
                self.thread.interrupt()


        self.thread.start()

    def show_window(self):
        while not self.shell.isDisposed():
            if not self.display.readAndDispatch():
                self.display.sleep()
        self.display.dispose()

        self.thread.interrupted()

    def create_button(self, statemachine, shell):
        self.buttonComposite = Composite(shell, SWT.NO_SCROLL)
        self.buttonCompositeGridData =  GridData()
        self.buttonCompositeGridData.horizontalAlignment = GridData.FILL
        self.buttonCompositeGridData.grabExcessHorizontalSpace = True
        self.buttonComposite.setLayoutData(self.buttonCompositeGridData)
        self.buttonCompositeLayout = FillLayout()
        self.buttonCompositeLayout.type = SWT.HORIZONTAL
        fillLayout = FillLayout()

        self.buttonComposite.setLayout(fillLayout)
        self.button = widgets.Button(self.buttonComposite, SWT.PUSH);
        self.button.setText("Ok")


foo = Cycle()
foo.run()
foo.create_button()
foo.show_window()
4

1 に答える 1

0

複数の問題があります。

  1. の位置self.shell.open()。シェルcreate_button()開いて呼び出しています!! の javadoc を参照してくださいshell.open()
  2. を使用しRowLayout、 を作成してGridDataから を設定していFillLayoutます。問題は教えません。コードを実行して確認してください。

スニペットの作業例

from java.lang import Thread as JThread, InterruptedException
from org.eclipse.swt import widgets, layout, SWT 
from org.eclipse.swt.layout import GridLayout, GridData, FillLayout
from org.eclipse.swt.widgets import Composite, Listener#, Event

import time

class Cycle(object):
    def __init__(self):

        self.display = widgets.Display()
        self.shell = widgets.Shell(self.display, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL)
        self.shell.setLayout(layout.RowLayout())
        self.shell.setText("Message Window")
        self.shell.setLocation(300, 300)
        self.shell.setSize(300, 150)

    def show_window(self):
        self.shell.open() // I have moved it here. After all the control creation !!
        while not self.shell.isDisposed():
            if not self.display.readAndDispatch():
                self.display.sleep()
        self.display.dispose()


    def create_button(self):
        self.buttonComposite = Composite(self.shell, SWT.NO_SCROLL)
        #self.buttonCompositeGridData =  GridData()
        #self.buttonCompositeGridData.horizontalAlignment = GridData.FILL
        #self.buttonCompositeGridData.grabExcessHorizontalSpace = True
        #self.buttonComposite.setLayoutData(self.buttonCompositeGridData)
        self.buttonCompositeLayout = FillLayout()
        self.buttonCompositeLayout.type = SWT.HORIZONTAL
        fillLayout = FillLayout()

        self.buttonComposite.setLayout(fillLayout)
        self.button = widgets.Button(self.buttonComposite, SWT.PUSH);
        self.button.setText("Ok")


foo = Cycle()
foo.create_button()
foo.show_window()

Output-

ここに画像の説明を入力

于 2012-06-29T14:59:44.357 に答える