简体   繁体   中英

Jython jlist text not showing in JFrame

I am writing a little stock quote application and when I compile the code the space for the text will be taken in the Jlist but no text will appear.

def loadStockDisplay(self):
    self.display = JPanel()
    self.display.setLayout(BorderLayout())
    self.display.setBackground(Color.decode("#0A0A33"))
    self.display.setBorder(BorderFactory.createMatteBorder(0,3,0,0,Color.decode("#8080E6")))
    self.label = JLabel("Stocks")
    self.label.setForeground(Color.decode("#FCFCFC"))
    self.label.setFont(self.font)
    self.display.add(self.label,BorderLayout.NORTH)
    self.stocks = DefaultListModel();
    self.items = JList(self.stocks)
    self.items.setBackground(Color.decode("#0A0A33"))
    self.items.setForeground(Color.decode("#FCFCFC"))
    self.items.setFont(self.font)
    self.items.setSelectionBackground(Color.decode("#0A0A33"))
    self.items.setSelectionForeground(Color.decode("#FCFCFC"))
    self.display.add(self.items, BorderLayout.CENTER)
    self.frame.add(self.display,BorderLayout.EAST)
    self.updateStocks()

def updateStocks(self):
    companys = ["MSFT","SNDK","GOOGL","NOK","EMC","HPQ","IBM","EBAY","AAPL","AMZN"]
    tempList = []
    for company in companys:
        Quote = web()
        tempList.append(company + " " + str(Quote.getQuote(company)))
    self.stocks.clear()
    for item in tempList:
        self.stocks.addElement(item)

Maybe there is a problem in the code that is not yet available in the question? Could you share the class and main method (if you have these)? There could also be an issue with fetching the quotes from the web.

If I add some code to make your program run, the list has items for each company (with a dummy quote):

from java.awt import BorderLayout, Color
from javax.swing import BorderFactory, DefaultListModel, JFrame, JLabel, JList, JPanel

class StocksTest:
    def loadStockDisplay(self):
        self.frame = JFrame('Stocks', defaultCloseOperation=JFrame.EXIT_ON_CLOSE, size=(300, 300), locationRelativeTo=None)
        self.display = JPanel()
        self.display.setLayout(BorderLayout())
        self.display.setBackground(Color.decode("#0A0A33"))
        self.display.setBorder(BorderFactory.createMatteBorder(0,3,0,0,Color.decode("#8080E6")))
        self.label = JLabel("Stocks")
        self.label.setForeground(Color.decode("#FCFCFC"))
        self.label.setFont(self.frame.font)
        self.display.add(self.label,BorderLayout.NORTH)
        self.stocks = DefaultListModel();
        self.items = JList(self.stocks)
        self.items.setBackground(Color.decode("#0A0A33"))
        self.items.setForeground(Color.decode("#FCFCFC"))
        self.items.setFont(self.frame.font)
        self.items.setSelectionBackground(Color.decode("#0A0A33"))
        self.items.setSelectionForeground(Color.decode("#FCFCFC"))
        self.display.add(self.items, BorderLayout.CENTER)
        self.frame.add(self.display,BorderLayout.EAST)
        self.frame.setVisible(True)
        self.updateStocks()

    def updateStocks(self):
        companys = ["MSFT","SNDK","GOOGL","NOK","EMC","HPQ","IBM","EBAY","AAPL","AMZN"]
        tempList = []
        for company in companys:
            #Quote = web()
            #companyQuote = Quote.getQuote(company)
            companyQuote = len(str(company)) * 314.15
            tempList.append(company + " " + str(companyQuote))
        self.stocks.clear()
        for item in tempList:
            self.stocks.addElement(item)


def main():
    StocksTest().loadStockDisplay()

if __name__ == '__main__':
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM