「Jython JFileChooser 1」の版間の差分
ナビゲーションに移動
検索に移動
1行目: | 1行目: | ||
− | ==Jython JFileChooser 1== | + | ==[[Jython JFileChooser 1]]== |
[[Jython Swing]] | [[Swing]] | [[Jython]] | [[Python]] | | [[Jython Swing]] | [[Swing]] | [[Jython]] | [[Python]] | | ||
===方針=== | ===方針=== | ||
− | + | 以下を参考に、[[Jython]]でファイル選択を実装してみる | |
*http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html | *http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html | ||
43行目: | 43行目: | ||
buttonPanel.add(self.saveButton) | buttonPanel.add(self.saveButton) | ||
− | self.add(buttonPanel, BorderLayout. | + | self.add(buttonPanel, BorderLayout.PAGE_STA[[R]]T) |
− | self.add(logScrollPane, BorderLayout. | + | self.add(logScrollPane, BorderLayout.CENTE[[R]]) |
def createUI(self): | def createUI(self): | ||
− | frame = JFrame("FileChooserDemo") | + | frame = [[JFrame]]("FileChooserDemo") |
− | frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE | + | frame.defaultCloseOperation = [[JFrame]].EXIT_ON_CLOSE |
frame.add(self) | frame.add(self) | ||
frame.pack() | frame.pack() | ||
− | frame. | + | frame.[[vi]]sible = True |
class ButtonListener(ActionListener): | class ButtonListener(ActionListener): | ||
61行目: | 61行目: | ||
returnVal = self.target.fc.showOpenDialog(self.target) | returnVal = self.target.fc.showOpenDialog(self.target) | ||
− | if returnVal == JFileChooser. | + | if returnVal == JFileChooser.APP[[R]]OVE_OPTION: |
file = self.target.fc.selectedFile | file = self.target.fc.selectedFile | ||
self.target.log.append("Opening: %s.\n" % file.name) | self.target.log.append("Opening: %s.\n" % file.name) | ||
70行目: | 70行目: | ||
elif e.source is self.target.saveButton: | elif e.source is self.target.saveButton: | ||
returnVal = self.target.fc.showSaveDialog(self.target); | returnVal = self.target.fc.showSaveDialog(self.target); | ||
− | if returnVal == JFileChooser. | + | if returnVal == JFileChooser.APP[[R]]OVE_OPTION: |
file = self.target.fc.selectedFile | file = self.target.fc.selectedFile | ||
− | self.target.log.append(" | + | self.target.log.append("Sa[[vi]]ng: %s.\n" % file.getName()) |
else: | else: | ||
self.target.log.append("Save command cancelled by user.\n") | self.target.log.append("Save command cancelled by user.\n") | ||
self.target.log.caretPosition = self.target.log.document.length | self.target.log.caretPosition = self.target.log.document.length | ||
− | class Invoker( | + | class Invoker([[R]]unnable): |
def run(self): | def run(self): | ||
− | # Java Look & Feel(Metal)のデフォルトでボールドフォントを使用しない | + | # [[Java]] Look & Feel(Metal)のデフォルトでボールドフォントを使用しない |
UIManager.put("swing.boldMetal", Boolean.FALSE) | UIManager.put("swing.boldMetal", Boolean.FALSE) | ||
sample = JFileChooserTest() | sample = JFileChooserTest() | ||
sample.createUI() | sample.createUI() | ||
− | + | [[Swing]]Utilities.invokeLater(Invoker()) |
2020年2月16日 (日) 04:28時点における最新版
Jython JFileChooser 1
Jython Swing | Swing | Jython | Python |
方針
以下を参考に、Jythonでファイル選択を実装してみる
ソースコード
実行例
ソースコード
# -*- coding: utf-8 -*- from java.lang import * from java.io import * from java.awt import * from java.awt.event import * from javax.swing import * from javax.swing.filechooser import * class JFileChooserTest(JPanel): def __init__(self): super(JFileChooserTest, self).__init__(BorderLayout()) self.log = JTextArea(5, 20) self.log.margin = Insets(5,5,5,5) self.log.editable = False logScrollPane = JScrollPane(self.log) self.fc = JFileChooser() self.openButton = JButton("Open a File...") self.saveButton = JButton("Save a File...") lsnr = ButtonListener(self) self.openButton.addActionListener(lsnr) self.saveButton.addActionListener(lsnr) buttonPanel = JPanel() buttonPanel.add(self.openButton) buttonPanel.add(self.saveButton) self.add(buttonPanel, BorderLayout.PAGE_START) self.add(logScrollPane, BorderLayout.CENTER) def createUI(self): frame = JFrame("FileChooserDemo") frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE frame.add(self) frame.pack() frame.visible = True class ButtonListener(ActionListener): def __init__(self, target): self.target = target def actionPerformed(self, e): if e.source is self.target.openButton: returnVal = self.target.fc.showOpenDialog(self.target) if returnVal == JFileChooser.APPROVE_OPTION: file = self.target.fc.selectedFile self.target.log.append("Opening: %s.\n" % file.name) else: self.target.log.append("Open command cancelled by user.\n") self.target.log.caretPosition = self.target.log.document.length elif e.source is self.target.saveButton: returnVal = self.target.fc.showSaveDialog(self.target); if returnVal == JFileChooser.APPROVE_OPTION: file = self.target.fc.selectedFile self.target.log.append("Saving: %s.\n" % file.getName()) else: self.target.log.append("Save command cancelled by user.\n") self.target.log.caretPosition = self.target.log.document.length class Invoker(Runnable): def run(self): # Java Look & Feel(Metal)のデフォルトでボールドフォントを使用しない UIManager.put("swing.boldMetal", Boolean.FALSE) sample = JFileChooserTest() sample.createUI() SwingUtilities.invokeLater(Invoker())
© 2006 矢木浩人