Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java GUI中的用户列表形状选择中随机绘制大小的形状_Java_Swing_Jlist_Listselectionlistener - Fatal编程技术网

从Java GUI中的用户列表形状选择中随机绘制大小的形状

从Java GUI中的用户列表形状选择中随机绘制大小的形状,java,swing,jlist,listselectionlistener,Java,Swing,Jlist,Listselectionlistener,我必须在JavaGUI中生成一个随机大小的形状(从50到300像素)。有一个包含3个形状(矩形、正方形、圆形)的列表供用户选择,当用户选择其中一个形状时,GUI中会出现一个随机大小的矩形、正方形或圆形 我想我只是不知道在哪里以及如何实现列表选择侦听器 以下是我目前的代码: import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener

我必须在JavaGUI中生成一个随机大小的形状(从50到300像素)。有一个包含3个形状(矩形、正方形、圆形)的列表供用户选择,当用户选择其中一个形状时,GUI中会出现一个随机大小的矩形、正方形或圆形

我想我只是不知道在哪里以及如何实现列表选择侦听器

以下是我目前的代码:

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;

public class ShapeSelectionWindow extends JPanel implements ListSelectionListener{

    public void paintComponent(Graphics g) {
        int x, y, width, height;
        super.paintComponent(g);

        width = (int)Math.floor(Math.random()*250) + 50;
        height = (int)Math.floor(Math.random()*250) + 50;
        x = (int)Math.floor((615 - width) / 2);
        y = (int)Math.floor((661 - height) / 2);

        g.fillRect(x, y, width, height);
    }

    public static void main(String[] args) {
        ShapeSelectionWindow ssw = new ShapeSelectionWindow();
        JFrame jf = new JFrame();
        JPanel shapeListPanel = new JPanel();
        shapeListPanel.setBackground(Color.WHITE);
        DefaultListModel<String> dlm = new DefaultListModel<String>();
        dlm.addElement("Rectangle");
        dlm.addElement("Square");
        dlm.addElement("Circle");
        JList<String> shapeList = new JList<String>(dlm);
        shapeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        shapeListPanel.add(shapeList);

        jf.getContentPane().setLayout(new BorderLayout());
        jf.getContentPane().add(ssw, BorderLayout.CENTER);
        jf.getContentPane().add(shapeListPanel, BorderLayout.EAST);
        jf.setTitle("Simple Drawing GUI");
        jf.setSize(700, 700);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        // TODO Auto-generated method stub

    }
}
import javax.swing.*;
导入javax.swing.event.ListSelectionEvent;
导入javax.swing.event.ListSelectionListener;
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Graphics;
公共类ShapeSelectWindow扩展JPanel实现ListSelectionListener{
公共组件(图形g){
整数x,y,宽度,高度;
超级组件(g);
宽度=(int)数学地板(Math.random()*250)+50;
高度=(int)数学地板(Math.random()*250)+50;
x=(int)数学地板((615-宽度)/2);
y=(整数)数学楼层((661-高度)/2);
g、 fillRect(x,y,宽度,高度);
}
公共静态void main(字符串[]args){
ShapeSelectWindow ssw=新的ShapeSelectWindow();
JFrame jf=新JFrame();
JPanel shapeListPanel=新的JPanel();
形状板.立根(颜色.白色);
DefaultListModel dlm=新的DefaultListModel();
dlm.addElement(“矩形”);
dlm.附录(“方形”);
dlm.附录(“圆圈”);
JList shapeList=新JList(dlm);
shapeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
shapeListPanel.add(shapeList);
jf.getContentPane().setLayout(新的BorderLayout());
jf.getContentPane().add(ssw,BorderLayout.CENTER);
jf.getContentPane().add(shapeListPanel,BorderLayout.EAST);
jf.setTitle(“简单绘图GUI”);
jf.设置大小(700700);
jf.setDefaultCloseOperation(JFrame.EXIT\u ON\u CLOSE);
jf.setVisible(真);
}
@凌驾
public void值已更改(ListSelectionEvent e){
//TODO自动生成的方法存根
}
}

当前的Paint Component方法用于绘制矩形。正如我所说,我不知道如何实现此项目的列表选择侦听器,以便根据用户的选择生成不同的形状。

您当前的方法是将太多的鸡蛋放在一个篮子中。为什么
ShapeSelectionWindow
会负责管理
JList
?它的唯一责任是绘制一个随机形状

相反,您应该分解您的设计

我将首先定义一个简单的“形状”实体

public enum Shape {
    RECTANGLE("Rectangle"), SQUARE("Square"), CIRCLE("Circle");

    private String name;

    private Shape(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return getName();
    }

}
这只是提供了一组可以使用的有限选项

然后我会更新“形状窗格”以支持这些选项

public class ShapePane extends JPanel {

    private Shape shape;

    public void setShape(Shape shape) {
        this.shape = shape;
        repaint();
    }

    public Shape getShape() {
        return shape;
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(800, 800);
    }

    public void paintComponent(Graphics g) {
        int x, y, width, height;
        super.paintComponent(g);
        Shape shape = getShape();
        if (shape == null) {
            return;
        }

        width = (int) Math.floor(Math.random() * 250) + 50;
        height = (int) Math.floor(Math.random() * 250) + 50;
        x = (int) Math.floor((615 - width) / 2);
        y = (int) Math.floor((661 - height) / 2);

        switch (shape) {
            case RECTANGLE:
                g.fillRect(x, y, width, height);
                break;
            case SQUARE:
                break;
            case CIRCLE:
                break;
        }
    }
}
ShapePane
不关心
Shape
是如何指定的,它只关心它何时更改并想要绘制它

然后,我将使用另一个组件作为
JList
ShapePane
之间的主控制器

public class MainPane extends JPanel {

    private JList<Shape> list;
    private ShapePane shapePane;

    public MainPane() {
        setLayout(new BorderLayout());

        DefaultListModel<Shape> model = new DefaultListModel<>();
        model.addElement(Shape.SQUARE);
        model.addElement(Shape.RECTANGLE);
        model.addElement(Shape.CIRCLE);

        shapePane = new ShapePane();

        list = new JList<Shape>(model);
        list.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                Shape shape = list.getSelectedValue();
                shapePane.setShape(shape);
            }
        });

        add(list, BorderLayout.WEST);
        add(shapePane, BorderLayout.CENTER);
    }

}
public类主窗格扩展了JPanel{
私人名单;
私人ShapePane ShapePane;
公共主窗格(){
setLayout(新的BorderLayout());
DefaultListModel=新的DefaultListModel();
模型.附加元素(形状.正方形);
模型.附加元素(形状.矩形);
模型.加法(形.圆);
shapePane=新的shapePane();
列表=新JList(型号);
addListSelectionListener(新的ListSelectionListener(){
@凌驾
public void值已更改(ListSelectionEvent e){
Shape=list.getSelectedValue();
形状面板。设置形状(形状);
}
});
添加(列表,BorderLayout.WEST);
添加(shapePane、BorderLayout.CENTER);
}
}
可运行的示例。。。
导入java.awt.BorderLayout;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入javax.swing.DefaultListModel;
导入javax.swing.JFrame;
导入javax.swing.JList;
导入javax.swing.JPanel;
导入javax.swing.event.ListSelectionEvent;
导入javax.swing.event.ListSelectionListener;
公开课考试{
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
JFrame=新JFrame();
frame.add(新主窗格());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共枚举形状{
矩形(“矩形”)、正方形(“正方形”)、圆形(“圆形”);
私有字符串名称;
私有形状(字符串名称){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
@凌驾
公共字符串toString(){
返回getName();
}
}
公共类主窗格扩展了JPanel{
私人名单;
私人ShapePane ShapePane;
公共主窗格(){
setLayout(新的BorderLayout());
DefaultListModel=新的DefaultListModel();
模型.附加元素(形状.正方形);
模型.附加元素(形状.矩形);
模型.加法(形.圆);
shapePane=新的shapePane();
列表=新JList(型号);
addListSelectionListener(新的ListSelectionListener(){
@凌驾
public void值已更改(ListSelectionEvent e){
Shape=list.getSelectedValue();
形状面板。设置形状(形状);
}
});
添加(列表,BorderLayout.WEST);
添加(shapePane、BorderLayout.CENTER);
}
}
公共类ShapePa
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public enum Shape {
        RECTANGLE("Rectangle"), SQUARE("Square"), CIRCLE("Circle");

        private String name;

        private Shape(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            return getName();
        }

    }

    public class MainPane extends JPanel {

        private JList<Shape> list;
        private ShapePane shapePane;

        public MainPane() {
            setLayout(new BorderLayout());

            DefaultListModel<Shape> model = new DefaultListModel<>();
            model.addElement(Shape.SQUARE);
            model.addElement(Shape.RECTANGLE);
            model.addElement(Shape.CIRCLE);

            shapePane = new ShapePane();

            list = new JList<Shape>(model);
            list.addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    Shape shape = list.getSelectedValue();
                    shapePane.setShape(shape);
                }
            });

            add(list, BorderLayout.WEST);
            add(shapePane, BorderLayout.CENTER);
        }

    }

    public class ShapePane extends JPanel {

        private Shape shape;

        public void setShape(Shape shape) {
            this.shape = shape;
            repaint();
        }

        public Shape getShape() {
            return shape;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 800);
        }

        public void paintComponent(Graphics g) {
            int x, y, width, height;
            super.paintComponent(g);
            Shape shape = getShape();
            if (shape == null) {
                return;
            }

            width = (int) Math.floor(Math.random() * 250) + 50;
            height = (int) Math.floor(Math.random() * 250) + 50;
            x = (int) Math.floor((615 - width) / 2);
            y = (int) Math.floor((661 - height) / 2);

            switch (shape) {
                case RECTANGLE:
                    g.fillRect(x, y, width, height);
                    break;
                case SQUARE:
                    break;
                case CIRCLE:
                    break;
            }
        }
    }

}