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

Posted cc123nice

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 实验6 图形用户界面设计试验相关的知识,希望对你有一定的参考价值。

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

小结:

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);
    }
}

 

 结果

技术图片技术图片

 

以上是关于java 实验6 图形用户界面设计试验的主要内容,如果未能解决你的问题,请参考以下文章

Java语言程序设计 上机实验6 掌握Java的图形用户界面编程,掌握布局管理器和事件的响应方法

实验十:图形用户界面设计

Java图形用户界面设计实验报告,成长路线图

第四次试验报

计算机系统实验三 汇编语言试验

java 图形界面设计制作计算器