首页 > 解决方案 > 如何使用带有 Jbutton 的 actionlistener 根据 JComboBox 中的选项绘制形状?

问题描述

好的,所以我正在尝试创建一个用户选择形状、颜色和大小的程序,但它仅在用户单击绘制形状时显示。这是我认为主要问题是弄清楚如何调用draw方法的代码。我尝试使用 get selected item 方法,但输出中根本没有弹出任何内容。

package com.java24hours;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import javax.swing.*;

public class shape1Student implements ActionListener
{
   int choice;
   int myColor;
   int myShape;
   int mySize;

   JButton drawShape = new JButton("DrawShape");
   JComboBox shape = new JComboBox();
   JComboBox color = new JComboBox();
   JComboBox size = new JComboBox();   
   Drawing draw = new Drawing();
   Color gold = new Color(159, 121, 44);

   public shape1Student()
   {
      JFrame frame = new JFrame("DS Draw Shapes");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JPanel panel = new JPanel();
      panel.add(drawShape);
      panel.add(shape);
      drawShape.setFont(new Font("Book Antiqua",Font.BOLD|Font.ITALIC, 16));
      shape.addItem("Square");
      shape.addItem("Rectangle");
      shape.addItem("Circle");
      shape.addItem("Ellipse");
      panel.add(color);
      color.addItem("Red");
      color.addItem("Blue");
      color.addItem("Custom");
      panel.add(size);
      size.addItem("Small");
      size.addItem("Medium");
      size.addItem("Large");
      drawShape.addActionListener(this);
      frame.add(panel, "North");
      frame.add(draw);
      frame.setSize(500,500);
      frame.setVisible(true);
   }

   public void actionPerformed(ActionEvent e) {
     if (e.getSource() == drawShape)
       {
        choice = 1;
       //get color, shape and size vales from combo what DRAW is clicked, 
       //call draw
      color.getSelectedIndex();
      shape.getSelectedIndex();
      size.getSelectedIndex();
      draw.repaint();
 
       }
    }

class Drawing extends JComponent
     {
      public void paint(Graphics g2)
      {
       Graphics2D g = (Graphics2D) g2;
       int[] sizes = {20, 20, 100, 100};
   
         if (choice == 1) //DRAW!
         
         {

          // set COLOR, SIZE, then fill with shape LAST
                //Set color based on combo box selection
                if (myColor == 1) {//Red was selected
                    g.setColor(Color.red);
            } else if (myColor == 2) {//Custom was selected
                    g.setColor(gold);
            } else if (myColor == 3) {//Blue was selected
                    g.setColor(Color.blue);
            }
            
            if (mySize == 1){
                g.scale(0.5, 0.5);
            } else if (mySize == 2) {
                g.scale(1, 1);
            } else if (mySize == 3) {
                g.scale(2, 2);
            }
            
   
     if (myShape == 1)
     {
        g.fillRect(sizes[0], sizes[1], sizes[2], sizes[3]);
     }
     if (myShape == 2)
     {
           g.fillRect(sizes[0], sizes[2], sizes[1], sizes[3]);
     }
     if (myShape == 3)
     {
            Ellipse2D.Float myEllipse = new Ellipse2D.Float(sizes[0], sizes[1], sizes[2], sizes[3]);
             g.fill(myEllipse);
             g.draw(myEllipse);
     }
     if (myShape == 4)
     {
             Ellipse2D.Float myEllipse = new Ellipse2D.Float(sizes[0], sizes[1], sizes[2] + 50, 
  sizes[3]);
             g.fill(myEllipse);
             g.draw(myEllipse);
     }
  }
  }  
 }
 
public static void main(String[] args)
 {
  new shape1Student();
 }
}

标签: java

解决方案


推荐阅读