java 图形用户界面

1.编程包含一个标签和一个按钮,单击按钮时,标签的内容在”你好”和”再见”之间切换。

package s1;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class MyWin extends JFrame implements ActionListener {
    JButton b=new JButton("点击");
    JLabel l=new JLabel("你好");
    public MyWin(){
        FlowLayout f=new FlowLayout();
        this.setLayout(f);
        this.add(l);
        this.add(b);
        b.addActionListener(this);
        this.setBounds(10,10, 500, 400);
        this.setVisible(true);
        this.validate();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==b){
            if(l.getText().equals("你好"))
                l.setText("再见");
            else if(l.getText().equals("再见"))
                l.setText("你好");
        }
    }   
}
package s1;

public class Test {
    public static void main(String[] a){
        new MyWin();
    }
}

2,编程包含一个文本框和一个文本区域,在文本框中按回车键时,把文本框的内容写入文本区域。

package s2;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class MyWin extends JFrame implements ActionListener{
    JTextField b;
    JTextArea a;
    public MyWin(){
        FlowLayout l=new FlowLayout();
        this.setLayout(l);
        JLabel l2=new JLabel("input:");
        this.add(l2);
        b=new JTextField(30);
        b.addActionListener(this);
        this.add(b);
        a=new JTextArea(20,20);
        this.add(a);
        this.setBounds(100,100,500,400);
        this.setVisible(true);
        this.validate();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==b){
            if(a.getText().equals(""))
                a.setText(b.getText());
            else a.setText(a.getText()+"\n"+b.getText());
            b.setText("");
        }
    }
}
package s2;

public class Test {
    public static void main(String[] a){
        new MyWin();
    }
}

3,试设计一个窗口,窗口界面如下图。包含Style菜单、Color菜单和Exit菜单,Style菜单设计字体的样式(包括Plane、Bold、Italic),Color(红、绿、蓝、自定义)菜单设计字体的颜色、Exit菜单(CloseWindow)退出系统。
这里写图片描述

package s3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Ys extends JFrame{
    JMenuBar mb;
    JMenu me1,me2,me3;
    JMenuItem m1,m2,m3,m4,m5,m6,m7,m8;
    JLabel la;
    Font f;
    Ys(){
        f=new Font("楷体",Font.BOLD,30);
        la=new JLabel("See me?",JLabel.CENTER);
        la.setFont(f);
        add(la);
        mb=new JMenuBar();
        me1=new JMenu("Style");
        me2=new JMenu("Color");
        me3=new JMenu("Exit");
        m1=new JMenuItem("Plain");
        m2=new JMenuItem("Bold");
        m3=new JMenuItem("Italic");
        m4=new JMenuItem("红");
        m5=new JMenuItem("绿");
        m6=new JMenuItem("蓝");
        m7=new JMenuItem("自定义");
        m8=new JMenuItem("CloseWindow");
        setJMenuBar(mb);
        mb.add(me1);
        mb.add(me2);
        mb.add(me3);
        me1.add(m1);
        me1.addSeparator();
        me1.add(m2);
        me1.addSeparator();
        me1.add(m3);
        me2.add(m4);
        me2.addSeparator();
        me2.add(m5);
        me2.addSeparator();
        me2.add(m6);
        me2.addSeparator();
        me2.add(m7);
        me3.add(m8);
        m1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                f=new Font("楷体",Font.PLAIN,30);
                la.setFont(f);
            }

        });
        m2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                f=new Font("楷体",Font.BOLD,30);
                la.setFont(f);
            }

        });
        m3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                f=new Font("Italic",Font.ITALIC,30);
                la.setFont(f);
            }

        });
        m4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                la.setForeground(Color.red);
            }

        });
        m5.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                la.setForeground(Color.green);
            }

        });
        m6.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                la.setForeground(Color.blue);
            }

        });
        m7.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                la.setForeground(Color.pink);
            }

        });
        m8.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
               System.exit(0);
            }

        });
        setBounds(100,100,400,300);
        setVisible(true);
        validate();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
package s3;

public class Test {

    public static void main(String[] args) {
        new Ys();
    }

}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值