2015-11-14 50 views
1

我已经写了这段代码,询问用户要显示的形状,然后在JFrame框中显示它,我的问题是问题显示在控制台框(内部eclipse)不在JFrame框,所以我怎么能改变?如何在JFrame框中显示用户输入?

此外问题重复两次我不知道为什么。

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); 


     } 

}珍贵的一切事物

回答

2

神圣的母亲,不要使用在的paintComponent方法的中间使用System.in扫描仪。这会将您的GUI程序磨碎成戛然而止。认真。这种方法是GUI程序感知响应的最重要的决定因素之一,如果以任何方式减慢速度,或停止它(如您所做的那样),您的程序将完全冻结。如果你正在编写一个GUI,那么通过GUI进行所有的用户交互,而不是通过控制台。例如,您可以使用JRadioButtons来更改GUI中字段的状态,然后相应地绘制图像。例如:

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(); 
      } 
     }); 
    } 
} 

注意,在上面的代码中,的paintComponent方法是用于与涂装。没有用户交互,即使是GUI类型,也只是绘画。

+0

我可以继续使用您的代码并重新制作矩形/椭圆中给定高度和宽度的部分,并让用户输入他们选择的尺寸? – ItssMohammed

相关问题