2014-04-09 39 views
0

我做了一個愚蠢的隨機語句生成器的練習,我現在試圖讓結果顯示在JLabel中。我很困難,在嘗試按照一些教程獲取控制檯輸出到JTextArea或JLabel後,我決定這不是我想要實現的。我只是想當我點擊我的按鈕時,它運行wordGen類並將其結果輸出到我的JLabel中。JFrame - 輸出方法結果到JLabel

這裏是我的代碼片段:

package proGui; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 


public class HelloWorldGUI2 { 

private static class HelloWorldDisplay extends JPanel { 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
//   g.draw3DRect(20, 30, 100, 100, true); 
    } 

} 

private static class ButtonHandler implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     return wordGen(); // This is me trying to get the button when clicked to run the wordGen class. Obviously completely wrong. 
    } 
} 

public static class wordGen { 
    // Make three sets of words to choose from 
    String[] wordListOne = {"24/7","Terrific","Time-tested","Returned","Tried","Intepreted","Ebay","Large","Small","Medium"}; 
    String[] wordListTwo = {"Oriented","Shared","Aligned","Targeted","Leveraged","Charged","Networked","Centric","Distributed","Postioned"}; 
    String[] wordListThree = {"Bush-beater","Pikestaff","Placket-racket","Hammer-handle","Quim-wedge","Creamstick","Tug-mutton","Kennel-raker","Testicles","Penis","Vagina","Breasts","TallyWhacker","Johnson","Meat","ClamFist","Binlid"}; 

    //find out how many words are in each list 
    int oneLength = wordListOne.length; 
    int twoLength = wordListTwo.length; 
    int threeLength = wordListThree.length; 

    //generate three random numbers 
    int rand1 = (int) (Math.random() * oneLength); 
    int rand2 = (int) (Math.random() * twoLength); 
    int rand3 = (int) (Math.random() * threeLength); 

    //now build a phrase 
    String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3]; 
} 

public static void main(String[] args) { 
    HelloWorldDisplay displayPanel = new HelloWorldDisplay(); 
    JButton okButton = new JButton("New Word Combo"); // Create new button 
    okButton.setFont(new Font("Malina Light",Font.TRUETYPE_FONT,14)); // Assign  custom font to new button 
    ButtonHandler listener = new ButtonHandler(); 
    okButton.addActionListener(listener); 

    //Text Label - For the eventual output of the random sentence generator 
    JLabel jLab = new JLabel(wordGen); 

    JPanel content = new JPanel(); 
    content.setLayout(new BorderLayout()); 
    content.add(displayPanel, BorderLayout.CENTER); 
    content.add(okButton, BorderLayout.SOUTH); 
    content.add(jLab, BorderLayout.NORTH); 

    JFrame window = new JFrame("GUI Test"); // Declaring the variable 'window'  to type JFrame and sets it to refer to a new window object within the statement 
    window.setContentPane(content); 
    window.setSize(250,150); 
    window.setLocation(100,100); 
    window.setVisible(true); 

    } 
} 

無論如何,我爲是n00bish道歉,但是這是我的小編程拼圖的最後一塊!

回答

1

您正在使用的類和方法可以互換。例如,wordGen是一個類,但您試圖調用它,就好像它是一種方法。我改變了你的代碼,所以你可以運行這個例子。我也刪除了不必要的代碼。 HelloWorldDisplayButtonHandler似乎是無用的類。我刪除了它們,並清理了代碼。完整的工作示例:

package proGui; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


public class HelloWorldGUI2 { 

    public static void main(final String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       showGUI(args); 
      } 
     }); 
    } 

    public static void showGUI(String[] args) { 
     JPanel displayPanel = new JPanel(); 
     JButton okButton = new JButton("New Word Combo"); // Create new button 
     okButton.setFont(new Font("Malina Light", Font.TRUETYPE_FONT, 14)); // Assign  custom font to new button 
     final JLabel jLab = new JLabel(); 
     okButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       jLab.setText(wordGen()); 
      } 
     }); 

     //random sentence is set above, see jLab.setText(wordGen()) 
     JPanel content = new JPanel(); 
     content.setLayout(new BorderLayout()); 
     content.add(displayPanel, BorderLayout.CENTER); 
     content.add(okButton, BorderLayout.SOUTH); 
     content.add(jLab, BorderLayout.NORTH); 

     JFrame window = new JFrame("GUI Test"); // Declaring the variable 'window'  to type JFrame and sets it to refer to a new window object within the statement 
     window.setContentPane(content); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setSize(250, 150); 
     window.setLocation(100, 100); 
     window.setVisible(true); 

    } 

    public static String wordGen() { 
     // Make three sets of words to choose from 
     String[] wordListOne = {"24/7", "Terrific", "Time-tested", "Returned", "Tried", "Intepreted", "Ebay", "Large", "Small", "Medium"}; 
     String[] wordListTwo = {"Oriented", "Shared", "Aligned", "Targeted", "Leveraged", "Charged", "Networked", "Centric", "Distributed", "Postioned"}; 
     String[] wordListThree = {"Bush-beater", "Pikestaff", "Placket-racket", "Hammer-handle", "Quim-wedge", "Creamstick", "Tug-mutton", "Kennel-raker", "Testicles", "Penis", "Vagina", "Breasts", "TallyWhacker", "Johnson", "Meat", "ClamFist", "Binlid"}; 

     //find out how many words are in each list 
     int oneLength = wordListOne.length; 
     int twoLength = wordListTwo.length; 
     int threeLength = wordListThree.length; 

     //generate three random numbers 
     int rand1 = (int) (Math.random() * oneLength); 
     int rand2 = (int) (Math.random() * twoLength); 
     int rand3 = (int) (Math.random() * threeLength); 

     //now build a phrase 
     String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3]; 
     return phrase; 
    } 


} 
+0

非常好,非常感謝。正是我想要的!我現在將通過您的編輯並確保我完全理解這一切! – c0d3n4m3

1

第一個你不能在void方法中返回對象。所以你不能做這樣:

public void actionPerformed(ActionEvent e) { 
    return wordGen(); // This is me trying to get the button when clicked to run the wordGen class. Obviously completely wrong. 
} 

但是你可以用另一種方式來實現自己的目標,

您可以創建一個anonymousActionListener類直接到按鈕,這將允許你訪問JLabel,然後添加一個文本要顯示

對於實例

JLabel jLab = new JLabel(wordGen);// note this Label should be final or an instance variable. 
b.addActionListener(new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     jLab.setText(new awordGen().getSomeText());// suppose you have getSomeText method. 
    } 
}); 
0

申報JLabel jLab;全球然後

public static class wordGen 
{ 
//your exact code 
jLabl.setText(phrase); 
} 
0

在您的代碼中,wordGen應該是方法而不是類。 jLab必須是HelloWorldGUI2中的一個字段,它可以從ActionListener訪問。

public class HelloWorldGUI2 { 

    private static JLabel jLab; 

    private static class HelloWorldDisplay extends JPanel { 

     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
//   g.draw3DRect(20, 30, 100, 100, true); 
     } 

    } 

    private static class OkButtonActionListener implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 
      String generatedPhrase = wordGen(); 
      jLab.setText(generatedPhrase); 
     } 
    } 

    public static String wordGen() { 
     // Make three sets of words to choose from 
     String[] wordListOne = {"24/7", "Terrific", "Time-tested", "Returned", "Tried", "Intepreted", "Ebay", "Large", "Small", "Medium"}; 
     String[] wordListTwo = {"Oriented", "Shared", "Aligned", "Targeted", "Leveraged", "Charged", "Networked", "Centric", "Distributed", "Postioned"}; 
     String[] wordListThree = {"Bush-beater", "Pikestaff", "Placket-racket", "Hammer-handle", "Quim-wedge", "Creamstick", "Tug-mutton", "Kennel-raker", "Testicles", "Penis", "Vagina", "Breasts", "TallyWhacker", "Johnson", "Meat", "ClamFist", "Binlid"}; 

     //find out how many words are in each list 
     int oneLength = wordListOne.length; 
     int twoLength = wordListTwo.length; 
     int threeLength = wordListThree.length; 

     //generate three random numbers 
     int rand1 = (int) (Math.random() * oneLength); 
     int rand2 = (int) (Math.random() * twoLength); 
     int rand3 = (int) (Math.random() * threeLength); 

     //now build a phrase 
     String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3]; 
     return phrase; 
    } 

    public static void main(String[] args) { 
     HelloWorldDisplay displayPanel = new HelloWorldDisplay(); 
     jLab = new JLabel(); 
     JButton okButton = new JButton("New Word Combo"); // Create new button 
     okButton.setFont(new Font("Malina Light", Font.TRUETYPE_FONT, 14)); // Assign  custom font to new button 
     okButton.addActionListener(new OkButtonActionListener()); 

     JPanel content = new JPanel(); 
     content.setLayout(new BorderLayout()); 
     content.add(displayPanel, BorderLayout.CENTER); 
     content.add(okButton, BorderLayout.SOUTH); 
     content.add(jLab, BorderLayout.NORTH); 

     JFrame window = new JFrame("GUI Test"); // Declaring the variable 'window'  to type JFrame and sets it to refer to a new window object within the statement 
     window.setContentPane(content); 
     window.setSize(250, 150); 
     window.setLocation(100, 100); 
     window.setVisible(true); 

    } 
} 
+0

謝謝您的輸入!對此,我真的非常感激。 – c0d3n4m3