0

我正在尝试在 ActionListener 类的 actionPerformed() 方法中绘制一个 Shape 对象并将其添加到 InternalFrame 中。

public class InternalFrame extends JInternalFrame{

    public JSlider redSlider, greenSlider, blueSlider;
    public TextField redField = new TextField();
    public TextField greenField = new TextField();
    public TextField blueField = new TextField();
    Checkbox checkBox = new Checkbox("Filled", true);
    String[] shapeNames = {"Oval", "Rectangle", "Line", "NewShape"};

    public InternalFrame(String string, boolean b,
                    boolean c, boolean d, boolean e) {
        super(string,b,c,d);

        this.setLayout(new BorderLayout());

        redSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 256, 32);
        greenSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 256, 32);
        blueSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 256, 32);

        redField.setText(" " + redSlider.getValue());
        greenField.setText(" " + greenSlider.getValue());
        blueField.setText(" " + blueSlider.getValue());

        redSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                // TODO Auto-generated method stub
                redField.setText(" " + redSlider.getValue());
                //System.out.println("red value: " + redSlider.getValue());
            }
        });

        greenSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                // TODO Auto-generated method stub
                greenField.setText(" " + greenSlider.getValue());
            }
        });

        blueSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                // TODO Auto-generated method stub
                blueField.setText(" " + blueSlider.getValue());
            }
        });

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(1, 4, 6, 0));

        JComboBox shapesDropDown = new JComboBox(shapeNames);
        shapesDropDown.setSelectedIndex(0);

        shapesDropDown.addActionListener(new ActionListener (){     
            @Override
            public void actionPerformed(ActionEvent arg0) {
                //if(arg0.getSource() == shapeNames[1] ){
                MyShape myShape = new MyRectangle();
                add(myShape, BorderLayout.CENTER);

                System.out.println("HHHH");
                //}
            }
        });

        JPanel empty1 = new JPanel();
        JPanel empty2 = new JPanel();

        topPanel.add(empty1);
        topPanel.add(shapesDropDown);
        topPanel.add(checkBox);
        topPanel.add(empty2);

        JPanel sliderPanel = new JPanel();
        sliderPanel.setLayout(new GridLayout(3, 3));

        sliderPanel.add(new Label("Red:"));
        sliderPanel.add(redSlider);
        sliderPanel.add(redField);
        sliderPanel.add(new Label("Green:"));
        sliderPanel.add(greenSlider);
        sliderPanel.add(greenField);
        sliderPanel.add(new Label("Blue:"));
        sliderPanel.add(blueSlider);
        sliderPanel.add(blueField);

        add(topPanel, BorderLayout.NORTH);
        add(sliderPanel, BorderLayout.SOUTH);
    }
}

形状对象也被创建,但它没有显示在 InternalFrame 上。

4

1 回答 1

1

代替扩展JInternalFrame,使用工厂方法来创建内部框架及其内容。在本中,createInternalFrame()在构造函数中调用,但也可以从 an 调用ActionListener。请注意,内部框架的默认布局是BorderLayout,默认放置是CENTER; pack()在调整容器的大小和可见性之前,请确保容器。

图片

于 2013-09-21T17:50:34.703 回答