单击按钮时无法在jpanel中绘制椭圆

时间:2015-04-22 02:07:35

标签: java swing

import java.util.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import reactiontimer.ReactionTimer;

class MyPanel1 extends JPanel
{

    boolean showEllipse;


    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setPaint(Color.red);
        g2.drawOval(0, 0, 36, 36);
    }


}

public class DrawOval extends JFrame implements ActionListener
{
    Boolean flag1=false;
    Boolean flag2=false;
    Boolean flag3=false;
    JButton name;
    JButton oval;
    JButton rectangle;
    JPanel centerPanel;
    MyPanel1 leftPanel;
    JPanel rightPanel;
    JLabel centerLabel;
    JPanel botPanel;
    JLabel label1;
    JLabel label2;
    JLabel label3;

    public static void main(String args[])
    {
        // [Add Your Name and section number Below]
        Gui rt = new Gui("Chengjie Lin, Section  012");
        rt.setVisible(true);
    }

    public DrawOval(String title)
    {
        setSize(600, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle(title);
        this.setVisible(true);

        Font buttonFont = new Font(Font.SANS_SERIF, Font.BOLD, 24);
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new BorderLayout());
        botPanel=new JPanel();

        name = new JButton("Name");
        name.setFont(buttonFont);
        botPanel.add(name);
        //
        oval = new JButton("Oval");
        oval.setFont(buttonFont);
        botPanel.add(oval);
        //
        rectangle = new JButton("Rectangle");
        rectangle.setFont(buttonFont);
        botPanel.add(rectangle);

        contentPane.add(botPanel, BorderLayout.SOUTH);

        Font bigLabelFont = new Font(Font.SANS_SERIF, Font.BOLD, 48);
        label1=new JLabel("1");
        label1.setFont(bigLabelFont);
        label1.setHorizontalAlignment(SwingConstants.CENTER);

        label2=new JLabel("2");
        label2.setFont(bigLabelFont);
        label2.setHorizontalAlignment(SwingConstants.CENTER);

        label3=new JLabel("3");
        label3.setFont(bigLabelFont);
        label3.setHorizontalAlignment(SwingConstants.CENTER);

        centerPanel = new JPanel();
        leftPanel=new MyPanel1();
        rightPanel=new JPanel();
        leftPanel.setLayout(new BorderLayout());
        rightPanel.setLayout(new BorderLayout());
        centerPanel.setLayout(new BorderLayout());
    //  centerPanel.add(label1, BorderLayout.EAST);
        centerPanel.add(label2, BorderLayout.CENTER);
    //  centerPanel.add(label3, BorderLayout.WEST);
        contentPane.add(leftPanel, BorderLayout.EAST);
        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(rightPanel, BorderLayout.WEST);


        name.addActionListener(this);
        oval.addActionListener(this);
        rectangle.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        String command = e.getActionCommand();
        System.out.println(command);

        // [Add Menu Code 3]
        if (source == name)
        {
            updateView1();
        }
        if (source == oval)
        {
         updateView2();
        }
        if(source==rectangle){


        }


    }

    void updateView1()
    {
        // [ADD Center Panel Code 2]
    if(flag2==false){
        label2.setText("cj");
    }else{
        label2.setText(null);   
    }
        flag2=!flag2;
    repaint();
    }


    void updateView2()
    {
        // [ADD Center Panel Code 2]
        //leftPanel.add(label1);
    if(flag1==false){


    }else{
        //leftPanel.removeAll();

    }
        flag1=!flag1;
    leftPanel.repaint();
    }

}

我是Java的新手。这可能是一个愚蠢的问题。当点击椭圆形按钮时,我试图在leftPanel中绘制一个椭圆。但是当我点击按钮时没有任何反应。有人可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:1)

您的MyPanel1面板的大小为(0,0),因此无需绘制任何内容。

重写类的getPreferredSize()方法以返回自定义绘画的尺寸。

阅读Custom Painting上Swing教程中的部分,了解更多信息和工作示例。