简体   繁体   English

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

[英]GUI that draws a string, rectangle, or oval when a button is pressed

For my assignment, I had to write a program that will either print some text, an oval, or a rectangle depending on which button is pressed at the top of the screen, however; 对于我的作业,我必须编写一个程序,该程序将打印一些文本,椭圆形或矩形,具体取决于在屏幕顶部按下的按钮。 when I press a button nothing happens, how would I fix this? 当我按下按钮时,什么也没发生,我该如何解决? This is my first GUI and I would appreciate any help! 这是我的第一个GUI,不胜感激! I'll end up needing the program to: start out with a rectangle, make whichever shape happens to be on screen stay in the center of the drawing area when the window gets resized, and my ovals and rectangles have to have half the width and height of the display area. 我将最终需要该程序执行以下操作:从矩形开始,使窗口调整大小时,使屏幕上碰巧出现的任何形状都停留在绘图区域的中心,并且我的椭圆形和矩形必须具有一半的宽度,显示区域的高度。 I'm taking this one step at a time so I'll try to figure those out once I can actually get a shape on the screen, thanks :-). 我一次要迈出这一步,所以一旦我可以在屏幕上实际得到一个形状,我就会尽力解决这些问题,谢谢:-)。

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

Try this. 尝试这个。 I added some repaint() s and helped you out with centering the objects being painted. 我添加了一些repaint()并帮助您将要绘制的对象居中。 I also changed the draw to paintComponent . 我还将draw更改为paintComponent This is what you should use when drawing on JComponents 这是在JComponentsJComponents时应该使用的

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

You have all the building blocks, but after the button is pressed, the draw() method isn't called. 您拥有所有构建块,但是在按下按钮后,不会调用draw()方法。 Once you set your state (which item to draw) you need to re-draw() 设置状态(要绘制的项目)后,您需要重新绘制()

  1. Take a look at Performing Custom Painting . 看一看表演定制绘画
  2. Don't override paint but instead use paintComponent . 不要覆盖paint ,而是使用paintComponent
  3. Take your draw method and move it to your drawStuff class. 使用您的draw方法并将其移至drawStuff类。 Call drawStuff from your paintComponent method paintComponent方法调用drawStuff
  4. Set up some kind of flag (in drawStuff ) that determines what should be painted 设置某种标志(在drawStuff ),该标志确定应绘制的内容
  5. When you handle the button event, change the state flag on the panel and repaint the panel.... 处理按钮事件时,请更改面板上的状态标志并重新粉刷面板。

This will require your frame to have a reference to drawStuff 这将要求您的框架具有对drawStuff的引用

You may also want to take a look at and use Code Conventions for the Java Programming Language 您可能还想看看并使用Java编程语言的代码约定

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

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