java - 如何使用带有 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();
}
}
解决方案
推荐阅读
- javascript - 生产中的深奥错误:`类型错误:e未定义`
- python - Seaborn:如何使用多个折线图制作子图?sns.relplot 似乎不支持它?
- apache - WAMP 服务器不允许使用“www”访问网站
- c# - 我的更新运行计算,但将相同的值更新到所有行
- json - 将变量插入 json 有效负载(松弛 webhook)问题的 Bash 脚本
- javascript - 通过 wix 代码提供 HTML 图表时的事件处理程序问题
- python - 有人可以用python和google sheet api帮我解决这个问题吗?
- javascript - 我用 Javascript 设置的一个 cookie 总是为空。其余的读得很好
- debugging - 修改符号文件对源代码文件的引用
- three.js - 具有不同三角形方向的 THREE.js 中的平滑阴影