Java基础-java图形化界面(二)

 public CenteredFrame()
   {
	  //获取屏幕大小
	  Toolkit kit = Toolkit.getDefaultToolkit(); //在java中,可使用Toolkit获得系统信息,
	  Dimension screenSize = kit.getScreenSize();//获得屏幕的分辨率
	  int screenHeight = screenSize.height;      //屏幕的高度
	  int screenWidth = screenSize.width;        //屏幕的宽度
 //设定框架的大小
  setSize(screenWidth/2,screenHeight/2);

  //居中显示框架
  int x=(screenWidth-getWidth())/2;
  int y=(screenHeight-getHeight())/2;
  setLocation(x,y);//设定框架的显示位置

  //设定图标
  Image img=new ImageIcon("blue-ball.gif").getImage();
  setIconImage(img);

  //设定框架的标题
  setTitle("CenteredFrame");

猜数

package day4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;


public class GuessNum implements ActionListener{
    int count = 0;
    int Number1;
    JPanel Jpanel;
    JLabel label1;
    JLabel label2;
    JLabel label3;
    JLabel label4;
    JButton Button1;
    JButton Button2;
    JButton Button3;
    JButton Button4;
    JTextField Text;
    Font f=new Font("华文行楷",Font.BOLD,20);
    Font f2=new Font("黑体",Font.BOLD,15);

    public void set_Num()
    {
        Number1 = (int)(Math.random()*1000);
    }

    public GuessNum()
    {
        set_Num();
        System.out.println(Number1);

        JFrame Frame = new JFrame();
        Frame.setSize(600, 400);
        Jpanel = new JPanel();
        Jpanel.setSize(600, 400);
        Jpanel.setBackground(null);
        Jpanel.setLayout(null);

        label1 = new JLabel("你已经猜了" + count + "次");
        label1.setFont(f);
        label1.setBounds(200, 0, 200, 30);
        label1.setVisible(false);

        label4 = new JLabel("答案是:" + String.valueOf(Number1));
        label4.setFont(f);
        label4.setBounds(200, 300, 200, 30);
        label4.setVisible(false);

        label2 = new JLabel("输入猜测的数");
        label2.setBounds(150, 100, 100, 30);
        label2.setFont(f2);

        Text = new JTextField();
        Text.setBounds(250, 100, 100, 30);

        label3 = new JLabel();
        label3.setBounds(190, 50, 100, 30);
        label3.setVisible(false);

        Button1 = new JButton("确认");
        Button1.setFont(f2);
        Button1.setMnemonic(KeyEvent.VK_I);
        Button1.addActionListener(this);
        Button1.setBounds(50, 250, 100, 50);

        Button2 = new JButton("重新开始");
        Button2.setFont(f2);
        Button2.setMnemonic(KeyEvent.VK_I);
        Button2.addActionListener(this);
        Button2.setBounds(200, 250, 100, 50);

        Button3 = new JButton("退出");
        Button3.setFont(f2);
        Button3.setMnemonic(KeyEvent.VK_I);
        Button3.addActionListener(this);
        Button3.setBounds(350, 250, 100, 50);

        Button4 = new JButton("查看答案!");
        Button4.setFont(f2);
        Button4.setMnemonic(KeyEvent.VK_I);
        Button4.addActionListener(this);
        Button4.setBounds(200, 150, 150, 50);

        Jpanel.add(Button1);
        Jpanel.add(Text);
        Jpanel.add(Button2);
        Jpanel.add(Button3);
        Jpanel.add(Button4);
        Jpanel.add(label1);
        Jpanel.add(label2);
        Jpanel.add(label3);
        Jpanel.add(label4);
        Frame.add(Jpanel);
        Frame.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()== Button1)
        {
            label1.setVisible(true);
            count ++;
            label1.setText("你已经猜了" + count + "次");
            String str = new String(Text.getText());
            int Num2 = Integer.parseInt(str);
            if(Num2 == Number1)
            {
                Text.setEditable(false);
                Jpanel.setBackground(null);
                label3.setVisible(true);
                label3.setText("GOOD JOB!");
                label4.setVisible(false);
                Button1.setEnabled(false);
                Button4.setEnabled(false);
            }
            else if(Num2 < Number1)
            {
                Jpanel.setBackground(Color.blue);
                label3.setVisible(true);
                label4.setVisible(false);
                label3.setText("太小");
            }
            else
            {
                Jpanel.setBackground(Color.red);
                label3.setVisible(true);
                label4.setVisible(false);
                label3.setText("太大");
            }
        }
        else if(e.getSource() == Button2)
        {
            label1.setVisible(false);
            Text.setEditable(true);
            label3.setVisible(false);
            Button1.setEnabled(true);
            Text.setText(null);
            Jpanel.setBackground(null);
            count = 0;
            set_Num();
            label4.setText("答案是:" + String.valueOf(Number1));
            label4.setVisible(false);
            Button4.setEnabled(true);
            System.out.println(Number1);
        }
        else if(e.getSource() == Button3)
            System.exit(0);
        else
        {
            label4.setVisible(true);
        }
    }
    public static void main(String[] args) {
        @SuppressWarnings("unused")
        GuessNum Num1;
        Num1 = new GuessNum();
    }

}

绘画函数图像

package day4;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

class DrawFUnction extends JPanel {
    public static final  double ACC=0.01;
    public static final  int STEP=5000;
    private double fun(double x){
            return x*x;
            }
    //重写方法
    public void paintComponent(Graphics graphics){
        super.paintComponent(graphics);
        Graphics2D d1 = (Graphics2D) graphics;

        double x0=(double)(getWidth()/2);
        double y0=(double)(getHeight()/2);
        d1.drawString("o",getWidth()/2-15,getHeight()/2+15);

        Point2D.Double xs = new Point2D.Double((double) getWidth() / 5, (double) getHeight() / 2);
        Point2D.Double xend = new Point2D.Double((double) getWidth() *4/ 5, (double) getHeight() / 2);
        Line2D.Double xline = new Line2D.Double(xs, xend);
        d1.draw(xline);
        Line2D.Double aArr1 = new Line2D.Double(xend, new Point2D.Double(xend.getX() - 10, xend.getY()-10));
        Line2D.Double aArr2= new Line2D.Double(xend, new Point2D.Double(xend.getX() - 10, xend.getY()+10));
        d1.draw(aArr1);
        d1.draw(aArr2);
        d1.drawString("x",getWidth()/5*4-15,getHeight()/2-15);


        Point2D.Double ys = new Point2D.Double((double) getWidth() / 2, (double) getHeight() / 5);
        Point2D.Double yend = new Point2D.Double((double) getWidth() /2, (double) getHeight() / 5*4);
        Line2D.Double yline = new Line2D.Double(ys, yend);
        d1.draw(yline);
        Line2D.Double bArr1 = new Line2D.Double(ys, new Point2D.Double(ys.getX() - 10, ys.getY()+10));
        Line2D.Double bArr2= new Line2D.Double(ys, new Point2D.Double(ys.getX() + 10, ys.getY()+10));
        d1.draw(bArr1);
        d1.draw(bArr2);
        d1.drawString("y",getWidth()/2-15,getHeight()/5-15);
        System.out.println(getWidth()+"   "+getHeight());

        double x=0,y=0;
        double x1=0,y1;
        y=fun(x);
        y1=fun(x1);
        Point2D p = new Point2D.Double(x0, y0);
        Point2D p0 = new Point2D.Double(x0, y0);
        while (y0-y>ACC){
            x=x+1.0/STEP;
            x1=x1-1.0/STEP;
            y1=fun(x1)/10;
            y=fun(x)/10;
            Point2D p1 = new Point2D.Double(x0+x, y0-y);
            Point2D p2 = new Point2D.Double(x0+x1, y0-y1);

            d1.draw(new Line2D.Double(p,p1));
            d1.draw(new Line2D.Double(p0,p2));
            p=p1;
            p0=p2;

        }

	}
}
class CenteredFrame extends JFrame {
    public CenteredFrame(){
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dimension = toolkit.getScreenSize();
        int screenHeight = dimension.height;
        int screenWidth = dimension.width;

        setSize(screenWidth/2, screenHeight/2);

        int x = (screenWidth - getWidth()) / 2;
        int y = (screenHeight - getHeight()) / 2;
        setLocation(x, y);


        setTitle("函数图像");
    }
}
public class FunctionDraw {
    public static void main(String[] args) {
        CenteredFrame centeredFrame = new CenteredFrame();
        centeredFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        centeredFrame.setVisible(true);

        DrawFUnction function = new DrawFUnction();
        centeredFrame.add(function);
    }

}

JOptionPane的一些静态方法

public static void main(String[] args) {
    int i=JOptionPane.showConfirmDialog(null,"你好");
    /**
     * 点击 是 返回0
     * 点击 否 返回1
     * 点击 取消 返回2
     * 点击关闭窗口 返回-1
     */
    System.out.println(i);
    /**
     * 返回用户输入的字符串,然后输入框默认是456,标题是123
     */
    String s=JOptionPane.showInputDialog("123","456");
}

形状

package day4;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Scanner;

abstract class MyShape{
    private double a;
    private double b;
    private double c;
    private double d;
    /*构造函数  set  get方法*/
    public abstract  void draw(Graphics g);
}
class MyLine extends MyShape{
    public MyLine() {
    }

    public MyLine(double a, double b, double c, double d) {
        super(a, b, c, d);
    }
    @Override
    public void draw(Graphics g) {
        Graphics2D g2=(Graphics2D)g;
        //起始坐标x,y终点坐标x,y
        Line2D line =new Line2D.Double(getA(),getB(),getC(),getD());
        g2.draw(line);
    }
}
class DrawFrame extends JFrame
{
    public DrawFrame()
    {
        setTitle("Shapes");
        setSize(400,400);

        //窗体中加入面板
        DrawPanel panel = new DrawPanel();
        add(panel);
    }
}
class DrawPanel extends JPanel
{
    //重写paintComponent方法
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        ArrayList<MyShape> list = new ArrayList<>();
            MyShape s;
            double a,b,c,d;
        for (int j = 0; j <20; j++) {
            a=Math.random()*200;
            b=Math.random()*200;
            c=Math.random()*200;
            d=Math.random()*200;
            int n=(int)(Math.random()*3);
            System.out.println(n);
            switch (n){
                case 0:{
                    s=new MyLine(a,b,c,d);
                    //s.draw(g);
                    list.add(s);
                    break;
                }
                case 1:{
                    s=new MyOval(a,b,c,d);
                    //s.draw(g);
                    list.add(s);
                    break;
                }
                case 2:{
                    s=new MyRectangle(a,b,c,d);
                    //s.draw(g);
                    list.add(s);
                    break;
                }
        }

        }
        for (int i = 0; i <list.size(); i++) {
            list.get(i).draw(g);
        }
    }

    class MyRectangle extends MyShape {

        @Override
        public void draw(Graphics g) {
            Graphics2D g2=(Graphics2D)g;
            //double型的矩形左上方坐标x,y和宽和高
            Rectangle2D rect=new Rectangle2D.Double(getA(),getB(),getC(),getD());
            g2.draw(rect);

        }
        /*构造函数*/
    }

    class MyOval extends MyShape {
        @Override
        public void draw(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            Ellipse2D e=new Ellipse2D.Double(getA(),getB(),getC(),getD());
            g2.draw(e);

        }
        /*构造函数*/
    }
}
public class Shapes {
    public static void main(String[] args) {
        DrawFrame frame=new DrawFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值