简体   繁体   English

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

[英]Can't draw a oval in the jpanel when a button is clicked

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();
    }

}

I am new to the Java.This might be a stupid question to ask.I tried to draw an oval in the leftPanel when the oval button is clicked. 我是Java的新手,这可能是一个愚蠢的问题。单击椭圆形按钮时,我试图在leftPanel中绘制一个椭圆形。 But nothing happened when I clicked the Button. 但是当我单击按钮时,什么也没发生。 Can someone help me out plz? 有人可以帮我吗?

Your MyPanel1 panel has a size of (0, 0) so there is nothing to paint. MyPanel1面板的大小为(0,0),因此无需绘制任何内容。

Override the getPreferredSize() method of the class to return the Dimension of your custom painting. 重写类的getPreferredSize()方法以返回自定义绘画的Dimension。

Read the section from the Swing tutorial on Custom Painting for more information and a working example. 阅读Swing教程中有关Custom Paint的部分,以获取更多信息和工作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM