简体   繁体   English

使用Eclipse的Java GUI应用程序

[英]GUI application in Java using eclipse

So this was the example given in the notes for my current project. 这就是我当前项目的注释中给出的示例。 Usually I run the the example code and play around with it to see how everything works. 通常,我会运行示例代码并对其进行处理,以查看一切如何工作。 However i'm not sure how to properly call the functions in the example code. 但是我不确定如何正确调用示例代码中的函数。

This what was given to me: 这是给我的:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;

public class Draw extends JPanel implements ActionListener
{
    JTextField tfInfo;
    JLabel lblColor, lblShapes;
    JCheckBox cbRed, cbBlue;
    ButtonGroup shapes;
    JRadioButton rbCircle, rbSquare;
    JButton btnSubmit; 
    public Draw() 
    {
        setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
        tfInfo = new JTextField("Color & Shapes", 15);
        lblColor = new JLabel("Colors:");
        cbRed = new JCheckBox("Red");
        cbBlue = new JCheckBox("Blue");
        lblShapes = new JLabel("Shapes:");
        shapes = new ButtonGroup();
        rbCircle = new JRadioButton("Circle");
        rbSquare = new JRadioButton("Square");
        btnSubmit = new JButton("Submit"); 
        btnSubmit.addActionListener(this);
        add(tfInfo);
        add(lblColor);
        add(cbRed); 
        add(cbBlue); 
        add(lblShapes);
        add(rbCircle);
        add(rbSquare);
        add(btnSubmit);
        shapes.add(rbCircle);
        shapes.add(rbSquare); 
    }
    public static void main(String [] args)
    {
        Draw n = new Draw();
        n.setVisible(true);
    }
    public void actionPerformed(ActionEvent a)
    {
        if(a.getSource() == btnSubmit)
        { 
            if(cbRed.isSelected()&&cbBlue.isSelected())
            {
                if(rbCircle.isSelected())
                {
                    tfInfo.setText("urple Circle"); 
                }
                else if(rbSquare.isSelected())
                {
                    tfInfo.setText("Purple Square");
                } 
            }
            else if(cbRed.isSelected())
            {   
                if(rbCircle.isSelected())
                { 
                    tfInfo.setText("Red Circle");
                }
                else if(rbSquare.isSelected())
                {
                    tfInfo.setText("Red Square");
                }    
            }
            else if(cbBlue.isSelected())
            {  
                if(rbCircle.isSelected())
                {
                    tfInfo.setText("Blue Circle"); 
                } 
            }
            else if(rbSquare.isSelected())
            {
                tfInfo.setText("Blue Square");
            } 
        }
    }
    public class MApp extends JPanel implements MouseListener 
    {
        private boolean clicked; 
        private Rectangle r; 
        public MApp()
        {
            clicked = false;
            r = new Rectangle(10, 10, 50, 50); 
            addMouseListener(this);
        }
        public void paintComponent(Graphics g)
        {
            if(clicked)
            {
                g.setColor(Color.BLUE);
            }
            else
            {
                g.setColor(Color.RED);
            } 
            g.fillRect((int)r.getX(), (int)r.getY(),
            (int)r.getWidth(), (int)r.getHeight()); 
        }
        public void mouseClicked (MouseEvent e) 
        {
            Point p = new Point(e.getX(),e.getY()); 
            if(r.contains(p))
            {
                clicked = !clicked; 
            }
            repaint(); 
        }
        public void mousePressed (MouseEvent evnt) {}
        public void mouseReleased (MouseEvent evnt) {}
        public void mouseEntered (MouseEvent evnt) {}
        public void mouseExited (MouseEvent evnt) {} 
    }
}

I think I would break it down into these parts: 我想我可以将其分为以下几个部分:

1) put the MApp into it's own java file. 1)将MApp放入其自己的Java文件中。 Also, I changed the repaint() in the mouseClicked to: paintComponent(getGraphics()); 另外,我将mouseClicked中的repaint()更改为:paintComponent(getGraphics()); Your new java file will look like this: 您的新Java文件将如下所示:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

public class MApp extends JPanel implements MouseListener 
{
    private boolean clicked; 
    private Rectangle r; 
    public MApp()
    {
        clicked = false;
        r = new Rectangle(10, 10, 50, 50); 
        addMouseListener(this);
    }
    public void paintComponent(Graphics g)
    {
        if(clicked)
        {
            g.setColor(Color.BLUE);
        }
        else
        {
            g.setColor(Color.RED);
        } 
        g.fillRect((int)r.getX(), (int)r.getY(),
        (int)r.getWidth(), (int)r.getHeight()); 
    }
    public void mouseClicked (MouseEvent e) 
    {
        Point p = new Point(e.getX(),e.getY()); 
        if(r.contains(p))
        {
            clicked = !clicked; 
        }
        paintComponent(getGraphics()); 
    }
    public void mousePressed (MouseEvent evnt) {}
    public void mouseReleased (MouseEvent evnt) {}
    public void mouseEntered (MouseEvent evnt) {}
    public void mouseExited (MouseEvent evnt) {} 
}

Next: Create a main file like so: 下一步:创建一个主文件,如下所示:

import javax.swing.JFrame;

public class Tester {

    public static JFrame window = new JFrame("Graphics");

    public static void main(String[] args) {
        window.setBounds(100, 100,800, 800);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(null);

        MApp m = new MApp();
        m.setBounds(100,100,50,50);
        window.add(m);

        Draw d = new Draw();
        d.setBounds(0, 0, window.getWidth(), 80);
        window.add(d);

        window.setVisible(true);
    }

}

(Don't forget to delete the MApp class from your Draw class) (不要忘记从Draw类中删除MApp类)

You'll be able to run your code and view what's going on... This is just a quick example with a few shortcuts but you'll get the idea 您将能够运行您的代码并查看发生了什么...这只是一个带有一些快捷方式的快速示例,但您会明白的

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

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