我编写了这段代码,询问用户要显示哪个形状,然后将其显示在 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);
}
}
最佳答案
天哪,不要使用在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 类型的交互——只是绘画。
关于java - 如何在 JFrame 框中显示用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33705254/