zoukankan      html  css  js  c++  java
  • JAVA图形界面常用知识点总会《代码分析》

    1.

    package CLASS16.bin.com.GridLayout;

    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JSplitPane;

    public class Swing extends JFrame{
    JSplitPane jsp;
    JList jli;
    JLabel jla;
    public static void main(String[] args) {
    Swing a=new Swing();
    }


    public Swing(){
    String[] sh={"boy","gril","brod"};
    jli=new JList(sh);
    jla=new JLabel(new ImageIcon("images/1.jpg"));
    jsp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jli,jla);
    //设置布局管理器
    //添加组件
    this.add(jsp);

    //可以伸缩
    jsp.setOneTouchExpandable(true);
    // this.setBounds(200, 200, 400, 300);
    this.setLocation(200, 200);
    this.setSize(400, 300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setResizable(false);
    this.setVisible(true);

    }

    }

    实现效果  如下:

    2.

    package CLASS16.bin.com.GridLayout;

    import java.awt.BorderLayout;

    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;

    public class QQsay extends JFrame{
    /**
    *
    */
    private static final long serialVersionUID = 1L;
    JTextArea jte=null;
    JScrollPane jsc=null;
    JPanel jpa=null;
    JComboBox jco=null;
    JTextField jtf=null;
    JButton jbu=null;
    public static void main(String[] args) {
    @SuppressWarnings("unused")
    QQsay qq=new QQsay();

    }
    //构造
    public QQsay(){
    jte=new JTextArea();
    //多行文本滚动条
    jsc=new JScrollPane(jte);
    jpa=new JPanel();
    String []sh={"张三","王二"};
    jco=new JComboBox(sh);
    jtf=new JTextField(10);
    jbu=new JButton("发送");
    //设置布局

    //添加组件
    jpa.add(jco);
    jpa.add(jtf);
    jpa.add(jbu);

    //加入到JFrmare
    this.add(jsc);
    this.add(jpa,BorderLayout.SOUTH);
    //属性

    this.setSize(300,200);
    this.setLocation(200, 200);
    this.setIconImage((new ImageIcon("images/qq.jpg")).getImage());
    this.setTitle("腾讯qq");
    //退出程序
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setVisible(true);

    }

    }

    实现效果  如下:

    3.

    package CLASS16.bin.com.JiSuan;

    import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.ComponentOrientation;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.regex.Pattern;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;

    public class MyCalculator extends JFrame{
    /**
    *
    */
    private static final long serialVersionUID = 1L;
    public void menu(){
    addmenu();

    addGridLayout();
    addMenu();
    //addGridLayout();
    }
    public void addMenu(){

    this.setBackground(getBackground());
    setSize(200, 300);//大小
    setLocationRelativeTo(null);//居中
    setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭
    setResizable(false);//不允许修改窗口
    setVisible(true);//显示
    }
    //添加计算器
    public void addmenu(){
    JMenuBar jmen=new JMenuBar();
    super.setJMenuBar(jmen);
    // 设置计算器的背景颜色
    setBackground(Color.lightGray);
    JMenu jmenu=new JMenu("计算器");
    jmen.add(jmenu);

    }
    public void addGridLayout(){

    /**
    * 按钮
    */

    //单行文本
    JTextArea jtext=new JTextArea("0.0");
    this.add(jtext, BorderLayout.NORTH);

    /**
    * 网格布局管理器
    */
    GridLayout grid=new GridLayout(4,4,5,5);


    // 创建面板对象
    JPanel pnl = new JPanel();
    grid.addLayoutComponent(getName(), pnl);
    pnl.createToolTip();
    JButton one1=new JButton("7");
    JButton one2=new JButton("8");
    JButton one3=new JButton("9");
    JButton one4=new JButton("/");
    JButton two1=new JButton("4");
    JButton two2=new JButton("5");
    JButton two3=new JButton("6");
    JButton two4=new JButton("*");
    JButton three1=new JButton("1");
    JButton three2=new JButton("2");
    JButton three3=new JButton("3");
    JButton three4=new JButton("-");
    JButton four1=new JButton("0");
    JButton four2=new JButton(".");
    JButton four3=new JButton("=");
    JButton four4=new JButton("+");
    one4.setForeground(Color.red);
    two4.setForeground(Color.red);
    three4.setForeground(Color.red);
    four4.setForeground(Color.red);
    pnl.add(one1,BorderLayout.NORTH);
    pnl.add(one2,BorderLayout.NORTH);
    pnl.add(one3,BorderLayout.NORTH);
    pnl.add(one4,BorderLayout.NORTH);
    pnl.add(two1,BorderLayout.SOUTH);
    pnl.add(two2,BorderLayout.SOUTH);
    pnl.add(two3,BorderLayout.SOUTH);
    pnl.add(two4,BorderLayout.SOUTH);
    pnl.add(three1);
    pnl.add(three2);
    pnl.add(three3);
    pnl.add(three4);
    pnl.add(four1);
    pnl.add(four2);
    pnl.add(four3);
    pnl.add(four4);
    add(pnl);
    ActionListener listener=new CListener();

    one4.addActionListener(listener);
    two4.addActionListener(listener);
    three4.addActionListener(listener);
    four4.addActionListener(listener);
    }
    /**
    * 监视器
    * @param value
    */
    public void waynemu(String value){
    //正则表达式,输入字符串要求以 零个或一个 - 开头,其余都是数字
    Pattern pattern = Pattern.compile("^-?\d+$");

    if (value == null || value.length() <= 0) {
    value = "0";
    }

    }
    class CListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
    System.out.println("Is ok!");


    }

    }

    }

    package CLASS16.bin.com.JiSuan;


    public class test {

    /**
    * @param args
    */
    public static void main(String[] args) {
    MyCalculator my=new MyCalculator();
    my.menu();
    }

    }

    实现效果  如下:

    <没添加监视器,希望博友可以思考思考>

  • 相关阅读:
    SoapUI 使用笔记
    git 使用笔记(二)
    git 使用笔记(一)
    jquery 拓展
    hdu 1024 Max Sum Plus Plus (DP)
    hdu 2602 Bone Collector (01背包)
    hdu 1688 Sightseeing (最短路径)
    hdu 3191 How Many Paths Are There (次短路径数)
    hdu 2722 Here We Go(relians) Again (最短路径)
    hdu 1596 find the safest road (最短路径)
  • 原文地址:https://www.cnblogs.com/buglove/p/4458510.html
Copyright © 2011-2022 走看看