JComboBox更改形状和颜色 [英] JComboBox change shapes and colors
问题描述
现在,我有一个可以左右移动矩形的程序.我想制作一个JComboBox来更改对象的形状和颜色.
Right now I have a program that can move a rectangle left and right. I would like to make a JComboBox to change the shape and color of the object.
public class DrawShapes extends JFrame{
//code
private CanvasDrawArea canvas; // the custom drawing canvas (extends JPanel)
/** Constructor to set up the GUI */
public DrawShapes() {
// Panel for JComboBox and buttons
JPanel btnPanel = new JPanel(new FlowLayout());
JComboBox shapes = new JComboBox(shapeName);
btnPanel.add(shapes);
//code for left/right buttons
}
}
因此,上面是包含所有内容的类和设置GUI的构造函数.我也有一个创建形状的内部类
So above is the class that contains everything and the constructor that sets up the GUI. I also have an inner class that creates the shape
class CanvasDrawArea extends JPanel {
public void paintComponent(Graphics rect) {
super.paintComponent(rect);
setBackground(CANVAS_BACKGROUND);
rect.setColor(Color.BLUE);
rect.fillRect(x1, y1, rectWidth, rectHeight); //these int are defined earlier
}
}
我想,如果我想使用JComboBox更改形状,则需要将JComboBox的ActionListener放在CanvasDrawArea类(内部类)中.但是,如果这样做,则无法在btnPanel中添加JComboBox形状.那么我该如何改变被移动物体的形状和颜色呢?
I thought if I wanted to change the shape with the JComboBox, I would need to place the ActionListener for the JComboBox in the CanvasDrawArea class (the inner class). But if I do that, I can't add the shape JComboBox in the btnPanel. So how would I change the shape and color of the object being moved?
此处是编写此问题时的完整代码,以防万一有人想看它.
Here is the full code as of writing this question in case someone wants to look at it.
这是我当前用于按钮的JPanel.我只复制了组合框的代码.
This is my current JPanel for my buttons. I only copied the code for the combo box.
JPanel btnPanel = new JPanel(new FlowLayout());
JComboBox shapes = new JComboBox(shapeName);
shapes.setSelectedIndex(1);
btnPanel.add(shapes);
shapes.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource(); //copies shapes combo box
String msg = (String)cb.getSelectedItem();
switch(msg){
case "Rectangle":
Rectangle blueRect = new Rectangle(x1, y1, rectWidth, rectHeight, blue);
canvas.add(blueRect);
repaint();
case "Circle":
Circle blueCirc = new Circle(x2, y2, circWidth, circHeight, blue);
canvas.add(blueCirc);
repaint();
}//switch end
}//method end
}); //action listener end
这是我当前的Rectangle Class
And here's my current Rectangle Class
public Rectangle(int x, int y, int width, int height, Color color) {
setLocation(x, y);
setSize(width, height);
setColor(color);
}
@Override
public void paint(Graphics g) {
g.setColor(getColor());
g.fillRect(getX(), getY(), getWidth(), getHeight());
}
我的Circle
类与我的Rectangle
类相同.但是当我运行该应用程序时,只有矩形显示,并且不再能够移动.
My Circle
class is the same as my Rectangle
class. But when I run the app, only the rectangle shows up and is no longer able to be moved.
推荐答案
- 首先创建形状"的概念,该概念知道如何在指定位置以指定颜色自行绘制
- 更改您的
CanvasDrawArea
以使其接受该形状"的不同实例并重新粉刷自身. - 创建您的
JComboBox
并将其放置在与CanvasDrawArea
相同的容器中,而不是放置在其中.当用户更改选择时,告诉CanvasDrawArea
的实例相应地更改形状... - Start by creating a concept of a "shape", which knows how to paint it self, at a specified location and with a specified color
- Change your
CanvasDrawArea
to allow it to accept different instances of this "shape" and repaint itself. - Create your
JComboBox
and place it within the same container as theCanvasDrawArea
, but not within it. When the user changes the selection, tell the instance of theCanvasDrawArea
to change the shape accordingly...
核心概念是CanvasDrawArea
负责绘制形状,仅此而已.您可以通过一系列的setter和getter来通过某种方式改变形状,这些设置器提供了灵活性,但并不限制您使用给定的机制(因此,唯一的改变形状是通过触发ActionEvent
) ,但允许您更改形状更改机制的工作方式(例如,通过随机选择,单选按钮或列表)
The core concept is, the CanvasDrawArea
is responsible for drawing the shape, that's it. You allow the shape to be changed by some means, via a series of setters and getters, which provides flexibility, but doesn't tie you into a given mechanism (so that the only to change the shape is by triggering an ActionEvent
), but allows you to change the way in which the shape changing mechanism works (such as via a random selection, radio buttons or list for example)
例如...
class CanvasDrawArea extends JPanel {
//...
private MyShape shape;
//..
public void setShape(MyShape shape) {
this.shape = shape;
repaint();
}
public MyShape getShape() {
return shape;
}
//...
public void paintComponent(Graphics rect) {
super.paintComponent(rect);
MyShape shape = getShape();
if (shape != null) {
shape.paint(rect);
}
}
}
- 例如,不要从任何
paint
方法中更新组件(或任何其他组件)的状态;setBackground(CANVAS_BACKGROUND);
是一个非常糟糕的主意.这将安排另一个重新绘制,这可能导致重新绘制管理器重复重新绘制您的组件,从而消耗您的CPU.相反,您应该事先在构造函数中设置此值... - Don't update the state of your component (or any other component) from within any
paint
method, for example;setBackground(CANVAS_BACKGROUND);
is a really bad idea. This will schedule another repaint which could cause the repaint manager to repeatedly repaint your component, consuming your CPU. Instead you should have been setting this values before hand, perhaps in the constructor...
首先定义形状"的概念并定义其要求,例如...
Start by defining the concept of a "shape" and define it's requirements, for example...
public interface MyShape {
public void setLocation(int x, int y);
public void setSize(int width, int height);
public void setColor(Color color);
public int getX();
public int getY();
public int getWidth();
public int getHeight();
public Color getColor();
public void paint(Graphics g);
}
创建实际的实现,例如...
The create the actual implementations, for example...
public abstract class AbstractShape implements MyShape {
private int x, y;
private int width, height;
private Color color;
@Override
public void setLocation(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void setSize(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
}
@Override
public void setColor(Color color) {
this.color = color;
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
@Override
public Color getColor() {
return color;
}
}
public class Rectangle extends AbstractShape {
public Rectangle() {
}
public Rectangle(int x, int y, int width, int height, Color color) {
setLocation(x, y);
setSize(width, height);
setColor(color);
}
@Override
public void paint(Graphics g) {
g.setColor(getColor());
g.drawRect(getX(), getY(), getWidth(), getHeight());
}
}
在可能的情况下,请始终处理interface
,这意味着当您创建更多形状时,将更易于集成它们
Where possible, always deal with the interface
, this means that when you create more shapes, it will be easier to integrate them
这篇关于JComboBox更改形状和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!