简体   繁体   English

如何在JFrame框中显示用户输入?

[英]How to show user input in JFrame box?

I have written this code that asks user which shape you want to display and then show it in the JFrame box, my problem is the question is shown in the console box (inside eclipse) not in the JFrame box, so how can I change that? 我写了这段代码,询问用户要显示哪种形状,然后在JFrame框中显示它,我的问题是问题显示在控制台框中(在Eclipse中)而不是JFrame框中,所以我该如何更改?

Also the question is repeated twice am not sure why. 同样的问题被重复两次并不确定为什么。

 public void paintComponent(Graphics g) {
     super.paintComponent(g);
     this.setBackground(Color.WHITE);


     Scanner user_input = new Scanner(System.in);
    int shape_num;
    System.out.println("What is the shape you want to draw? 1- Rectangle 2- Circle");
    shape_num = user_input.nextInt();
     if(shape_num ==1){  
     g.setColor(Color.BLUE);
     g.fillRect(25, 25, 150, 50);
     }
     else if(shape_num ==2) {
     g.setColor(Color.RED);
     g.fillOval(25, 80, 100, 100);
 }
     else if (shape_num  > 2) {
         System.out.println("Error");
     }
 }
     public static void main(String[] args){

            JFrame f = new JFrame("Title");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Rectangle r = new Rectangle();
            f.add(r);
            f.setSize(400, 250);
            f.setVisible(true);


        }

} }

Holy mother of all things precious, don't use a Scanner that uses System.in in the middle of a paintComponent method. 万物之母,不要在paintComponent方法中间使用使用System.in的Scanner。 This will grind your GUI program to a screeching halt. 这将使您的GUI程序陷入停顿。 Seriously. 认真。 This method is one of the most important determinants of the perceived responsiveness of your GUI program, and if you slow it down in any way, or stop it (as you're doing), your program will completely freeze. 此方法是可感知的GUI程序响应能力的最重要决定因素之一,如果您以任何方式降低它的速度或停止它(如执行操作),您的程序将完全冻结。 If you're coding a GUI, then do all user interaction through the GUI, not through the console. 如果要编写GUI,请通过GUI而不是通过控制台进行所有用户交互。

For instance, you could use JRadioButtons that change the state of a field in the GUI, and then draw the image accordingly. 例如,您可以使用JRadioButtons更改GUI中字段的状态,然后相应地绘制图像。 For example: 例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class ItssRectangle extends JPanel {
    private static final String RECTANGLE = "Rectangle";
    private static final String OVAL = "Oval";
    private static final String[] SHAPES = { RECTANGLE, OVAL };
    private static final int PREF_W = 400;
    private static final int PREF_H = 250;
    private String shapeText = "";
    private ButtonGroup buttonGroup = new ButtonGroup();

    public ItssRectangle() {
        RadioBtnListener radioBtnListener = new RadioBtnListener();
        JPanel panel = new JPanel();
        for (String shape : SHAPES) {
            JRadioButton rBtn = new JRadioButton(shape);
            rBtn.setOpaque(false);
            rBtn.setActionCommand(shape);
            rBtn.addActionListener(radioBtnListener);
            panel.add(rBtn);
            buttonGroup.add(rBtn);
        }

        panel.setOpaque(false);
        setLayout(new BorderLayout());
        add(panel, BorderLayout.PAGE_START);

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (RECTANGLE.equals(shapeText)) {
            g.setColor(Color.BLUE);
            g.fillRect(25, 25, 150, 50); // TODO: Get rid of magic numbers
        } else if (OVAL.equals(shapeText)) {
            g.setColor(Color.RED);
            g.fillOval(25, 80, 100, 100); // TODO: Ditto!
        }
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class RadioBtnListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            shapeText = e.getActionCommand();
            repaint();
        }
    }

    private static void createAndShowGui() {
        ItssRectangle mainPanel = new ItssRectangle();

        JFrame frame = new JFrame("ItssRectangle");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

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

Note that in the code above, the paintComponent method is for painting and painting only . 请注意,在上面的代码中,方法的paintComponent是绘画和画。 No user interaction, even of a GUI kind, goes on in there -- just painting. 没有用户交互,即使是GUI类型的交互也没有进行-只是绘画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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