java 实验6 图形用户界面设计试验(2)

共两大题 窗体实现(略去测试类即 实例化自定义窗体)

小结:

1. JRadioButton 单选按钮
  ButtonGroup 按钮集合(加入单选按钮)
  清空选项需让ButtonGroup调用clearSelection()函数
  bg.clearSelection();

2. JCheckBox 多选
  清空选项需实例调用 jcb1.setSelected(false);

3. JComboBox 下拉列表
 利用additem(str)增加选项

4. 实现树切换后的画布切换
 JTabbedPane(选项卡) 清空所有组建removeAll();再增加组建

1、完成如下登陆界面, 其中用户名为abc。密码为1234,登陆成功登陆后弹出消息框,“欢迎您,abc 登陆”;如果密码输入错误,弹出对话框,显示“用户名和密码错误”,并清空输入的信息。

Login窗体类

package login;

import javax.swing.JFrame;

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

import javax.swing.*;

public class Login extends JFrame implements ActionListener{
    private static String USERNAME="abc";
    private static String PASSWORD="1234";

    JButton jb1,jb2;
    JTextField text1,text2;
    JDialog jd;
    Login() {
        setTitle("the login window");
        JLabel label1=new JLabel("username                ");
        JLabel label2=new JLabel("password                ");
        text1=new JTextField(10);
        text2=new JTextField(10);
        jb1=new JButton("confirm");
        jb2=new JButton("cancel");
        jb1.addActionListener(this);

        setLayout(new GridLayout(3,1));
        JPanel jp1=new JPanel();
        JPanel jp2=new JPanel();
        JPanel jp3=new JPanel();
        jp1.add(label1);
        jp1.add(text1);
        jp2.add(label2);
        jp2.add(text2);
        jp3.add(jb1);
        jp3.add(jb2);
        add(jp1);
        add(jp2);
        add(jp3);
        setBounds(100,100,300,150);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==jb1);
            if(text1.getText().equals(USERNAME)&&text2.getText().equals(PASSWORD)) {
                JOptionPane.showMessageDialog(this, "输入正确");
                new Info();
                setVisible(false);
            }
            else{
                JOptionPane.showMessageDialog(this, "用户名和密码错误");
                text1.setText(null);
                text2.setText(null);
            }
    }
}

结果

2、接第一题,如果登录成功,显示下面的界面:

(1)输入username,sex,department,hobby,telephoneNo, resume,点击“confirm”按钮,左边的信息显示在右边的区域,如下图所示:

(2)点击“Cancel”,弹出对话框请用户确认,如果用户选择“确定”,就清空左边和右边的信息。

(3)点击“Save”,在控制台显示:你的学号+你的姓名+“信息已保存”。

(4) 菜单“File”下显示 “New” 和 “Open”,指定“New”的快捷方式是Ctrl+N,点击“New”,将下面各输入部分清空(Sex项与Department项置为默认);点击“Open”,在控制台显示:你的学号+你的姓名+“点击了Open菜单项”。

(5)菜单“Edit”下显示“Cut”和“Copy”,点击“Cut”,在控制台显示:你的学号+你的姓名+“点击了Cut菜单项”; 点击“Copy”,在控制台显示:你的学号+你的姓名+“点击了Copy菜单项”。

info消息窗体类(大窗体实现)

package login;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class Info extends JFrame{

    Info(){
        //一些基本组建
        JLabel label1 = new JLabel("Please input your information",Label.LEFT);
        JLabel label2 = new JLabel("username");
        JLabel label3 = new JLabel("sex");
        JLabel label4 = new JLabel("department");
        JLabel label5 = new JLabel("hobby");
        JLabel label6 = new JLabel("telephoneNo");
        JLabel label7 = new JLabel("resume");
        JLabel label8 = new JLabel("your information");
        JTextField text1=new JTextField(20);
        JTextField text2=new JTextField(20);
        JTextArea text3=new JTextArea(20,20);
        JTextArea text4=new JTextArea(100,100);

        JPanel jp1=new JPanel();
        JPanel jp2=new JPanel();
        JPanel jp3=new JPanel();
        JPanel jp4=new JPanel();
        JPanel jp5=new JPanel();
        JPanel jp6=new JPanel();
        JPanel jp7=new JPanel();

        JPanel left=new JPanel();
        JPanel right=new JPanel();

        JButton jb1=new JButton("Confirm");
        JButton jb2=new JButton("Cancel");
        JButton jb3=new JButton("Save");

        //department下拉列表
        JComboBox<String> box=new JComboBox<>();
        box.addItem("Computer");
        box.addItem("Math");
        box.addItem("Energy");
        box.addItem("Law");

        //sex单选
        JRadioButton sex1= new JRadioButton("male");
        JRadioButton sex2= new JRadioButton("female");
        ButtonGroup bg=new ButtonGroup();
        bg.add(sex1);
        bg.add(sex2);

        //hobby多选
        JCheckBox jcb1=new JCheckBox("music");
        JCheckBox jcb2=new JCheckBox("reading");
        JCheckBox jcb3=new JCheckBox("cooking");

        //设置菜单
        JMenuBar menubar=new JMenuBar();
        JMenu menu1=new JMenu("File");
        JMenu menu2=new JMenu("Edit");
        JMenuItem item1=new JMenuItem("New");
        JMenuItem item2=new JMenuItem("Open");
        JMenuItem item3=new JMenuItem("Cut");
        JMenuItem item4=new JMenuItem("Copy");
        item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
        menu1.add(item1);
        menu1.addSeparator();
        menu1.add(item2);
        menu2.add(item3);
        menu2.addSeparator();
        menu2.add(item4);
        menubar.add(menu1);
        menubar.add(menu2);
        setJMenuBar(menubar);
        item1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                    text1.setText(null);
                    text2.setText(null);
                    text3.setText(null);
                    text4.setText(null);
                    //想重置JRadioButton必让ButtonGroup调用clearSelection()函数
                    bg.clearSelection();
                    jcb1.setSelected(false);
                    jcb2.setSelected(false);
                    jcb3.setSelected(false);
                }
        });
        item2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("20171335+cf+“点击了Open菜单项”");
            }
        });
        item3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("20171335+cf+“点击了Cut菜单项”");
            }
        });
        item4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("20171335+cf+“点击了Copy菜单项”");
            }
        });

        //设置左半组件
        jp1.add(sex1);
        jp1.add(sex2);
        jp2.add(jcb1);
        jp2.add(jcb2);
        jp2.add(jcb3);

        Box basebox,box1,box2;
        box1=Box.createVerticalBox();
        box2=Box.createVerticalBox();
        basebox=Box.createHorizontalBox();

        box1.add(label2);
        box1.add(Box.createVerticalStrut(22));
        box1.add(label3);
        box1.add(Box.createVerticalStrut(22));
        box1.add(label4);
        box1.add(Box.createVerticalStrut(22));
        box1.add(label5);
        box1.add(Box.createVerticalStrut(22));
        box1.add(label6);
        box1.add(Box.createVerticalStrut(180));
        box1.add(label7);

        box2.add(text1);
        box2.add(Box.createVerticalStrut(10));
        box2.add(jp1);
        box2.add(Box.createVerticalStrut(10));
        box2.add(box);
        box2.add(Box.createVerticalStrut(10));
        box2.add(jp2);
        box2.add(Box.createVerticalStrut(10));
        box2.add(text2);
        box2.add(Box.createVerticalStrut(10));
        box2.add(new JScrollPane(text3));

        basebox.add(box1);
        basebox.add(Box.createHorizontalStrut(20));
        basebox.add(box2);

        jp3.add(jb1);
        jp3.add(jb2);
        jp5.add(jb1);
        jp5.add(jb2);

        left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
        left.add(label1);
        left.add(basebox);
        left.add(jp5);

        //设置右半组件
        jp6.add(jb3);

        right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
        right.add(label8);
        right.add(text4);
        right.add(jp6);

        //设置confirm键
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = text1.getText();
                String sex ="";
                if(sex1.isSelected())
                    sex=sex1.getText();
                else
                    sex=sex2.getText();
                String department = box.getSelectedItem().toString();
                String hobby="";
                for(Component c:jp2.getComponents()) {
                    if(c instanceof JCheckBox) {
                        if(((JCheckBox)c).isSelected()) {
                            hobby+=((JCheckBox)c).getText();
                            hobby+=",";
                        }
                    }
                }
                String telephonNo=text2.getText();
                String resume=text3.getText();
                String result="my name:       "+username+"\nsex:                "+sex+"\ndepartment:    "
                +department+"\nhobby:            "+hobby+"\ntelephonNo:    "+telephonNo+"\nresume:          "+resume;
                text4.setText(result);
            }
        });

        //设置Cancel键
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int clean = JOptionPane.showConfirmDialog(null, "Do you confirm to delete it?","Warning",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
                if(clean==JOptionPane.YES_OPTION) {
                    text1.setText(null);
                    text2.setText(null);
                    text3.setText(null);
                    text4.setText(null);
                    //想重置JRadioButton必让ButtonGroup调用clearSelection()函数
                    bg.clearSelection();
                    jcb1.setSelected(false);
                    jcb2.setSelected(false);
                    jcb3.setSelected(false);
                }
            }
        });
        //设置Save键
        jb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Saveinfo.save(text4.getText());
                System.out.println("保存成功");
            }
        });

        //整体画布
        JSplitPane all=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,right);
        add(all,BorderLayout.CENTER);

        //设置整体
        setTitle("info");
        setBounds(100,100,800,500);
        setVisible(true);
        validate();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}

储存信息类(Save键用)

package login;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Path;

public class Saveinfo {
    public static void save(String str){
        File directory = new File(".");
        String path = null;
        try {
            path = directory.getCanonicalPath();//获取当前路径
            path +="\\info.txt";
            System.out.println(path);
            BufferedWriter  b=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
            b.write(str);
            b.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

结果

3、参照如下界面,利用JTree, JSplitPane, JTable, JTabbedPane完成。

(1) 选中“沪宁高速A段维修工程”,三个页面如下:

(2) 选中“沪宁高速B段维修工程”,出现相应的页面,仅以“专家设置”举例如下:

(3) 选中任意一个表格,选中一行,在控制台输出选中行的内容,如下图选中了“沪宁高速A段维修工程”中“竞标单位设置”中的一行:

在控制台输出了相应内容:

窗体类

package 工程项目招标设置;

import java.awt.BorderLayout;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

public class Project extends JFrame {

    Project(){

        //A“指标设置”表格
        JPanel jp1=new JPanel();
        jp1.setLayout(new BorderLayout());
        Object[] Names1 = {"指标编号", "指标名称", "是否入选", "指标分值"};
        Object[][] a= {
                {10,"施工性质","入选",20},
                {11,"以往施工项目","入选",20},
                {12,"注册资金","入选",30},
                {13,"企业总资产","不选",20},
                {14,"公司管理水平","不选",10},
        };
        JTable tabel1=new JTable(a,Names1);
        tabel1.setShowGrid(true);
        tabel1.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e){
            for (int i = 0; i < tabel1.getColumnCount(); i++)
                System.out.print(tabel1.getModel().getValueAt(tabel1.getSelectedRow(), i)+" ");
            System.out.println();
        }});
        jp1.add(new JScrollPane(tabel1),BorderLayout.NORTH);

        //A"竞标单位设置"表格
        JPanel jp2=new JPanel();
        jp2.setLayout(new BorderLayout());
        Object[] Names2 = {"公司编号", "公司名称", "法人代表", "成立时间","地址"};
        Object[][] b= {
                {001,"华西沪东建筑公司","张三","2000-2-1","上海市浦东新区"},
                {002,"瑞丰建筑公司","李四","1998-3-1","上海市浦东新区"}
        };
        JTable tabel2=new JTable(b,Names2);
        tabel2.setShowGrid(true);
        jp2.add(new JScrollPane(tabel2),BorderLayout.NORTH);
        tabel2.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e){
            for (int i = 0; i < tabel2.getColumnCount(); i++)
                System.out.print(tabel2.getModel().getValueAt(tabel2.getSelectedRow(), i)+" ");
            System.out.println();
        }});

        //A“专家设置”表格
        JPanel jp3=new JPanel();
        jp3.setLayout(new BorderLayout());
        Object[] Names3 = {"专家编号", "专家名称", "性别", "学历","单位"};
        Object[][] c= {
                {001,"张小三","男","博士","上海建筑研究所"},
                {002,"刘芳芳","女","博士","北京建筑研究所"}
        };
        JTable tabel3=new JTable(c,Names3);
        tabel3.setShowGrid(true);
        jp3.add(new JScrollPane(tabel3),BorderLayout.NORTH);
        tabel3.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e){
            for (int i = 0; i < tabel3.getColumnCount(); i++)
                System.out.print(tabel3.getModel().getValueAt(tabel3.getSelectedRow(), i)+" ");
            System.out.println();
        }});

        //B“指标设置”表格
        JPanel jp4=new JPanel();
        jp4.setLayout(new BorderLayout());
        Object[] Names4 = {"指标编号", "指标名称", "是否入选", "指标分值"};
        Object[][] d= {
                {1,"施工性质","不选",30},
                {2,"以往施工项目","入选",40},
                {3,"注册资金","入选",60},
                {4,"企业总资产","不选",10},
                {5,"公司管理水平","不选",20},
        };
        JTable tabel4=new JTable(d,Names4);
        tabel4.setShowGrid(true);
        jp4.add(new JScrollPane(tabel4),BorderLayout.NORTH);
        tabel4.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e){
            for (int i = 0; i < tabel4.getColumnCount(); i++)
                System.out.print(tabel4.getModel().getValueAt(tabel4.getSelectedRow(), i)+" ");
            System.out.println();
        }});

        //B"竞标单位设置"表格
        JPanel jp5=new JPanel();
        jp5.setLayout(new BorderLayout());
        Object[] Names5 = {"公司编号", "公司名称", "法人代表", "成立时间","地址"};
        Object[][] e= {
                {001,"华东建筑公司","张三","2001-2-1","上海市浦东新区"},
                {002,"瑞丰建筑公司","李四","2000-3-1","上海市浦东新区"}
        };
        JTable tabel5=new JTable(e,Names5);
        tabel5.setShowGrid(true);
        jp5.add(new JScrollPane(tabel5),BorderLayout.NORTH);
        tabel5.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e){
            for (int i = 0; i < tabel5.getColumnCount(); i++)
                System.out.print(tabel5.getModel().getValueAt(tabel5.getSelectedRow(), i)+" ");
            System.out.println();
        }});

        //B“专家设置”表格
        JPanel jp6=new JPanel();
        jp6.setLayout(new BorderLayout());
        Object[] Names6 = {"专家编号", "专家名称", "性别", "学历","单位"};
        Object[][] f= {
                {001,"任芳","男","硕士","华北建筑研究所"},
                {002,"刘齐齐","女","博士","华东建筑研究所"}
        };
        JTable tabel6=new JTable(f,Names6);
        tabel6.setShowGrid(true);
        jp6.add(new JScrollPane(tabel6),BorderLayout.NORTH);
        tabel6.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e){
            for (int i = 0; i < tabel6.getColumnCount(); i++)
                System.out.print(tabel6.getModel().getValueAt(tabel6.getSelectedRow(), i)+" ");
            System.out.println();
        }});

        //选项卡-收录表格
        JTabbedPane p=new JTabbedPane(JTabbedPane.NORTH);

        //“招标项目名”树
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("招标项目名");
        DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("沪宁高速A段维修工程");
        DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("沪宁高速B段维修工程");
        root.add(node1);
        root.add(node2);
        JTree tree=new JTree(root);
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
                if(node.equals(node1)) {
                    p.removeAll();
                    p.add("指标设置",jp1);
                    p.add("竞标单位设置",jp2);
                    p.add("专家设置",jp3);
                }
                if(node.equals(node2)) {
                    p.removeAll();
                    p.add("指标设置",jp4);
                    p.add("竞标单位设置",jp5);
                    p.add("专家设置",jp6);
                }
            }
        });

        setTitle("工程项目招标设置");
        JSplitPane jsp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,tree,p);
        jsp.setDividerLocation(170);
        add(jsp);
        setBounds(100,100,700,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

结果

原文地址:https://www.cnblogs.com/cc123nice/p/10770598.html

时间: 2024-10-27 01:58:09

java 实验6 图形用户界面设计试验(2)的相关文章

【Java】_图形用户界面(GUI)

学习资料来源:斯坦福大学公开课编程方法cs106aJAVA 学习过程中使用的代码.笔记初稿(主要按顺序记录视频教程讲解内容)以及课程讲义已上传至下载中心(有兴趣者可自行下载学习), 学习过程中的大部分程序需要导入acm.jar包(已上传至下载中心,也可自行进入http://jtf.acm.org/ 进行下载), GObject: super()--调用父类构造函数 acm.graphics--添加进画布中的图形是有堆叠顺序的 GCanvas--拼贴画的背景画布 画布(GCanvas)和图形程序(

黑马程序员------Java中GUI(图形用户界面)学习总结

Java培训.Android培训.iOS培训..Net培训</a>.期待与您交流! GUI: Graphical User Interface(图形用户接口). 即用图形的方式,来显示计算机操作的界面,以方便用户更容易更直观地操作. Java中为GUI提供的对象都在Java.Awt和Javax.Swing两个包中. java.Awt: Abstract Window ToolKit (抽象窗口工具包). 需要调用本地系统方法实现功能,属于重量级控件. javax.Swing: 在AWT的基础上

java GUI(图形用户界面)练习

6.1.3 一个简单的GUI程序 package six; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main { private static int numclicks = 0; //记录次数 private static JFrame frame = new JFrame("简单GUI"); private static JLabel label = new JLa

Java图形用户界面编程

GUI代表图形用户界面(Graphical User Interface),Java API中提供了两套组件用户支持编写图形用户界面,包括AWT和Swing. AWT使用本地操作系统的代码资源,被称为重量级组件. Swing建立在AWT提供的基础之上,同时使用AWT相同的事件处理机制.Swing组件是轻量级的GUI组件,完全有纯Java代码编写,不依赖于任何特定平台,采用MVC架构方式来设计组件类.它将表示层分为三个分离的部分,分别是模型(model).视图(view).控制器(controle

java图形用户界面程序

GUI(Graphical User Interface) 定义:一个程序中,用户可以可以看见和与之交流的部分. 支持图形用户界面的两套组件: 1,AWT 2,Swing 建立步骤: 建立容器-建立组件-将组件添加到容器-设置布局 Swing中的重量级容器: 继承自AWT中的Container类,为其他容器和组件提供绘制位置 有JFrame,JDialog,JWindow,JApplet; Swing中的轻量级容器: 继承自Swing类的JComponent,为了方便其他组件的定位 有JPane

java图形用户界面认知

为了使我们生成的图形用户界面具有良好的平台无关性,Java语言中,提供了布局管理器这个工具来管理 组件在容器中的布局,而不使用直接设置组件位置.组件类是创建用户界面的,例如,JButton,JLabel和JTextField.容器类是用来包含其他组件的,例如,JFrame,JPanel和JApplet.辅助类是用来支持GUI组件的,例如,Graphics,Color,Font,FontMetrics和Dimension. 一.组件类 Component 类的实例可以显示在屏幕上.Componen

java计算器 图形用户界面 升级版 v1.02

jsp页面: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.m

黑马程序员——【Java基础】——GUI(图形用户界面)

一.概述 1.GUI(GraphicalUser Interface):又称图形用户界面,是计算机用户与计算机交互的一种方式. 2.用户与计算机交互的方式有两种:GUI和CLI. (1)GUI(Graphical User Interface):图形用户接口,用图形方式,来显示计算机操作界面,方便直观. (2)CLI(Command LineUser Interface):命令行用户接口,即常见的Dos命令行操作,须记住一些命令,操作不直观. 3.Java提供的GUI对象都存在于java.Awt

java文本编辑器v2.0 图形用户界面

package 文本编辑器; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListe