如何在GUI文本框而不是Eclipse控制台上打印输出?

时间:2022-05-18 20:45:19

I am trying to allow a user click a btnGenerate which then generates a random number that is assigned to a phrase that can be called to print out the phrase in a box below the button in the application window in eclipse.

我试图允许用户单击btnGenerate,然后生成一个随机数,该数字被分配给一个短语,可以在eclipse的应用程序窗口中的按钮下面的框中打印出该短语。

The problem is that the random statement comes up on the Eclipse console instead of the textbox on my GUI.

问题是随机语句出现在Eclipse控制台而不是GUI上的文本框中。

Any help is appreciated. Here is my code so far:

任何帮助表示赞赏。到目前为止,这是我的代码:

//generate crime button

JButton generateBtn = new JButton("Generate Crime");
generateBtn.setBackground(Color.LIGHT_GRAY);
generateBtn.setFont(new Font("HGHeiseiKakugothictaiW3", Font.BOLD, 20));
GridBagConstraints gbc_generateBtn = new GridBagConstraints();

gbc_generateBtn.fill = GridBagConstraints.BOTH;
gbc_generateBtn.insets = new Insets(0, 0, 5, 5);
gbc_generateBtn.gridx = 15;
gbc_generateBtn.gridy = 5;

frmHeroVillains.getContentPane().add(generateBtn, gbc_generateBtn);
generateBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

        /*random number generator that generates a number between 1-4 and outputs a random crime to the updatePane depending on which 
        number was generated */
        int number =  ((int) (Math.random()*4)+1);
        switch (number) {
        case 1:
            System.out.println("Jewelry Heist on main street!");
            break;
        case 2:
            System.out.println("Mugging in China town!");
            break;
        case 3:
            System.out.println("Boeing 247 - Hijacked!");
        default:
            System.out.println("Nothing to Report.");
            break;


        }
    ;
    JLabel updateLabel = new JLabel("UPDATE ALERT.... " + number);
            GridBagConstraints gbc_updateLabel = new GridBagConstraints();
            gbc_updateLabel.gridheight = 3;
            gbc_updateLabel.insets = new Insets(0, 0, 5, 5);
            gbc_updateLabel.gridx = 15;
            gbc_updateLabel.gridy = 12;
            frmHeroVillains.getContentPane().add(updateLabel, gbc_updateLabel);}    }
);

2 个解决方案

#1


You need to redirect the System.out.println(...) message to your own component.

您需要将System.out.println(...)消息重定向到您自己的组件。

Check out the Message Console for one approach to doing this. You can redirect the output to a JTextArea or JTextPane.

查看消息控制台,了解执行此操作的一种方法。您可以将输出重定向到JTextArea或JTextPane。

#2


public static void main(String[] args) throws Exception {

//Must throws Exception

    JPanel myOutput = new JPanel();
    myOutput.setVisible(true);
    myOutput.setBackground(Color.GRAY);

    JTextArea mynewText = new JTextArea();
    myOutput.add(mynewText);

    URL oracle = new URL("http://www.oracle.com/");
    BufferedReader in = new BufferedReader(new InputStreamReader(
            oracle.openStream()));
//InputStreamReader wrapped in BufferedReader

    String inputLine;
    inputLine = in.readLine();
    mynewText.setText(inputLine);

    in.close();

//In the target window class
mainWindow.add(myOutput);

#1


You need to redirect the System.out.println(...) message to your own component.

您需要将System.out.println(...)消息重定向到您自己的组件。

Check out the Message Console for one approach to doing this. You can redirect the output to a JTextArea or JTextPane.

查看消息控制台,了解执行此操作的一种方法。您可以将输出重定向到JTextArea或JTextPane。

#2


public static void main(String[] args) throws Exception {

//Must throws Exception

    JPanel myOutput = new JPanel();
    myOutput.setVisible(true);
    myOutput.setBackground(Color.GRAY);

    JTextArea mynewText = new JTextArea();
    myOutput.add(mynewText);

    URL oracle = new URL("http://www.oracle.com/");
    BufferedReader in = new BufferedReader(new InputStreamReader(
            oracle.openStream()));
//InputStreamReader wrapped in BufferedReader

    String inputLine;
    inputLine = in.readLine();
    mynewText.setText(inputLine);

    in.close();

//In the target window class
mainWindow.add(myOutput);