Java基础-25(02)图形用户界面编程GUI

G:一级菜单
package cn.itcast_08;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/*
 * 一级菜单
 */
public class FrameDemo {
 public static void main(String[] args) {
 // 创建窗体对象并设置属性
 Frame f = new Frame("一级菜单");
 f.setBounds(400, 200, 400, 300);
 f.setLayout(new FlowLayout());
 // 创建菜单栏
 MenuBar mb = new MenuBar();
 // 创建菜单
 Menu m = new Menu("文件");
 // 创建菜单项
 MenuItem mi = new MenuItem("退出系统");
 // 谁添加谁呢
 m.add(mi);
 mb.add(m);
 // 设置菜单栏
 f.setMenuBar(mb);
 // 设置窗体关闭
 f.addWindowListener(new WindowAdapter() {
 @Override
 public void windowClosing(WindowEvent e) {
 System.exit(0);
 }
 });
 mi.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 System.exit(0);
 }
 });
 // 设置窗体可见
 f.setVisible(true);
 }
}

H:多级菜单

package cn.itcast_09;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
/*
 * 多级菜单
 */
public class FrameDemo {
 public static void main(String[] args) {
 // 创建窗体对象并设置属性
 final Frame f = new Frame("多级菜单");
 f.setBounds(400, 200, 400, 300);
 f.setLayout(new FlowLayout());
 final String name = f.getTitle();
 // 创建菜单栏
 MenuBar mb = new MenuBar();
 // 创建菜单
 Menu m1 = new Menu("文件");
 Menu m2 = new Menu("更改名称");
 // 创建菜单项
 final MenuItem mi1 = new MenuItem("好好学习");
 final MenuItem mi2 = new MenuItem("天天向上");
 MenuItem mi3 = new MenuItem("恢复标题");
 MenuItem mi4 = new MenuItem("打开记事本");
 MenuItem mi5 = new MenuItem("退出系统");
 // 谁添加谁呢
 m2.add(mi1);
 m2.add(mi2);
 m2.add(mi3);
 m1.add(m2);
 m1.add(mi4);
 m1.add(mi5);
 mb.add(m1);
 // 设置菜单栏
 f.setMenuBar(mb);
 // 设置窗体关闭
 f.addWindowListener(new WindowAdapter() {
 @Override
 public void windowClosing(WindowEvent e) {
 System.exit(0);
 }
 });
 mi1.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 f.setTitle(mi1.getLabel());
 }
 });
 mi2.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 f.setTitle(mi2.getLabel());
 }
 });
 mi3.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 f.setTitle(name);
 }
 });
 mi4.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 Runtime r = Runtime.getRuntime();
 try {
 r.exec("notepad");
 } catch (IOException e1) {
 e1.printStackTrace();
 }
 }
 });
 mi5.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 System.exit(0);
 }
 });
 // 设置窗体可见
 f.setVisible(true);
 }
}

(7)Netbeans的概述和使用

A:是可以做Java开发的另一个IDE工具。

B:使用

A:四则运算

a:修改图标

b:设置居中

package cn.itcast.util;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
/**
 * 专门做界面效果的类
 *
 * @author Administrator
 */
public class UiUtil {
    private UiUtil() {
    }
    //修改窗体的图标
    public static void setFrameImage(JFrame jf) {
        //获取工具类对象
        //public static Toolkit getDefaultToolkit():获取默认工具包。 
        Toolkit tk = Toolkit.getDefaultToolkit();
        //根据路径获取图片
        Image i = tk.getImage("src\\cn\\itcast\\resource\\jjcc.jpg");
        //给窗体设置图片
        jf.setIconImage(i);
    }
    //设置窗体居中
    public static void setFrameCenter(JFrame jf) {
        /*
         思路:
         A:获取屏幕的宽和高
         B:获取窗体的宽和高
         C:(用屏幕的宽-窗体的宽)/2,(用屏幕的高-窗体的高)/2作为窗体的新坐标。
         */
        //获取工具对象
        Toolkit tk = Toolkit.getDefaultToolkit();
        //获取屏幕的宽和高
        Dimension d = tk.getScreenSize();
        double srceenWidth = d.getWidth();
        double srceenHeigth = d.getHeight();
        //获取窗体的宽和高
        int frameWidth = jf.getWidth();
        int frameHeight = jf.getHeight();
        //获取新的宽和高
        int width = (int) (srceenWidth - frameWidth) / 2;
        int height = (int) (srceenHeigth - frameHeight) / 2;
        //设置窗体坐标
        jf.setLocation(width, height);
    }
}

c:设置皮肤

package cn.itcast.util;
//这里面定义了常见的要使用的皮肤的字符串路径。
public abstract class MyLookAndFeel {
 // 系统自带皮肤,5种都能用
 public static String SYS_METAL = "javax.swing.plaf.metal.MetalLookAndFeel";
 public static String SYS_NIMBUS = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
 // 有个性
 public static String SYS_CDE_MOTIF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
 public static String SYS_WINDOWS = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
 public static String SYS_WINDOWS_CLASSIC = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
 // JIattoo jar包资源
 public static String JTATTOO_ACRYL = "com.jtattoo.plaf.acryl.AcrylLookAndFeel";
 public static String JTATTOO_AERO = "com.jtattoo.plaf.aero.AeroLookAndFeel";
 // 还可以
 public static String JTATTOO_ALUMINUM = "com.jtattoo.plaf.aluminium.AluminiumLookAndFeel";
 // 很喜欢
 public static String JTATTOO_BERNSTEIN = "com.jtattoo.plaf.bernstein.BernsteinLookAndFeel";
 public static String JTATTOO_FAST = "com.jtattoo.plaf.fast.FastLookAndFeel";
 // 有个性
 public static String JTATTOO_HIFI = "com.jtattoo.plaf.hifi.HiFiLookAndFeel";
 public static String JTATTOO_LUNA = "com.jtattoo.plaf.luna.LunaLookAndFeel";
 // 很喜欢
 public static String JTATTOO_MCWIN = "com.jtattoo.plaf.mcwin.McWinLookAndFeel";
 public static String JTATTOO_MINT = "com.jtattoo.plaf.mint.MintLookAndFeel";
 // 有个性
 public static String JTATTOO_NOIRE = "com.jtattoo.plaf.noire.NoireLookAndFeel";
 public static String JTATTOO_SMART = "com.jtattoo.plaf.smart.SmartLookAndFeel";
 // liquidlnf.jar包资源
 // 很喜欢
 public static String LIQUIDINF = "com.birosoft.liquid.LiquidLookAndFeel";
}

d:数据校验

package cn.itcast.view;(7)
import cn.itcast.util.UiUtil;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class NewJFrame extends javax.swing.JFrame {
    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
        init();
    }
     public NewJFrame(String name) {
        initComponents();
        init(name);
    }
    private void init() {
        this.setTitle("模拟四则运算");
        UiUtil.setFrameImage(this,"jjcc.jpg");
        UiUtil.setFrameCenter(this);
    }
     private void init(String name) {
        this.setTitle("欢迎"+name+"光临");
        UiUtil.setFrameImage(this,"jjcc.jpg");
        UiUtil.setFrameCenter(this);
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        firstNumber = new javax.swing.JTextField();
        secondNumber = new javax.swing.JTextField();
        resultNumber = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        selectOperator = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("第一个操作数");
        jLabel2.setText("第二个操作数");
        jLabel3.setText("结果");
        jLabel4.setText("=");
        selectOperator.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "+", "-", "*", "/" }));
        jButton1.setText("计算");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(firstNumber)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(selectOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(secondNumber))
                .addGap(14, 14, 14)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jLabel3)
                    .addComponent(resultNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(secondNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(resultNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(selectOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //获取第一个操作数
        String firstNumberString = this.firstNumber.getText().trim();
        //获取运算符
        String selectOperator = this.selectOperator.getSelectedItem().toString();
        //获取第二个操作数
        String secondNumberString = this.secondNumber.getText().trim();
//        System.out.println(firstNumberString);
//        System.out.println(selectOperator);
//        System.out.println(secondNumberString);
        //数据校验,必须是数字字符串
        String regex = "\\d+";
        //校验第一个数
        if (!firstNumberString.matches(regex)) {
//            System.out.println("数据不匹配");
            //public static void showMessageDialog(Component parentComponent,Object message)
            JOptionPane.showMessageDialog(this, "第一个操作数不满足要求必须是数字");
            this.firstNumber.setText("");
            this.firstNumber.requestFocus();
            return;//回去吧
        }
 //校验第二个数
        if (!secondNumberString.matches(regex)) {
//            System.out.println("数据不匹配");
            //public static void showMessageDialog(Component parentComponent,Object message)
            JOptionPane.showMessageDialog(this, "第二个操作数不满足要求必须是数字");
             this.secondNumber.setText("");
            this.secondNumber.requestFocus();
            return;//回去吧
        }
        //把字符串数据转换为整数
        int firstNumber = Integer.parseInt(firstNumberString);
        int secondNumber = Integer.parseInt(secondNumberString);
        //定义变量接收结果
        int resultNumber = 0;
        switch (selectOperator) {
            case "+":
                resultNumber = firstNumber + secondNumber;
                break;
            case "-":
                resultNumber = firstNumber - secondNumber;
                break;
            case "*":
                resultNumber = firstNumber * secondNumber;
                break;
            case "/":
                if(secondNumber==0){
                     JOptionPane.showMessageDialog(this, "除数不能为0");
                     this.secondNumber.setText("");
                     this.secondNumber.requestFocus();
                     return;
                }else {
                    resultNumber = firstNumber / secondNumber;
                }
                break;
        }
        //把结果赋值给结果框
        this.resultNumber.setText(String.valueOf(resultNumber));
    }//GEN-LAST:event_jButton1ActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new NewJFrame().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField firstNumber;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField resultNumber;
    private javax.swing.JTextField secondNumber;
    private javax.swing.JComboBox selectOperator;
    // End of variables declaration//GEN-END:variables
}

原文发布于微信公众号 - Java帮帮(javahelp)

原文发表时间:2016-12-16

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏ml

Java之线程———GUI线程(包含打字游戏和计时器俩个GUI实列)

     当java程序包含图形用户界面(GUI)时,Java虚拟机在运行应用程序时会自动启动更多的线程,其中有两个重要的线程:AWT-EventQuecue ...

61160
来自专栏王磊的博客

javascript数字格式化通用类——accounting.js使用

简介 accounting.js 是一个非常小的JavaScript方法库用于对数字,金额和货币进行格式化。并提供可选的Excel风格列渲染。它没有依赖任何JS...

73160
来自专栏码匠的流水账

聊聊pg jdbc的queryTimeout及next方法

本文主要介绍一下pg jdbc statement的queryTimeout及resultSet的next方法

40310
来自专栏跟着阿笨一起玩NET

UML图示与代码对照

本文转载:http://www.cnblogs.com/iamlilinfeng/archive/2012/08/29/2662740.html

8900
来自专栏菩提树下的杨过

AS3中的序列化与反序列化

AS3中的序列化功能其实比较弱,Adobe官方的第三方json序列化工具类可以把对象的属性序列化为字符串,并反序列化为对象。但如果对象中定义了自己的functi...

25490
来自专栏ml

java版本的学生管理系统

1 import java.awt.BorderLayout; 2 import java.awt.Color; 3 import java.awt...

79160
来自专栏非典型技术宅

iOS实践:一步步实现星级评分1. 创建星星2. 优化3. 灵异事件

19540
来自专栏ml

Java初学之华容道游戏

1 package hhuarongdao; 2 3 public class example { 4 public static void main(St...

67670
来自专栏码匠的流水账

聊聊resilience4j的bulkhead

resilience4j-bulkhead-0.13.0-sources.jar!/io/github/resilience4j/bulkhead/Bulkhe...

22210
来自专栏刘晓杰

嵌套滑动机制探究

37670

扫码关注云+社区

领取腾讯云代金券