• 苏浪浪 201771010120 14周


    实验十四  Swing图形界面组件

    实验时间 2018-11-29

    1、实验目的与要求

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

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

    2、实验内容和步骤

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

    测试程序1

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

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

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

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

    package calculator;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     * A panel with calculator buttons and a result display.
     */
    public class CalculatorPanel extends JPanel
    {
       private JButton display;
       private JPanel panel;
       private double result;
       private String lastCommand;
       private boolean start;
    
       public CalculatorPanel()
       {
          setLayout(new BorderLayout());
    
          result = 0;
          lastCommand = "=";
          start = true;
    
          // 添加显示
    
          display = new JButton("0");
          display.setEnabled(false);//显示功能,使按钮不可按
          add(display, BorderLayout.NORTH);
    
          ActionListener insert = new InsertAction();
          ActionListener command = new CommandAction();
    
          // 在一个4×4的网格中添加按钮
    
          panel = new JPanel();
          panel.setLayout(new GridLayout(4, 4));//设置一个4行4列的网格
    
          addButton("1", insert);
          addButton("2", insert);
          addButton("3", insert);
          addButton("/", command);
    
          addButton("4", insert);
          addButton("5", insert);
          addButton("6", insert);
          addButton("*", command);
    
          addButton("7", insert);
          addButton("8", insert);
          addButton("9", insert);
          addButton("-", command);
    
          addButton(".", insert);
          addButton("0", insert);
          addButton("=", command);
          addButton("+", command);
    
          add(panel, BorderLayout.CENTER);
          
          JButton b1 = new JButton("验证");
          add(b1, BorderLayout.SOUTH);
          
          JButton bl = new JButton("1验证");
          add(bl, BorderLayout.WEST);
          
          JButton br = new JButton("2验证");
          add(br, BorderLayout.EAST);
          
       }
    
       /**
        * Adds a button to the center panel.
        * @param label the button label
        * @param listener the button listener
        */
       private void addButton(String label, ActionListener listener)
       {
          JButton button = new JButton(label);
          button.addActionListener(listener);
          panel.add(button);
       }
    
       /**
        * This action inserts the button action string to the end of the display text.
        */
       private class InsertAction implements ActionListener
       {
          public void actionPerformed(ActionEvent event)
          {
             String input = event.getActionCommand();
             if (start)
             {
                display.setText("");//判断是否处于起始状态
                start = false;
             }
             display.setText(display.getText() + input);
          }
       }
    
       /**
        * This action executes the command that the button action string denotes.
        */
       private class CommandAction implements ActionListener
       {
          public void actionPerformed(ActionEvent event)
          {
             String command = event.getActionCommand();
    
             if (start)
             {
                if (command.equals("-"))
                {
                   display.setText(command);
                   start = false;
                }
                else lastCommand = command;
             }
             else
             {
                calculate(Double.parseDouble(display.getText()));
                lastCommand = command;
                start = true;
             }
          }
       }
    
       /**
        * Carries out the pending calculation.
        * @param x the value to be accumulated with the prior result.
        */
       public void calculate(double x)
       {
          if (lastCommand.equals("+")) result += x;
          else if (lastCommand.equals("-")) result -= x;
          else if (lastCommand.equals("*")) result *= x;
          else if (lastCommand.equals("/")) result /= x;
          else if (lastCommand.equals("=")) result = x;
          display.setText(""+ result);
       }
    }

    测试程序2

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

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

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

    测试程序3

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

    掌握复选框组件的用法;

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

    package checkBox;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     * A frame with a sample text label and check boxes for selecting font
     * attributes.
     */
    public class CheckBoxFrame extends JFrame
    {
       private JLabel label;
       private JCheckBox bold;
       private JCheckBox italic;
       private static final int FONTSIZE = 24;
    
       public CheckBoxFrame()
       {
          // 添加示例文本标签
    
    
          label = new JLabel("The quick brown fox jumps over the lazy dog.");
          label.setFont(new Font("Serif", Font.BOLD, FONTSIZE));
          add(label, BorderLayout.CENTER);
    
          // 字体属性
          // 复选框状态的标签
    
          ActionListener listener = event -> {
             int mode = 0;
             if (bold.isSelected()) mode += Font.BOLD;
             if (italic.isSelected()) mode += Font.ITALIC;
             label.setFont(new Font("Serif", mode, FONTSIZE));
          };
    
          // 添加复选框
    
          JPanel buttonPanel = new JPanel();
    
          bold = new JCheckBox("Bold");
          bold.addActionListener(listener);
          bold.setSelected(true);
          buttonPanel.add(bold);
    
          italic = new JCheckBox("Italic");
          italic.addActionListener(listener);
          buttonPanel.add(italic);
    
          add(buttonPanel, BorderLayout.SOUTH);
          pack();
       }
    }

    测试程序4

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

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

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

    测试程序5

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

    掌握边框的用法;

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

    9package border;
    
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    /**
     * A frame with radio buttons to pick a border style.
     */
    public class BorderFrame extends JFrame
    {
       private JPanel demoPanel;
       private JPanel buttonPanel;
       private ButtonGroup group;
    
       public BorderFrame()
       {
          demoPanel = new JPanel();
          buttonPanel = new JPanel();
          group = new ButtonGroup();
    
          addRadioButton("Lowered bevel", BorderFactory.createLoweredBevelBorder());//创建一个具有凹入斜面边缘的边框
          addRadioButton("Raised bevel", BorderFactory.createRaisedBevelBorder());
          addRadioButton("Etched", BorderFactory.createEtchedBorder());//创建一个具有“浮雕化”外观效果的边框
          addRadioButton("Line", BorderFactory.createLineBorder(Color.BLUE));//创建一个具有指定颜色的线边框
          addRadioButton("Matte", BorderFactory.createMatteBorder(10, 10, 10, 10, Color.BLUE));//使用纯色创建一个类似衬边的边框。(
          addRadioButton("Empty", BorderFactory.createEmptyBorder());//使用纯色创建一个类似衬边的边框。
    
          Border etched = BorderFactory.createEtchedBorder();
          Border titled = BorderFactory.createTitledBorder(etched, "Border types");//向现有边框添加一个标题,
          buttonPanel.setBorder(titled);
    
          setLayout(new GridLayout(2, 1));//创建具有指定行数和列数的网格布局。给布局中的所有组件分配相等的大小。
          add(buttonPanel);
          add(demoPanel);
          pack();
       }
    
       public void addRadioButton(String buttonName, Border b)
       {
          JRadioButton button = new JRadioButton(buttonName);
          button.addActionListener(event -> demoPanel.setBorder(b));//将一个 ActionListener 添加到按钮中。 
          group.add(button);
          buttonPanel.add(button);
       }
    }

    测试程序6

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

    掌握组合框组件的用法;

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

     

    测试程序7

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

    掌握滑动条组件的用法;

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

    package slider;
    
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    /**
     * A frame with many sliders and a text field to show slider values.
     */
    public class SliderFrame extends JFrame
    {
       private JPanel sliderPanel;
       private JTextField textField;
       private ChangeListener listener;
    
       public SliderFrame()
       {
          sliderPanel = new JPanel();
          sliderPanel.setLayout(new GridBagLayout());
    
          // 所有滑块的公共侦听器
          listener = event -> {
             // update text field when the slider value changes
             JSlider source = (JSlider) event.getSource();
             textField.setText("" + source.getValue());
          };
    
          // 添加一个普通滑块
    
          JSlider slider = new JSlider();
          addSlider(slider, "Plain");
    
          //添加带有大调和小调的滑块
    
          slider = new JSlider();
          slider.setPaintTicks(true);
          slider.setMajorTickSpacing(20);
          slider.setMinorTickSpacing(5);
          addSlider(slider, "Ticks");
    
          // 添加一个滑块,吸附滴答声
          slider = new JSlider();
          slider.setPaintTicks(true);
          slider.setSnapToTicks(true);
          slider.setMajorTickSpacing(20);
          slider.setMinorTickSpacing(5);
          addSlider(slider, "Snap to ticks");
    
          //添加一个没有跟踪的滑块
    
          slider = new JSlider();
          slider.setPaintTicks(true);
          slider.setMajorTickSpacing(20);
          slider.setMinorTickSpacing(5);
          slider.setPaintTrack(false);
          addSlider(slider, "No track");
    
          // 添加一个反向滑块
    
          slider = new JSlider();
          slider.setPaintTicks(true);
          slider.setMajorTickSpacing(20);
          slider.setMinorTickSpacing(5);
          slider.setInverted(true);
          addSlider(slider, "Inverted");
    
          // 添加带有数字标签的滑块
    
          slider = new JSlider();
          slider.setPaintTicks(true);
          slider.setPaintLabels(true);
          slider.setMajorTickSpacing(20);
          slider.setMinorTickSpacing(5);
          addSlider(slider, "Labels");
    
          // 添加带有字母标签的滑块
    
          slider = new JSlider();
          slider.setPaintLabels(true);
          slider.setPaintTicks(true);
          slider.setMajorTickSpacing(20);
          slider.setMinorTickSpacing(5);
    
          Dictionary<Integer, Component> labelTable = new Hashtable<>();
          labelTable.put(0, new JLabel("A"));
          labelTable.put(20, new JLabel("B"));
          labelTable.put(40, new JLabel("C"));
          labelTable.put(60, new JLabel("D"));
          labelTable.put(80, new JLabel("E"));
          labelTable.put(100, new JLabel("F"));
    
          slider.setLabelTable(labelTable);
          addSlider(slider, "Custom labels");
    
          //添加带有图标标签的滑块
    
          slider = new JSlider();
          slider.setPaintTicks(true);
          slider.setPaintLabels(true);
          slider.setSnapToTicks(true);
          slider.setMajorTickSpacing(20);
          slider.setMinorTickSpacing(20);
    
          labelTable = new Hashtable<Integer, Component>();
    
          // 添加卡图片
    
          labelTable.put(0, new JLabel(new ImageIcon("nine.gif")));
          labelTable.put(20, new JLabel(new ImageIcon("ten.gif")));
          labelTable.put(40, new JLabel(new ImageIcon("jack.gif")));
          labelTable.put(60, new JLabel(new ImageIcon("queen.gif")));
          labelTable.put(80, new JLabel(new ImageIcon("king.gif")));
          labelTable.put(100, new JLabel(new ImageIcon("ace.gif")));
    
          slider.setLabelTable(labelTable);
          addSlider(slider, "Icon labels");
    
          // 添加显示滑块值的文本字段
    
          textField = new JTextField();
          add(sliderPanel, BorderLayout.CENTER);
          add(textField, BorderLayout.SOUTH);
          pack();
       }
    
       /**
        * Adds a slider to the slider panel and hooks up the listener
        * @param s the slider
        * @param description the slider description
        */
       public void addSlider(JSlider s, String description)
       {
          s.addChangeListener(listener);
          JPanel panel = new JPanel();
          panel.add(s);
          panel.add(new JLabel(description));
          panel.setAlignmentX(Component.LEFT_ALIGNMENT);
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridy = sliderPanel.getComponentCount();
          gbc.anchor = GridBagConstraints.WEST;
          sliderPanel.add(panel, gbc);
       }
    }

    测试程序8

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

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

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

    package menu;
    
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     * A frame with a sample menu bar.
     */
    public class MenuFrame extends JFrame
    {
       private static final int DEFAULT_WIDTH = 300;
       private static final int DEFAULT_HEIGHT = 200;
       private Action saveAction;
       private Action saveAsAction;
       private JCheckBoxMenuItem readonlyItem;
       private JPopupMenu popup;
    
       /**
        * A sample action that prints the action name to System.out
        */
       class TestAction extends AbstractAction
       {
          public TestAction(String name)
          {
             super(name);
          }
    
          public void actionPerformed(ActionEvent event)
          {
             System.out.println(getValue(Action.NAME) + " selected.");
          }
       }
    
       public MenuFrame()
       {
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    
          JMenu fileMenu = new JMenu("File");
          fileMenu.add(new TestAction("New"));
    
          //演示加速器
    
          JMenuItem openItem = fileMenu.add(new TestAction("Open"));
          openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));
    
          fileMenu.addSeparator();
    
          saveAction = new TestAction("Save");
          JMenuItem saveItem = fileMenu.add(saveAction);
          saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
    
          saveAsAction = new TestAction("Save As");
          fileMenu.add(saveAsAction);
          fileMenu.addSeparator();
    
          fileMenu.add(new AbstractAction("Exit")
             {
                public void actionPerformed(ActionEvent event)
                {
                   System.exit(0);
                }
             });
    
          //演示复选框和单选按钮菜单
    
          readonlyItem = new JCheckBoxMenuItem("Read-only");
          readonlyItem.addActionListener(new ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                   boolean saveOk = !readonlyItem.isSelected();
                   saveAction.setEnabled(saveOk);
                   saveAsAction.setEnabled(saveOk);
                }
             });
    
          ButtonGroup group = new ButtonGroup();
    
          JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");
          insertItem.setSelected(true);
          JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype");
    
          group.add(insertItem);
          group.add(overtypeItem);
    
          //演示图标
    
          Action cutAction = new TestAction("Cut");
          cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif"));
          Action copyAction = new TestAction("Copy");
          copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif"));
          Action pasteAction = new TestAction("Paste");
          pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif"));
    
          JMenu editMenu = new JMenu("Edit");
          editMenu.add(cutAction);
          editMenu.add(copyAction);
          editMenu.add(pasteAction);
    
          // 演示嵌套菜单
    
          JMenu optionMenu = new JMenu("Options");
    
          optionMenu.add(readonlyItem);
          optionMenu.addSeparator();
          optionMenu.add(insertItem);
          optionMenu.add(overtypeItem);
    
          editMenu.addSeparator();
          editMenu.add(optionMenu);
    
          // 说明助记符
    
          JMenu helpMenu = new JMenu("Help");
          helpMenu.setMnemonic('H');
    
          JMenuItem indexItem = new JMenuItem("Index");
          indexItem.setMnemonic('I');
          helpMenu.add(indexItem);
    
          //您还可以向操作添加助记符键
          Action aboutAction = new TestAction("About");
          aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
          helpMenu.add(aboutAction);
          
          // 将所有顶级菜单添加到菜单栏
    
          JMenuBar menuBar = new JMenuBar();
          setJMenuBar(menuBar);
    
          menuBar.add(fileMenu);
          menuBar.add(editMenu);
          menuBar.add(helpMenu);
    
          // 显示弹出窗口
    
          popup = new JPopupMenu();
          popup.add(cutAction);
          popup.add(copyAction);
          popup.add(pasteAction);
    
          JPanel panel = new JPanel();
          panel.setComponentPopupMenu(popup);
          add(panel);
       }
    }

    测试程序9

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

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

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

    package toolBar;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     * A frame with a toolbar and menu for color changes.
     */
    public class ToolBarFrame extends JFrame
    {
       private static final int DEFAULT_WIDTH = 300;
       private static final int DEFAULT_HEIGHT = 200;
       private JPanel panel;
    
       public ToolBarFrame()
       {
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    
          //添加用于颜色更改的面板
    
          panel = new JPanel();
          add(panel, BorderLayout.CENTER);
    
          // 设置操作
          Action blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.BLUE);
          Action yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"),
                Color.YELLOW);
          Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.RED);
    
          Action exitAction = new AbstractAction("Exit", new ImageIcon("exit.gif"))
             {
                public void actionPerformed(ActionEvent event)
                {
                   System.exit(0);
                }
             };
          exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit");
    
          // 填充工具栏
    
          JToolBar bar = new JToolBar();
          bar.add(blueAction);
          bar.add(yellowAction);
          bar.add(redAction);
          bar.addSeparator();
          bar.add(exitAction);
          add(bar, BorderLayout.NORTH);
    
          
          //填充菜单
          JMenu menu = new JMenu("Color");
          menu.add(yellowAction);
          menu.add(blueAction);
          menu.add(redAction);
          menu.add(exitAction);
          JMenuBar menuBar = new JMenuBar();
          menuBar.add(menu);
          setJMenuBar(menuBar);
       }
    
       /**
        * The color action sets the background of the frame to a given color.
        */
       class ColorAction extends AbstractAction
       {
          public ColorAction(String name, Icon icon, Color c)
          {
             putValue(Action.NAME, name);
             putValue(Action.SMALL_ICON, icon);
             putValue(Action.SHORT_DESCRIPTION, name + " background");
             putValue("Color", c);
          }
    
          public void actionPerformed(ActionEvent event)
          {
             Color c = (Color) getValue("Color");
             panel.setBackground(c);
          }
       }
    }

    测试程序10

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

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

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

    package gridbag;
    
    import java.awt.Font;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    
    /**
     * A frame that uses a grid bag layout to arrange font selection components.
     */
    public class FontFrame extends JFrame
    {
       public static final int TEXT_ROWS = 10;
       public static final int TEXT_COLUMNS = 20;
    
       private JComboBox<String> face;
       private JComboBox<Integer> size;
       private JCheckBox bold;
       private JCheckBox italic;
       private JTextArea sample;
    
       public FontFrame()
       {
          GridBagLayout layout = new GridBagLayout();
          setLayout(layout);
    
          ActionListener listener = event -> updateSample();
    
          // 构建组件
    
          JLabel faceLabel = new JLabel("Face: ");
    
          face = new JComboBox<>(new String[] { "Serif", "SansSerif", "Monospaced",
                "Dialog", "DialogInput" });
    
          face.addActionListener(listener);
    
          JLabel sizeLabel = new JLabel("Size: ");
    
          size = new JComboBox<>(new Integer[] { 8, 10, 12, 15, 18, 24, 36, 48 });
    
          size.addActionListener(listener);
    
          bold = new JCheckBox("Bold");
          bold.addActionListener(listener);
    
          italic = new JCheckBox("Italic");
          italic.addActionListener(listener);
    
          sample = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
          sample.setText("The quick brown fox jumps over the lazy dog");
          sample.setEditable(false);
          sample.setLineWrap(true);
          sample.setBorder(BorderFactory.createEtchedBorder());
    
          // 使用GBC便利类向网格添加组件
    
          add(faceLabel, new GBC(0, 0).setAnchor(GBC.EAST));
          add(face, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0)
                .setInsets(1));
          add(sizeLabel, new GBC(0, 1).setAnchor(GBC.EAST));
          add(size, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0)
                .setInsets(1));
          add(bold, new GBC(0, 2, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
          add(italic, new GBC(0, 3, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
          add(sample, new GBC(2, 0, 1, 4).setFill(GBC.BOTH).setWeight(100, 100));
          pack();
          updateSample();
       }
    
       public void updateSample()
       {
          String fontFace = (String) face.getSelectedItem();
          int fontStyle = (bold.isSelected() ? Font.BOLD : 0)
                + (italic.isSelected() ? Font.ITALIC : 0);
          int fontSize = size.getItemAt(size.getSelectedIndex());
          Font font = new Font(fontFace, fontStyle, fontSize);
          sample.setFont(font);
          sample.repaint();
       }
    }

    package groupLayout;
    
    import java.awt.Font;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.GroupLayout;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.LayoutStyle;
    import javax.swing.SwingConstants;
    
    /**
     * A frame that uses a group layout to arrange font selection components.
     */
    public class FontFrame extends JFrame
    {
       public static final int TEXT_ROWS = 10;
       public static final int TEXT_COLUMNS = 20;
    
       private JComboBox<String> face;
       private JComboBox<Integer> size;
       private JCheckBox bold;
       private JCheckBox italic;
       private JScrollPane pane;
       private JTextArea sample;
    
       public FontFrame()
       {
          ActionListener listener = event -> updateSample(); 
    
          // 构建组件
    
          JLabel faceLabel = new JLabel("Face: ");
    
          face = new JComboBox<>(new String[] { "Serif", "SansSerif", "Monospaced", "Dialog",
                "DialogInput" });
    
          face.addActionListener(listener);
    
          JLabel sizeLabel = new JLabel("Size: ");
    
          size = new JComboBox<>(new Integer[] { 8, 10, 12, 15, 18, 24, 36, 48 });
    
          size.addActionListener(listener);
    
          bold = new JCheckBox("Bold");
          bold.addActionListener(listener);
    
          italic = new JCheckBox("Italic");
          italic.addActionListener(listener);
    
          sample = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
          sample.setText("The quick brown fox jumps over the lazy dog");
          sample.setEditable(false);
          sample.setLineWrap(true);
          sample.setBorder(BorderFactory.createEtchedBorder());
    
          pane = new JScrollPane(sample);
    
          GroupLayout layout = new GroupLayout(getContentPane());
          setLayout(layout);
          layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(
                      layout.createSequentialGroup().addContainerGap().addGroup(
                            layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
                                  GroupLayout.Alignment.TRAILING,
                                  layout.createSequentialGroup().addGroup(
                                        layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                              .addComponent(faceLabel).addComponent(sizeLabel))
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                              layout.createParallelGroup(
                                                    GroupLayout.Alignment.LEADING, false)
                                                    .addComponent(size).addComponent(face)))
                                  .addComponent(italic).addComponent(bold)).addPreferredGap(
                            LayoutStyle.ComponentPlacement.RELATED).addComponent(pane)
                            .addContainerGap()));
    
          layout.linkSize(SwingConstants.HORIZONTAL, new java.awt.Component[] { face, size });
    
          layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(
                      layout.createSequentialGroup().addContainerGap().addGroup(
                            layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(
                                  pane, GroupLayout.Alignment.TRAILING).addGroup(
                                  layout.createSequentialGroup().addGroup(
                                        layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                              .addComponent(face).addComponent(faceLabel))
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                              layout.createParallelGroup(
                                                    GroupLayout.Alignment.BASELINE).addComponent(size)
                                                    .addComponent(sizeLabel)).addPreferredGap(
                                              LayoutStyle.ComponentPlacement.RELATED).addComponent(
                                              italic, GroupLayout.DEFAULT_SIZE,
                                              GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(bold, GroupLayout.DEFAULT_SIZE,
                                              GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addContainerGap()));
          pack();
       }
       
       public void updateSample()
       {
          String fontFace = (String) face.getSelectedItem();
          int fontStyle = (bold.isSelected() ? Font.BOLD : 0)
                + (italic.isSelected() ? Font.ITALIC : 0);
          int fontSize = size.getItemAt(size.getSelectedIndex());
          Font font = new Font(fontFace, fontStyle, fontSize);
          sample.setFont(font);
          sample.repaint();
       }
    }

    测试程序11

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

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

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

     

    测试程序12

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

    掌握选项对话框的用法。

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

    测试程序13

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

    掌握对话框的创建方法;

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

    测试程序14

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

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

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

    测试程序15

    在elipse IDE中调试运行教材556页程序12-21、12-2212-23,结合程序运行结果理解程序;

    掌握文件对话框的用法;

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

    测试程序16

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

    了解颜色选择器的用法。

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

    实验2:组内讨论反思本组负责程序,理解程序总体结构,梳理程序GUI设计中应用的相关组件,整理相关组件的API,对程序中组件应用的相关代码添加注释。

    实验3:组间协同学习:在本班课程QQ群内,各位同学对实验1中存在的问题进行提问,提问时注明实验1中的测试程序编号,负责对应程序的小组需及时对群内提问进行回答。

     

     实验总结:通过小组内的商讨大致理解了这些测试程序的基本思路;也在合作伙伴的身上学到了一些学习的方法。

    在老师与助教学长的帮助下熟悉了本章的一些基础知识;

    但仍有许多问题需要解决,正在努力解决中。。。

     

     

  • 相关阅读:
    python 数据分析3
    python 数据分析2
    Python 数据分析1
    Python18 Django 基础
    Python 17 web框架&Django
    一只救助犬的最后遗言
    With As 获取 id parentId 递归获取所有
    分布式事物
    div 浮动框
    sql时间比较
  • 原文地址:https://www.cnblogs.com/xiaolangoxiaolang/p/10040089.html
Copyright © 2020-2023  润新知