按下按钮后向 JPanel 添加形状

时间:2021-02-07 21:33:55

标签: java

我似乎无法找到我希望是一个简单问题的答案。我正在用 Java 制作一个 GUI,让用户从列表中选择一个形状,输入所需的尺寸,然后当他们按下 Draw 时,它会在 JPanel 中绘制形状。但是,我尝试了多种不同的方法来使其发挥作用,但形状从未出现。这是我目前所拥有的。

在用户选择形状后,运行 UserInterface.java 中的以下代码

case "Circle":
    choiceLabelText.setText("You have selected " + choice);
    Circle myCircle = new Circle(0,0,0);
    try {
        Double r = Double.parseDouble(Dim1_Box.getText());
        Double area = myCircle.getArea(r);
        AreaVolume.setText(String.valueOf(area));
        ShapePanel.add(new DrawCircle(ShapePanel.getWidth(), ShapePanel.getHeight()));
        ShapePanel.revalidate();
        ShapePanel.repaint();
        userInterface.pack();
        userInterface.repaint();                        
    } 
    catch(NumberFormatException e) {
        break;
    }
    break;

它调用 DrawCircle 类,它很简单

public class DrawCircle extends JPanel {
    
    int x = 0;
    int y = 0;
    int w, h;
    
    public DrawCircle(int w, int h) {
        System.out.println("DrawCircle was called");
        this.w = w;
        this.h = h;
    }
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.red);
        g2.fillOval(x, y, w, h);
    }
    
}

这里是完整的 UserInterface.java 文件

public class UserInterface extends JFrame {
    
    JFrame userInterface;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.TextField AreaVolume;
    private javax.swing.JLabel ChooseShapeText;
    private java.awt.TextField Dim1_Box;
    private java.awt.Label Dim1_Label;
    private java.awt.TextField Dim2_Box;
    private java.awt.Label Dim2_Label;
    private java.awt.TextField Dim3_Box;
    private java.awt.Label Dim3_Label;
    private java.awt.TextField Dim4_Box;
    private java.awt.Label Dim4_Label;
    private java.awt.Button DrawButton;
    private javax.swing.JPanel ShapePanel;
    private java.awt.Label choiceLabelText;
    private java.awt.Label label1;
    private java.awt.Choice shapeSelection;
    // End of variables declaration//GEN-END:variables
    
    public UserInterface() {
        this.userInterface = new JFrame();
        initComponents(userInterface);
    }
    
    private JFrame getJFrame(){
        return this.userInterface;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents(JFrame userInterface) {

        ShapePanel = new javax.swing.JPanel();
        shapeSelection = new java.awt.Choice();
        ChooseShapeText = new javax.swing.JLabel();
        choiceLabelText = new java.awt.Label();
        DrawButton = new java.awt.Button();
        Dim1_Box = new java.awt.TextField();
        Dim2_Box = new java.awt.TextField();
        Dim3_Box = new java.awt.TextField();
        Dim4_Box = new java.awt.TextField();
        Dim1_Label = new java.awt.Label();
        Dim2_Label = new java.awt.Label();
        Dim3_Label = new java.awt.Label();
        Dim4_Label = new java.awt.Label();
        AreaVolume = new java.awt.TextField();
        label1 = new java.awt.Label();

        userInterface.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        userInterface.setResizable(false);
       

/* vvv THE JPANEL I AM WORKING WITH */   ShapePanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        javax.swing.GroupLayout ShapePanelLayout = new javax.swing.GroupLayout(ShapePanel);
        ShapePanel.setLayout(ShapePanelLayout);
        ShapePanelLayout.setHorizontalGroup(
            ShapePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        ShapePanelLayout.setVerticalGroup(
            ShapePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 446, Short.MAX_VALUE)
        );
/* ^^^ THE JPANEL I AM WORKING WITH */   

        shapeSelection.add("--- Choose Shape ---");
        shapeSelection.add("Circle");
        shapeSelection.add("Square");
        shapeSelection.add("Triangle");
        shapeSelection.add("Rectangle");
        shapeSelection.add("Sphere");
        shapeSelection.add("Cube");
        shapeSelection.add("Cone");
        shapeSelection.add("Cylinder");
        shapeSelection.add("Torus");
        shapeSelection.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                shapeSelectionItemStateChanged(evt);
            }
        });

        ChooseShapeText.setFont(new java.awt.Font("sansserif", 1, 14)); // NOI18N
        ChooseShapeText.setText("Choose Shape");

        DrawButton.setLabel("Draw!");
        DrawButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DrawButtonActionPerformed(evt);
            }
        });

        Dim1_Label.setText("Radius");

        label1.setText("Area");

        GroupLayout layout = new javax.swing.GroupLayout(userInterface.getContentPane());
        userInterface.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ChooseShapeText)
                            .addComponent(choiceLabelText, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(Dim2_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(Dim3_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(Dim4_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(Dim4_Box, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(Dim2_Box, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(Dim3_Box, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 196, Short.MAX_VALUE)
                                                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(Dim1_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(Dim1_Box, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(DrawButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(AreaVolume, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(shapeSelection, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(10, 10, 10))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(ShapePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(ShapePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(ChooseShapeText)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(shapeSelection, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(choiceLabelText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Dim1_Box, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(133, 133, 133)
                                .addComponent(Dim1_Label, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(Dim3_Box, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(Dim2_Label, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(AreaVolume, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(202, 202, 202)
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Dim2_Box, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(Dim3_Label, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addComponent(Dim4_Box, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(Dim4_Label, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(DrawButton, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(55, 55, 55))
        );

        userInterface.pack();
    }// </editor-fold>//GEN-END:initComponents
    
/* vvv THE METHOD CALLED WHEN THE USER MAKES A SELECTION */   
    private void shapeSelectionItemStateChanged(java.awt.event.ItemEvent evt) {
        String choice = shapeSelection.getSelectedItem();
        switch (choice) {
                case "Circle":
                    choiceLabelText.setText("You have selected " + choice);
                    Circle myCircle = new Circle(0,0,0);
                    try {
                        Double r = Double.parseDouble(Dim1_Box.getText());
                        Double area = myCircle.getArea(r);
                        AreaVolume.setText(String.valueOf(area));
                        int x = 25;
                        int y = 25;
                        //int radius = 5;
                        //Graphics2D circle = new Graphics2D();
                        //this.userInterface.getContentPane().add(label1)
                        //Graphics circle;
                        //int x = ShapePanel.getWidth()/2;
                        //int y = ShapePanel.getHeight()/2;
                        //int radius = 5;
                        //DrawCircle circle = new DrawCircle(1,1,1,1);
                        //ShapePanel.add(circle);
                        //ShapePanel.validate();
                        //ShapePanel.revalidate();
                        //ShapePanel.repaint();
                        //this.userInterface.revalidate();
                        //this.userInterface.repaint();
                        //ShapePanel.paintComponents(circle);
                        //circle.drawOval(x, y, radius, radius);
                        DrawCircle newJPanel = new DrawCircle(x, y, x, y);
                        newJPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
                        //ShapePanel.setForeground(new java.awt.Color(0, 0, 0));
                                //int x = ShapePanel.getWidth()/2;
                                //int y = ShapePanel.getHeight()/2;
                                //ShapePanel.add(new DrawCircle(x, y, 50, 50));

                        javax.swing.GroupLayout ShapePanelLayout = new javax.swing.GroupLayout(newJPanel);
                        newJPanel.setLayout(ShapePanelLayout);
                        ShapePanelLayout.setHorizontalGroup(
                            ShapePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGap(0, 0, Short.MAX_VALUE)
                        );
                        ShapePanel = newJPanel;
                        
                        userInterface.revalidate();
                        userInterface.pack();
                        userInterface.repaint();
                        //userInterface.add(new DrawCircle(x, y, x, y));
                        
                    } 
                    catch(NumberFormatException e) {
                        break;
                    }
                    break;
                case "Square":
                    choiceLabelText.setText("You have selected " + choice);
                    //Square mySquare = new Square(2,0,0);
                    //mySquare.getArea();
                    myCircle = new Circle(0,0,0);
                    try {
                        Double r = Double.parseDouble(Dim1_Box.getText());
                        Double area = myCircle.getArea(r);
                        AreaVolume.setText(String.valueOf(area));
                    } 
                    catch(NumberFormatException e) {
                        break;
                    }
                    break;
                default:
                    break;
            } 
    }
/* ^^^ THE METHOD CALLED WHEN THE USER MAKES A SELECTION */  

    private void DrawButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DrawButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_DrawButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UserInterface().getJFrame().setVisible(true);
            }
        });
        
    }
    
}

任何有用的指针将不胜感激!

编辑:通过删除我所有的测试内容来帮助简化代码

0 个答案:

没有答案