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

Java 按下按钮时绘制字符串、矩形或椭圆形的GUI,java,user-interface,Java,User Interface,在我的作业中,我必须编写一个程序,根据屏幕顶部按下的按钮,打印一些文本、椭圆形或矩形;当我按下一个按钮时,什么也没发生,我该如何解决这个问题?这是我的第一个图形用户界面,我将感谢任何帮助!我最终需要这个程序:从一个矩形开始,当窗口调整大小时,使屏幕上的任何形状都保持在绘图区域的中心,并且我的椭圆和矩形必须具有显示区域一半的宽度和高度。我一步一个脚印,所以一旦我能在屏幕上得到一个形状,我会尝试找出这些,谢谢:-) 您拥有所有构建块,但按下按钮后,不会调用draw()方法。设置状态(要绘制的项目)后

在我的作业中,我必须编写一个程序,根据屏幕顶部按下的按钮,打印一些文本、椭圆形或矩形;当我按下一个按钮时,什么也没发生,我该如何解决这个问题?这是我的第一个图形用户界面,我将感谢任何帮助!我最终需要这个程序:从一个矩形开始,当窗口调整大小时,使屏幕上的任何形状都保持在绘图区域的中心,并且我的椭圆和矩形必须具有显示区域一半的宽度和高度。我一步一个脚印,所以一旦我能在屏幕上得到一个形状,我会尝试找出这些,谢谢:-)


您拥有所有构建块,但按下按钮后,不会调用draw()方法。设置状态(要绘制的项目)后,需要重新绘制()。我添加了一些
repaint()
s,并帮助您将正在绘制的对象居中。我还将
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;
            }
        }
    }
}
  • 看一看
  • 不要覆盖
    paint
    ,而是使用
    paintComponent
  • 使用
    draw
    方法,将其移动到
    drawStuff
    类中。从
    paintComponent
    方法调用
    drawStuff
  • 设置某种标志(在
    drawStuff
    中)来确定应该绘制什么
  • 处理按钮事件时,更改面板上的状态标志并重新绘制面板
  • 这将要求您的框架引用
    drawStuff


    您可能还想看看并使用

    问题是什么?@Reimeus编辑了这个问题,这样实际上就是在问一个问题,谢谢您指出!当我尝试编译时,它给了我以下错误:
    firstGUI.java:74:error:找不到symbol super.paintComponent(g)
    firstGUI.java:71:error:method不重写或实现超类型中的方法
    对不起,您应该在Jpanel`中包含
    paintComponent。等待update@gdhc21我有一个正在运行的版本。我也做了一些修改。您需要
    g.drawString(“Hello”,getHeight()/2,getWidth()/2)而不是
    g.drawString(“Hello”,0,0)
    和顶部的
    showRect
    设置为
    true
    。非常感谢!很好用!
    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;
                }
            }
        }
    }