zoukankan      html  css  js  c++  java
  • 罗松-东文财-赵栋201771010112罗松《面向对象程序设计(java)》第十四周学习总结

     理论知识知:

    1.SwingMVC设计模式
    2.布局管理器
    3.文本输入
    4.选择组件
    5.菜单
    6.对话框

    1、实验目的与要求

    (1) 掌握GUI布局管理器用法;

    (2) 掌握各类Java Swing组件用途及常用API

    2、实验内容和步骤

    实验1 导入第12章示例程序,测试程序并进行组内讨论。

    测试程序1

    elipse IDE中运行教材479页程序12-1,结合运行结果理解程序;

    掌握各种布局管理器的用法;

    理解GUI界面中事件处理技术的用途。

    在布局管理应用代码处添加注释;

    测试程序2

    elipse IDE中调试运行教材486页程序12-2,结合运行结果理解程序;

    掌握各种文本组件的用法;

    记录示例代码阅读理解中存在的问题与疑惑。

    测试程序3

    elipse IDE中调试运行教材489页程序12-3,结合运行结果理解程序;

    掌握复选框组件的用法;

    记录示例代码阅读理解中存在的问题与疑惑。

    普通字体: 

    普通字体加粗:

    斜体字:

    斜体字加粗:

    测试程序4

    elipse IDE中调试运行教材491页程序12-4,运行结果理解程序;

    掌握单选按钮组件的用法;

    记录示例代码阅读理解中存在的问题与疑惑。

    samll

     

    medium:

    large:

    extra large:

    单选钮,只能选中一个

    测试程序5

    elipse IDE中调试运行教材494页程序12-5,结合运行结果理解程序;

    掌握边框的用法;

    记录示例代码阅读理解中存在的问题与疑惑。

    测试程序6

    elipse IDE中调试运行教材498页程序12-6,结合运行结果理解程序;

    掌握组合框组件的用法;

    记录示例代码阅读理解中存在的问题与疑惑。

     

    测试程序7

    elipse IDE中调试运行教材501页程序12-7,结合运行结果理解程序;

    掌握滑动条组件的用法;

    记录示例代码阅读理解中存在的问题与疑惑。

    测试程序8

    elipse IDE中调试运行教材512页程序12-8,结合运行结果理解程序;

    掌握菜单的创建、菜单事件监听器、复选框和单选按钮菜单项、弹出菜单以及快捷键和加速器的用法。

    记录示例代码阅读理解中存在的问题与疑惑。

     

    测试程序9

    elipse IDE中调试运行教材517页程序12-9,结合运行结果理解程序;

    掌握工具栏和工具提示的用法;

    记录示例代码阅读理解中存在的问题与疑惑。

    初始:

    将鼠标放置在四个按钮上都会有对应的提示(如图):

    测试程序10

    elipse IDE中调试运行教材524页程序12-1012-11,结合运行结果理解程序,了解GridbagLayout的用法。

    elipse IDE中调试运行教材533页程序12-12,结合程序运行结果理解程序,了解GroupLayout的用法。

    记录示例代码阅读理解中存在的问题与疑惑。

    测试程序11

    elipse IDE中调试运行教材539页程序12-1312-14,结合运行结果理解程序;

    掌握定制布局管理器的用法。

    记录示例代码阅读理解中存在的问题与疑惑。

    测试程序12

    elipse IDE中调试运行教材544页程序12-1512-16,结合运行结果理解程序;

    掌握选项对话框的用法。

    记录示例代码阅读理解中存在的问题与疑惑。

    测试程序13

    elipse IDE中调试运行教材552页程序12-1712-18,结合运行结果理解程序;

    掌握对话框的创建方法;

    记录示例代码阅读理解中存在的问题与疑惑。

    测试程序14

    elipse IDE中调试运行教材556页程序12-1912-20,结合运行结果理解程序;

    掌握对话框的数据交换用法;

    记录示例代码阅读理解中存在的问题与疑惑。

    代码:

    package dataExchange;
    
    import java.awt.*;
    import javax.swing.*;
    
    /**
     * @version 1.34 2015-06-12
     * @author Cay Horstmann
     */
    public class DataExchangeTest
    {
       public static void main(String[] args)
       {
          EventQueue.invokeLater(() -> {
             JFrame frame = new DataExchangeFrame();
             frame.setTitle("DataExchangeTest");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true);
          });
       }
    }
    package dataExchange;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     * A frame with a menu whose File->Connect action shows a password dialog.
     */
    public class DataExchangeFrame extends JFrame
    {
       public static final int TEXT_ROWS = 20;
       public static final int TEXT_COLUMNS = 40;
       private PasswordChooser dialog = null;
       private JTextArea textArea;
    
       public DataExchangeFrame()
       {
          // construct a File menu
    
          JMenuBar mbar = new JMenuBar();
          setJMenuBar(mbar);
          JMenu fileMenu = new JMenu("File");
          mbar.add(fileMenu);
    
          // add Connect and Exit menu items
    
          JMenuItem connectItem = new JMenuItem("Connect");
          connectItem.addActionListener(new ConnectAction());
          fileMenu.add(connectItem);
    
          // The Exit item exits the program
    
          JMenuItem exitItem = new JMenuItem("Exit");
          exitItem.addActionListener(event -> System.exit(0));
          fileMenu.add(exitItem);
    
          textArea = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
          add(new JScrollPane(textArea), BorderLayout.CENTER);
          pack();
       }
    
       /**
        * The Connect action pops up the password dialog.
        */
       private class ConnectAction implements ActionListener
       {
          public void actionPerformed(ActionEvent event)
          {
             // if first time, construct dialog
    
             if (dialog == null) dialog = new PasswordChooser();
    
             // set default values
             dialog.setUser(new User("yourname", null));
    
             // pop up dialog
             if (dialog.showDialog(DataExchangeFrame.this, "Connect"))
             {
                // if accepted, retrieve user input
                User u = dialog.getUser();
                textArea.append("user name = " + u.getName() + ", password = "
                      + (new String(u.getPassword())) + "
    ");
             }
          }
       }
    }
    package dataExchange;
    
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Frame;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    /**
     * A password chooser that is shown inside a dialog
     */
    public class PasswordChooser extends JPanel
    {
       private JTextField username;
       private JPasswordField password;
       private JButton okButton;
       private boolean ok;
       private JDialog dialog;
    
       public PasswordChooser()
       {
          setLayout(new BorderLayout());
    
          // construct a panel with user name and password fields
    
          JPanel panel = new JPanel();
          panel.setLayout(new GridLayout(2, 2));
          panel.add(new JLabel("User name:"));
          panel.add(username = new JTextField(""));
          panel.add(new JLabel("Password:"));
          panel.add(password = new JPasswordField(""));
          add(panel, BorderLayout.CENTER);
    
          // create Ok and Cancel buttons that terminate the dialog
    
          okButton = new JButton("Ok");
          okButton.addActionListener(event -> {
             ok = true;
             dialog.setVisible(false);
          });
    
          JButton cancelButton = new JButton("Cancel");
          cancelButton.addActionListener(event -> dialog.setVisible(false));
    
          // add buttons to southern border
    
          JPanel buttonPanel = new JPanel();
          buttonPanel.add(okButton);
          buttonPanel.add(cancelButton);
          add(buttonPanel, BorderLayout.SOUTH);
       }
    
       /**
        * Sets the dialog defaults.
        * @param u the default user information
        */
       public void setUser(User u)
       {
          username.setText(u.getName());
       }
    
       /**
        * Gets the dialog entries.
        * @return a User object whose state represents the dialog entries
        */
       public User getUser()
       {
          return new User(username.getText(), password.getPassword());
       }
    
       /**
        * Show the chooser panel in a dialog
        * @param parent a component in the owner frame or null
        * @param title the dialog window title
        */
       public boolean showDialog(Component parent, String title)
       {
          ok = false;
    
          // locate the owner frame
    
          Frame owner = null;
          if (parent instanceof Frame)
             owner = (Frame) parent;
          else
             owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);
    
          // if first time, or if owner has changed, make new dialog
    
          if (dialog == null || dialog.getOwner() != owner)
          {
             dialog = new JDialog(owner, true);
             dialog.add(this);
             dialog.getRootPane().setDefaultButton(okButton);
             dialog.pack();
          }
    
          // set title and show dialog
    
          dialog.setTitle(title);
          dialog.setVisible(true);
          return ok;
       }
    }
    package dataExchange;
    
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Frame;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    /**
     * A password chooser that is shown inside a dialog
     */
    public class PasswordChooser extends JPanel
    {
       private JTextField username;
       private JPasswordField password;
       private JButton okButton;
       private boolean ok;
       private JDialog dialog;
    
       public PasswordChooser()
       {
          setLayout(new BorderLayout());
    
          // construct a panel with user name and password fields
    
          JPanel panel = new JPanel();
          panel.setLayout(new GridLayout(2, 2));
          panel.add(new JLabel("User name:"));
          panel.add(username = new JTextField(""));
          panel.add(new JLabel("Password:"));
          panel.add(password = new JPasswordField(""));
          add(panel, BorderLayout.CENTER);
    
          // create Ok and Cancel buttons that terminate the dialog
    
          okButton = new JButton("Ok");
          okButton.addActionListener(event -> {
             ok = true;
             dialog.setVisible(false);
          });
    
          JButton cancelButton = new JButton("Cancel");
          cancelButton.addActionListener(event -> dialog.setVisible(false));
    
          // add buttons to southern border
    
          JPanel buttonPanel = new JPanel();
          buttonPanel.add(okButton);
          buttonPanel.add(cancelButton);
          add(buttonPanel, BorderLayout.SOUTH);
       }
    
       /**
        * Sets the dialog defaults.
        * @param u the default user information
        */
       public void setUser(User u)
       {
          username.setText(u.getName());
       }
    
       /**
        * Gets the dialog entries.
        * @return a User object whose state represents the dialog entries
        */
       public User getUser()
       {
          return new User(username.getText(), password.getPassword());
       }
    
       /**
        * Show the chooser panel in a dialog
        * @param parent a component in the owner frame or null
        * @param title the dialog window title
        */
       public boolean showDialog(Component parent, String title)
       {
          ok = false;
    
          // locate the owner frame
    
          Frame owner = null;
          if (parent instanceof Frame)
             owner = (Frame) parent;
          else
             owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);
    
          // if first time, or if owner has changed, make new dialog
    
          if (dialog == null || dialog.getOwner() != owner)
          {
             dialog = new JDialog(owner, true);
             dialog.add(this);
             dialog.getRootPane().setDefaultButton(okButton);
             dialog.pack();
          }
    
          // set title and show dialog
    
          dialog.setTitle(title);
          dialog.setVisible(true);
          return ok;
       }
    }

    运行结果:

    测试程序15

    elipse IDE中调试运行教材564页程序12-2112-2212-23,结合程序运行结果理解程序;

    掌握文件对话框的用法;

    记录示例代码阅读理解中存在的问题与疑惑。

    代码:

    运行结果:

    点击File中的Open后:

    elipse IDE中调试运行教材570页程序12-24,结合运行结果理解程序;

    了解颜色选择器的用法。

    记录示例代码阅读理解中存在的问题与疑惑。

    运行结果:

    三个按钮分别代表三种模式(详解在书上p570),这里选的Modal模式:

    共有五种标题(样本,HSV,HSL,RGB,CMYK)对话框

    小组讨论:

    负责程序:12-19。该程序主要是进行了数据的交换,使用对话框最通常的目的是获取用户的输人信息。在前面已经看到,构造对话框对象非常简单;首先初始化数据,然后调用 set Visible(true)就会在屏幕上显示对话框。

     (图1)  图2所示的对话框,可以用来获得用户名和用户密码以便连接某些在线服务。 

       

    对话框应该提供设置默认数据的方法。例如,示例程序中的 Passwordchooser类提供了一个 setuser方法,用来将默认值放到下面的字段中:public vold setuser(User u){
    username, settext(u, getname 0);} 一但设置了默认值(如果需要)就可以调用 setvisible(true)让对话框显示在屏幕上。然后用户输入信息,点击OK或者 Cancel按钮。

    这两个按钮的事件处理器都会调用set Visible( false)终止对 set Visible(true)的调用。另外,用户也可以选择关闭对话框。如果没有为对话框安装窗口监听器,就会

    执行默认的窗口结東操作,即对话框变为不可见,这也中止了对 setvisible(ue)的调用。 重要的问题是在用户解除这个对话框之前,一直调用 setvisible(true)阻塞。

    这样易于实现模式对话框。希望知道用户是接收对话框,还是取消对话框。在示例代码中设置了OK标志,在对话框显示之前是 false。只有OK按钮的事件处理器可以将

    它设置为tnue。这样,就可以获得对话框中的用户输入。

    实验总结:

    这次实验非常多,学起来比较难受,不过通过小组三人的讨论最终还是一一的顺利完成了,我们组这次所分析的是数据交换的用法方面的知识,有明确的分工,讨论,收获很大。

            

     

  • 相关阅读:
    js穿梭框;将两个table中的数据选中移动
    MySQL权限管理实战
    CentOS 7平台rpm包部署MySQL 8.0、Spring Security权限注解
    Nginx服务器配置服务实战
    创建索引,这些知识应该了解
    如何实现对ELK各组件的监控?试试Metricbeat
    大数据量查询容易OOM?试试MySQL流式查询
    免费正版IntelliJ IDEA license详细指南
    MySQL查询语句进阶知识集锦
    ClickHouse性能优化?试试物化视图
  • 原文地址:https://www.cnblogs.com/xuezhiqian/p/10053879.html
Copyright © 2011-2022 走看看