油漆和按钮

时间:2014-04-15 21:30:47

标签: java swing jframe

我正在尝试让这个程序在按下按钮时清除图像,每次在文本框中按下回车键时,都会显示一个新的部分。在第一次行动之后似乎没有做任何事情。

import javax.swing.JFrame;

public class PushPicture
{
//-----------------------------------------------------------------
//  Creates the main program frame.
//-----------------------------------------------------------------
public static void main(String[] args)
{
  JFrame frame = new JFrame("Push to Paint");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  frame.getContentPane().add(new PushToPaint());

  frame.pack();
  frame.setVisible(true);
}
}

该计划的内容......

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PushToPaint extends JPanel
{
//local constants
final int MAX = 5;

//local variables
private int count;
private String temp;
private JTextField text;
private JButton clear;
private JLabel title;
private GridLayout main;
ButtonListener listen;

/***********************
* Program Name  : Push to Paint
* Author        : Joshua James
* Date          : April 15, 2014
* Description   : This will initialize all elements and build the panel
*       be be displayed.
**********************/

public PushToPaint()
{
    listen = new ButtonListener();

    //set JButton
    clear = new JButton("Clear");
    clear.addActionListener(listen);

    //set up textfield and label
    text = new JTextField(5);
    text.addActionListener(listen);

    //initialize label
    title = new JLabel ("Enter text. See a picture.");

    //add textfield and label
    add(title);
    add(text);
    add(clear);

    //set window size and color
    setPreferredSize(new Dimension(400,400));
    setBackground(Color.blue);
}

/***********************
* Program Name  : ButtonListener
* Author        : Joshua James
* Date          : April 15, 2014
* Description   : This will react to the actions made
**********************/

private class ButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        if (event.getSource() == text)
        {
            text.setText("");
            count ++;
        }

        else
            count = 0;
    }
}

/***********************
* Program Name  : paintComponent
* Author        : Joshua James
* Date          : April 15, 2014
* Description   : This method will paint in the panel.
**********************/

public void paintComponent(Graphics page)
{
    super.paintComponent(page);

    //set reset parameters
    if (count == MAX)
        count = 0;

    switch(count)
    {
        //clear panel
        case 0:
            repaint();
            break;//end case

        case 1:
            //draw the snowman head
            page.setColor(Color.white);
            page.fillOval(160,100,80,80);
            break;

        case 2:
            //draw the eyes
            page.setColor(Color.red);
            page.fillOval(180,120,10,10);
            page.fillOval(210,120,10,10);
            break;

        case 3:
            //draw the torso
            page.setColor(Color.white);
            page.fillOval(160,180,100,100);
            break;

        case 4:
            //draw the base
            page.setColor(Color.white);
            page.fillOval(140,280,120,120);
            break;

        default:
            break;
    }
}
}

1 个答案:

答案 0 :(得分:0)

请勿在{{1​​}}方法中致电repaint,在paintComponent方法中致电repaint

actionPerformed方法不需要paintComponent,没有理由为什么其他任何人都想要调用它,只是说