我已经编写了这段代码,询问用户要显示哪种形状,然后在JFrame框中显示,我的问题是控制台框中显示的问题(在eclipse中)不在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);
}
}
答案 0 :(得分: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方法用于绘制和绘制 only 。没有用户交互,即使是GUI类型,也在那里继续 - 只是绘画。