这是我第一次发帖,请善待并知道我会记得解决任何建议!
我正在尝试创建一个绘制形状的程序。我需要我的程序一次只显示一个选择,圆形,方形或三角形。当我单击“绘制...”选项时,它应该仅绘制相应的形状,并在侧面标记高度或半径。形状是固定大小,但标签需要反映用户输入的内容。
但是,我正在使用switch语句在它们之间切换。我的开关立即显示所有内容!我该如何解决?
这是我的代码:
import java.awt.event.ActionEvent;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
public class MainPanel extends JPanel implements ActionListener{
JTextField num1;
JTextField num2;
JTextField num3;
JTextField num4;
JLabel equals;
JTextField answer;
JLabel labelTxt;
JComboBox shapeList;
JButton drawAThing;
JLabel radiusLabel;
JLabel base;
JLabel lengthLabel;
JLabel height;
JButton drawACircle;
JButton drawASquare;
JButton drawATriangle;
MainPanel(){
String [] shapes = {"circle", "square", "triangle"};
shapeList = new JComboBox(shapes);
shapeList.setSelectedIndex(2);
shapeList.addActionListener(this);
add(shapeList);
}
@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
Object a = e.getSource();
if (o == shapeList){
JComboBox cb = (JComboBox)e.getSource();
String msg = (String)cb.getSelectedItem();
switch(msg){
case "circle":
radiusLabel = new JLabel();
radiusLabel.setText("Radius: ");
add(radiusLabel);
num1 = new JTextField(12);
add(num1);
drawACircle = new JButton("Draw a circle ");
drawACircle.addActionListener(this);
add(drawACircle);
if (a == drawACircle){
drawACircleThing();
remove(drawACircle);
this.revalidate();
this.repaint();
}
break;
case "square":
lengthLabel = new JLabel();
lengthLabel.setText("Side Length: ");
add(lengthLabel);
num2 = new JTextField(12);
add(num2);
drawASquare = new JButton("Draw a square ");
drawASquare.addActionListener(this);
add(drawASquare);
if (a == drawASquare){
drawASquareThing();
remove(drawASquare);
this.revalidate();
this.repaint();
}
break;
case "triangle":
base = new JLabel();
base.setText("Base: ");
add(base);
num3 = new JTextField(12);
add(num3);
height = new JLabel();
base.setText("height: ");
add(height);
num4 = new JTextField(12);
add(num4);
drawATriangle = new JButton("Draw a triangle ");
drawATriangle.addActionListener(this);
add(drawATriangle);
if (a == drawATriangle){
drawATriangleThing();
remove(drawATriangle);
this.revalidate();
this.repaint();
}
break;
default:
break;
}
}
}
@Override
public void paintComponent(Graphics g)
{
g.setColor(Color.BLUE );
g.fillRect(50, 50, 200, 200);
g.fillOval(100,100,200,200);
}
private void drawASquareThing()
{
Graphics g = getGraphics();
g.setColor(Color.green);
g.fillRect(150,100,330,200);
g.drawString("My square", 50, 10);
}
private void drawATriangleThing()
{
Graphics g = getGraphics();
g.setColor(Color.yellow);
g.fillRect(150,100,330,200);
g.drawString("My Triangle", 50, 10);
}
public void drawACircleThing()
{
Graphics g = getGraphics();
g.setColor(Color.ORANGE);
int [] xPoints = {50, 50, 100};
int [] yPoints = {50, 100, 100};
g.drawPolygon(xPoints, yPoints, 3);
g.fillPolygon(yPoints, xPoints, 3);
}
}
我的新代码:
import java.awt.event.ActionEvent;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
public class MainPanel extends JPanel implements ActionListener{
JTextField num1;
JTextField num2;
JTextField num3;
JTextField num4;
JTextField answer;
JLabel labelTxt;
JComboBox shapeList;
JButton drawAThing;
JLabel radiusLabel;
JLabel base;
JLabel lengthLabel;
JLabel height;
JButton drawACircle;
JButton drawASquare;
JButton drawATriangle;
MainPanel(){
String [] shapes = {"circle", "square", "triangle"};
shapeList = new JComboBox(shapes);
shapeList.setSelectedIndex(2);
shapeList.addActionListener(this);
add(shapeList);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
Object a = e.getSource();
if (o == shapeList){
JComboBox cb = (JComboBox)e.getSource();
String message = (String)cb.getSelectedItem();
switch(message){
case "circle":
radiusLabel = new JLabel();
radiusLabel.setText("Radius: ");
add(radiusLabel);
num1 = new JTextField(12);
add(num1);
int radiusSize = Integer.parseInt(num1.getText());
this.revalidate();
drawACircle = new JButton("Draw a circle ");
drawACircle.addActionListener(this);
add(drawACircle);
if (a == drawACircle){
drawACircleThing(radiusSize);
}
this.repaint();
break;
case "square":
lengthLabel = new JLabel();
lengthLabel.setText("Side Length: ");
add(lengthLabel);
num2 = new JTextField(12);
add(num2);
int lengthSize = Integer.parseInt(num2.getText());
this.revalidate();
drawASquare = new JButton("Draw a square ");
drawASquare.addActionListener(this);
add(drawASquare);
if (a == drawASquare){
drawASquareThing(lengthSize);
}
this.repaint();
break;
case "triangle":
base = new JLabel();
base.setText("Base: ");
add(base);
num3 = new JTextField(12);
add(num3);
int baseSize = Integer.parseInt(num3.getText());
height = new JLabel();
base.setText("height: ");
add(height);
num4 = new JTextField(12);
add(num4);
int heightSize = Integer.parseInt(num4.getText());
this.revalidate();
drawATriangle = new JButton("Draw a triangle ");
drawATriangle.addActionListener(this);
add(drawATriangle);
if (a == drawATriangle){
drawATriangleThing(baseSize, heightSize);
}
this.repaint();
break;
default:
break;
}
}
}
@Override
public void paintComponent(Graphics g) //The last thing that is called and passed by java run time environment of Graphics g. It let’s us draw on the screen.
{
g.setColor(Color.BLUE ); // NEED an import. Don’t need to know all the colors.
g.fillRect(50, 50, 200, 200);
g.fillOval(100,100,200,200);
}
private void drawASquareThing(int lengthSize)
{
Graphics g = getGraphics();
g.setColor(Color.PINK);
g.drawRect(lengthSize, 45, 25, 35);
g.drawString("My Square", 30, 23);
}
private void drawATriangleThing(int baseSize, int heightSize)
{
Graphics g = getGraphics();
g.setColor(Color.GREEN);
g.fillRect(60,79,809,400);
g.drawRect(baseSize, baseSize, heightSize, heightSize);
g.drawString("My Triangle", baseSize, heightSize);
}
public void drawACircleThing(int radiusSize)
{
Graphics g = getGraphics();
g.setColor(Color.ORANGE);
int [] xPoints = {50, 50, 100};
int [] yPoints = {50, 100, 100};
g.drawPolygon(xPoints, yPoints, radiusSize);
g.fillPolygon(yPoints, xPoints, 3);
}
}
答案 0 :(得分:0)
你应该做更多关于自定义绘画的摇摆教程。但基本上,您应该在paintComponent方法中执行所有操作,而在其他方法中,您应该设置paintComponent中使用的变量/参数。例如(使用您的代码)
@Override
public void paintComponent(Graphics g)
{
g.setColor(Color.BLUE );
if (fillSquareFlag) g.fillRect(50, 50, 200, 200);
if (fillCircleFlag) g.fillOval(100,100,200,200);
}
有了这个,您可以为按钮设置按钮操作,如
public void actionPerformed(ActionEvent ae) {
fillSquareFlag = !fillSquareFlag;
repaint();
}