绘制输入字符串

时间:2014-01-09 15:56:33

标签: java swing graphics compiler-errors applet

我希望这个程序能够绘制用户的书写输入。我不知道如何使方法paint将输入n作为变量(n无法解析为变量)。

package naplety;

import java.awt.*;
import javax.swing.*;
import java.util.Scanner;
import java.awt.color.*;
import java.util.Random;

public class Bucky extends JApplet{

    public void ustaw(){

            Scanner input = new Scanner(System.in);
            String n = input.next();
            input.close();
    }

    public void paint(Graphics g){
        Random rand = new Random();
        float red = rand.nextFloat();
        float green = rand.nextFloat();
        float blue = rand.nextFloat();

        Color randomColor = new Color(red,green,blue);
        g.setColor(randomColor);
        g.drawString(n, 100, 100);

    }
}

3 个答案:

答案 0 :(得分:2)

您不应该将控制台输入与GUI代码混合使用。而是从JTextField或JOptionPane获取您的输入。

另外,不要直接在JApplet中绘制,而是在JPanel的paintComponent方法中绘制,然后在applet中显示JPanel。

我自己,我会在JLabel中显示文字:

import java.awt.*;
import javax.swing.*;
import java.util.Random;

public class Bucky extends JApplet {
   private String s = "";
   private JLabel label = new JLabel("", SwingConstants.CENTER);

   @Override
   public void init() {
      Random rand = new Random();
      float red = rand.nextFloat();
      float green = rand.nextFloat();
      float blue = rand.nextFloat();

      Color randomColor = new Color(red, green, blue);
      label.setForeground(randomColor);
      add(label);
      s = JOptionPane.showInputDialog(this, "Enter a String");
      label.setText(s);
   }

}

答案 1 :(得分:0)

抱歉这是错的:

n中传递paint作为参数:

public void paint(Graphics g, String n) {
...
}

正确的解决方案:

将变量n设为类成员:

public class Bucky extends JApplet{
    String n;
    ...
}

答案 2 :(得分:0)

  

“(n无法解析为变量)”

n不在范围内。 paint方法。您需要将其移动为类成员

String n;               <--------
public void ustaw() {

    Scanner input = new Scanner(System.in);
    n = input.next();
    input.close();
}

除此之外,您还应该了解GUI程序。这是事件驱动的。组件fire事件,它是其他组件的监听器。说按下按钮。按下按钮会向侦听器触发事件​​,并且侦听器会在触发事件时提供执行操作。因此,如果您想从用户那里获得输入,您可能需要类似文本字段的内容。您需要向该文本字段注册一个监听器,监听器将说明要执行的操作。请参阅下面的示例,我刚刚使用了您输入的文本字段示例,当您按Enter键时,面板上绘有字符串

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import stackoverflow.Bucky.DrawPanel;
import templates.GUI;

public class BuckyGUI {

    String n = "Hello";
    JTextField jtf = new JTextField(15);
    BuckyPanel panel = new BuckyPanel();
    public BuckyGUI() {


        JFrame frame = new JFrame();
        frame.add(panel, BorderLayout.CENTER);
        frame.add(jtf, BorderLayout.NORTH);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        jtf.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                n = jtf.getText();
                panel.repaint();
            }
        });
    }

    public class BuckyPanel extends JPanel{
        private static final int D_W = 250;
        private static final int D_H = 250;

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawString(n, 125, 125);
        }

        public Dimension getPreferredSize() {
            return new Dimension(D_W, D_H);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new BuckyGUI();
            }
        });
    }
}