按下按钮时绘制字符串,矩形或椭圆的GUI

时间:2013-11-15 01:22:49

标签: java user-interface

对于我的作业,我必须编写一个程序,根据屏幕顶部按下的按钮,打印一些文本,椭圆形或矩形;当我按下按钮没有任何反应时,我该如何解决这个问题呢?这是我的第一个GUI,我将不胜感激任何帮助!我最终需要程序:从矩形开始,当窗口调整大小时,使屏幕上的任何形状保持在绘图区域的中心,我的椭圆和矩形必须有一半的宽度和显示区域的高度。我一步一步走这一步,所以一旦我能在屏幕上形成一个形状,我会尝试解决这些问题,谢谢:-)。

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

public class firstGUI extends JFrame
    implements ActionListener
{
    private boolean showText = false;
    private boolean showRect = false;
    private boolean showOval = false;
    private JButton text;
    private JButton oval;
    private JButton rectangle;
    private JPanel buttonPanel;

    public firstGUI()
    {
        super("First GUI");
        setSize(512, 512);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(1,3));

        text = new JButton("Text");
        text.addActionListener(this);
        buttonPanel.add(text);

        oval = new JButton("Oval");
        oval.addActionListener(this);
        buttonPanel.add(oval);

        rectangle = new JButton("Rectangle");
        rectangle.addActionListener(this);
        buttonPanel.add(rectangle);

        //JComponent drawArea = new JComponent();
        drawStuff d = new drawStuff();

        Container contentPane = this.getContentPane();
        contentPane.add(buttonPanel, BorderLayout.NORTH);
        contentPane.add(d);
    }

    public void actionPerformed(ActionEvent event)
    {
        Object source = event.getSource();

        if (source == text)
        {
            showText = true;
        }
        else if (source == oval)
        {
            showOval = true;
        }
        else if (source == rectangle)
        {
            showRect = true;
        }
    }


    public void draw(Graphics g) 
    {
        if(showText)
        {
            g.drawString("Hello", 0, 0);
        }
        else if (showOval)
        {
            g.drawOval(0, 0, 100, 100);
        }
        else if (showRect)
        {
            g.drawRect(0, 0, 100, 100);
        }
    }

    public static void main(String [] args)
    {
        firstGUI myTest = new firstGUI();
        myTest.setVisible(true);
    }
}

class drawStuff extends JPanel
{
    public void paint(Graphics g)
    {
        super.paint(g);
    }
}

3 个答案:

答案 0 :(得分:1)

试试这个。我添加了一些repaint()并帮助您将绘制的对象居中。我还将draw更改为paintComponent。这是在JComponents

上绘图时应该使用的内容
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class firstGUI extends JFrame
     implements ActionListener {

     private boolean showText = false;
     private boolean showRect = true;
     private boolean showOval = false;
     private JButton text;
     private JButton oval;
     private JButton rectangle;
     private JPanel buttonPanel;
     private DrawStuff drawPanel = new DrawStuff();

     public firstGUI() {
         super("First GUI");
         setSize(512, 512);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         buttonPanel = new JPanel();
         buttonPanel.setLayout(new GridLayout(1, 3));

         text = new JButton("Text");
         text.addActionListener(this);
         buttonPanel.add(text);

         oval = new JButton("Oval");
         oval.addActionListener(this);
         buttonPanel.add(oval);

         rectangle = new JButton("Rectangle");
         rectangle.addActionListener(this);
         buttonPanel.add(rectangle);


         Container contentPane = this.getContentPane();
         contentPane.add(buttonPanel, BorderLayout.NORTH);
         add(drawPanel);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();

        if (source == text) {
            showText = true;
            repaint();
        } else if (source == oval) {
            showOval = true;
            repaint();
        } else if (source == rectangle) {
            showRect = true;
            repaint();
        }
    }

    public static void main(String[] args) {
        firstGUI myTest = new firstGUI();
        myTest.setVisible(true);
    }

    class DrawStuff extends JPanel {

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            if (showText) {
                g.drawString("Hello", getHeight() / 2, getWidth() / 2);
                showText = false;
            } else if (showOval) {
                g.drawOval(getWidth() / 4, getHeight() / 4, getWidth() / 2, getHeight() / 2);
                showOval = false;
            } else if (showRect) {
                g.drawRect(getWidth() / 4, getHeight() / 4, getWidth() / 2, getHeight() / 2);
                showRect = false;
            }
        }
    }
}

答案 1 :(得分:0)

您拥有所有构建块,但在按下按钮后,不会调用draw()方法。设置状态(要绘制的项目)后,需要重新绘制()

答案 2 :(得分:0)

  1. 看看Performing Custom Painting
  2. 请勿覆盖paint,而应使用paintComponent
  3. 使用draw方法将其移至drawStuff课程。从drawStuff方法
  4. 致电paintComponent
  5. 设置某种标志(在drawStuff中),用于确定应绘制的内容
  6. 处理按钮事件时,请更改面板上的状态标志并重新绘制面板....
  7. 这将要求您的框架引用drawStuff

    您可能还想查看并使用Code Conventions for the Java Programming Language