实例(一)——线程,图形用户界面的设计,图形处理,布局管理

1. 在窗口设置四个标签:标题的文本为标签一,使用多线程使四个标签按照时间隐藏,显示。(sleep)

Ⅰ、核心代码:

先定义一个基本框架:

 JFrame jf = new JFrame();

设置四个标签和四个面板,用add()方法将标签放入面板中,
用GridLayout布局对四个面板进行布局;

Container cp = jf.getContentPane();
        GridLayout gird = new GridLayout(2, 2);
        cp.setLayout(gird);
        cp.add(p1);
        cp.add(p2);
        cp.add(p3);
        cp.add(p4);

定义一个数组JLable[]将四个标签放入其中,构造一个方法体并用for循环判断该谁执行sleep操作将其隐藏起来多长时间。

public void flush() throws InterruptedException {
        for (int i = 0; i < labArray.length; i++) {
            labArray[i].setVisible(true);
        }

        while (true) {
            for (int i = 0; i < labArray.length; i++) {
                labArray[i].setVisible(false);

                Thread.sleep(500);
                labArray[i].setVisible(true);
            }
        }
    }
Ⅱ、运行结果

这里写图片描述

Ⅲ、源代码见附录


2. 在窗口中用不同的颜色绘制圆,椭圆、圆弧、文本

Ⅰ、核心代码

创建一个继承Jpanel的Mypanel类,调用java.awt.Component类中定义paint(Graphics g)方法。

public class Mypanel extends JPanel {
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.yellow);
            g.drawOval(150, 200, 110, 110);
            g.setColor(Color.cyan);
            g.fillOval(30, 40, 120, 100);
            g.setColor(Color.black);
            g.drawArc(120, 130, 140, 150, 100, 100);
            g.setColor(Color.red);
            g.drawString("绘制圆,圆弧,文本,", 200, 100);
        }
    }
Ⅱ、运行结果

这里写图片描述

Ⅲ、源代码见附录


3. 布局管理(flowlayout),事件处理:在窗口中,有四个按钮加减乘除,有三个文本框,分别表示数字123、单击四个按钮显示运算结果于文本框三

Ⅰ、核心代码

加法实现方法,将对加法事件的事件监听用addActionListerent(new
ActionListener(){public void actionPerformed(ActionEvent e) {})方法添加到按钮b1中:

b1.addActionListener(new ActionListener() {
            @Override
        public void actionPerformed(ActionEvent e) {
            if (t1.getText() != null && t2.getText() != null){ 
                    int a = Integer.parseInt(t1.getText());
                    int b = Integer.parseInt(t2.getText());
                    int count = a + b;
                    String s = String.valueOf(count);
                    t3.setText(s);
            }
            }
        });
Ⅱ、运行结果

这里写图片描述
Ⅲ、源代码见附录


4. 要求界面上左右各有一个文本区,要求中间上下四个按钮选中左边文本区的内容,点击上方按钮,复制到右边文本区,选中右边文本区

Ⅰ、核心代码

文本区的设置,选中文本时所选中的文本背景为蓝色
t1.setSelectedTextColor(Color.blue);
当选中文本区t1文本区不为空时点击按钮b1使其复制到文本区t2中,此时用事件监听的接口实现方法

b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (t1.getSelectedText()!=null){
                    t2.append(t1.getSelectedText()+"\n");
                }
            }
        });
Ⅱ、运行结果

这里写图片描述
Ⅲ、源代码见附录


附录:

1.

public class MyFrame extends JFrame {
    JFrame jf = new JFrame();
    JPanel p1, p2, p3, p4;
    private JLabel lbl1 = new JLabel("标签1");
    private JLabel lbl2 = new JLabel("标签2");
    private JLabel lbl3 = new JLabel("标签3");
    private JLabel lbl4 = new JLabel("标签4");
    private JLabel[] labArray;

    // labArray = new JLabel[]{lab1,lab2,lab3,lab4};
    public MyFrame() {

        p1 = new JPanel();
        p2 = new JPanel();
        p3 = new JPanel();
        p4 = new JPanel();

        p1.add(lbl1);
        p2.add(lbl2);
        p3.add(lbl3);
        p4.add(lbl4);
        labArray = new JLabel[] { lbl1, lbl2, lbl3, lbl4 };

        Container cp = jf.getContentPane();
        GridLayout gird = new GridLayout(2, 2);
        cp.setLayout(gird);
        cp.add(p1);
        cp.add(p2);
        cp.add(p3);
        cp.add(p4);

        // labArray = new JLabel[] { lbl1, lbl2, lbl3, lbl4 };
        jf.setSize(200, 300);
        jf.setVisible(true);

    }

    public void flush() throws InterruptedException {
        for (int i = 0; i < labArray.length; i++) {
            labArray[i].setVisible(true);
        }

        while (true) {
            for (int i = 0; i < labArray.length; i++) {
                labArray[i].setVisible(false);

                Thread.sleep(500);
                labArray[i].setVisible(true);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyFrame f = new MyFrame();
        f.flush();

    }
}

2.

public class Draw extends JFrame {
    JFrame Jf = new JFrame();
    Mypanel mp = new Mypanel();
    JPanel Jp = new JPanel();
    {
        Jf.add(mp);
        Jf.setSize(400, 400);
        Jf.setLocation(400, 400);
        Jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
        Jf.setVisible(true);
    }

    public static void main(String[] args) {
        Draw d = new Draw();
    }

    public class Mypanel extends JPanel {
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.yellow);
            g.drawOval(150, 200, 110, 110);
            g.setColor(Color.cyan);
            g.fillOval(30, 40, 120, 100);
            g.setColor(Color.black);
            g.drawArc(120, 130, 140, 150, 100, 100);
            g.setColor(Color.red);
            g.drawString("绘制圆,圆弧,文本,", 200, 100);

        }

    }
}

3.

public class Count extends JFrame {
    JFrame Jf = new JFrame();

    private JButton b1, b2, b3, b4;

    private JTextField t1, t2, t3;

    public Count() {
         setLayout(new FlowLayout());
        b1 = new JButton("加");
        b2 = new JButton("减");
        b3 = new JButton("乘");
        b4 = new JButton("除");
        t1 = new JTextField(5);
        t2 = new JTextField(5);
        t3 = new JTextField(5);

        b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (t1.getText() != null && t2.getText() != null) {

                    int a = Integer.parseInt(t1.getText());
                    int b = Integer.parseInt(t2.getText());
                    int count = a + b;
                    String s = String.valueOf(count);
                    t3.setText(s);

                }

            }
        });
        b2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (t1.getText() != null && t2.getText() != null) {

                    int a = Integer.parseInt(t1.getText());
                    int b = Integer.parseInt(t2.getText());
                    int count = a - b;
                    String s = String.valueOf(count);
                    t3.setText(s);
                }

            }
        });
        b3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (t1.getText() != null && t2.getText() != null) {

                    int a = Integer.parseInt(t1.getText());
                    int b = Integer.parseInt(t2.getText());
                    int count = a * b;
                    String s = String.valueOf(count);
                    t3.setText(s);
                }
            }
        });
        b4.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (t1.getText() != null && t2.getText() != null) {

                    int a = Integer.parseInt(t1.getText());
                    int b = Integer.parseInt(t2.getText());
                    int count = a / b;
                    String s = String.valueOf(count);
                    t3.setText(s);
                }

            }
        });
        JPanel p1 = new JPanel();
        p1.add(b1);
        p1.add(b2);
        p1.add(b3);
        p1.add(b4);
        JPanel p2 = new JPanel();
        p2.add(t1);
        p2.add(t2);
        p2.add(t3);

        add(p1, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);
        setBounds(100, 100, 370, 150);
        setVisible(true);
        validate();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    }

    public static void main(String[] args) {

        Count c = new Count();

    }

}

4.

public class copy extends JFrame{
    JFrame jf = new JFrame();
    JButton b1,b2;
    JTextArea t1,t2;

    public void go(){
        t1 = new JTextArea(7,20);
        t1.setSelectedTextColor(Color.blue);    //设置选中文本的颜色为蓝色
        t2 = new JTextArea(7,20);
        t2.setSelectedTextColor(Color.blue);

        b1 = new JButton("CopyRight");
        b2 = new JButton("CopyLeft");
        b1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (t1.getSelectedText()!=null){

                    t2.append(t1.getSelectedText()+"\n");
                }
            }
        });

        b2.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        if (t2.getSelectedText()!=null){

                            t1.append(t2.getSelectedText()+"\n");
                        }
                    }
                });


        JPanel panel1 = new JPanel();
        panel1.add(t1);
        JPanel panel2 = new JPanel();
        panel2.add(t2);
        JPanel panel3 = new JPanel();
        panel3.add(b1);
        panel3.add(b2);
        Container cp = jf.getContentPane();
        cp.add(panel1,BorderLayout.WEST);
        cp.add(panel2,BorderLayout.EAST);
        cp.add(panel3,BorderLayout.CENTER);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }



    public static void main(String[] args) {
        copy c = new copy();
        c.go();

    }

}

猜你喜欢

转载自blog.csdn.net/YOLO97/article/details/81610532