THỰC HÀNH CÔNG CỤ VÀ MÔI TRƯỜNG VÀ LẬP TRÌNH 2 --------------------------------------------------------------------------------------------------- TS. Võ Phương Bình Email: binhvp@dlu.edu.vn Information Technology Faculty - Dalat University Website: http://it.dlu.edu.vn/ivp-lab --------------------------------------------------------------------------------------------------- LAB 4(4 tiết): Thực hành ứng dụng GUI(tiếp) A. Mục tiêu: - Hiểu biết thiết kế giao diện với môi trường đồ họa nâng cao. - Xử lý các sự kiện trên môi trường GUI nâng cao B. Kết quả sau khi hoàn thành: - Sử dụng được các thành phần thiết kế để có thể tạo ra các giao diện. - Xây dựng thành công các ứng dụng. - Xử lý được các sự kiện trong Java. C. Luyện tập: - Tìm hiểu các kiến thức liên qu n đến r me thêm omponent v o r me v xử lý các sự kiện c các omponent - Xem lại slide b i học các kiến thức liên qu n đến thiết kế GUI D. Bài tập. Bài 1: Viết chương trình tạo các Slider v xử lý sự kiện c chúng như minh họ s u: Minh họ : Hướng dẫn: Tạo Slider thuộc lớp JSlider v lắng nghe sự kiện Ví dụ: redslider = new JSlider(0, 255, 0); redslider.addchangelistener(this); Thừ kế intef ce ChangeListener xử lý sự kiện trong phương thức statechanged(changeevent evt).
/* A RGBColorChooser shows three sliders that the user can manipulate to set the red, green, and blue, components of a color. A color patch shows the selected color, and there are three labels that show the numerical values of all the components. Values are in the range 0 to 255. The initial color is black. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class RGBColorChooser extends JFrame implements ChangeListener { private JSlider redslider, greenslider, blueslider; // For setting the color. private JLabel redlabel, greenlabel, bluelabel;
// For displaying RGB values. private JPanel colorpatch; // Color patch for displaying the color. public RGBColorChooser() { /* Create JSliders with possible values from 0 to 255. */ redslider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0); greenslider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0); blueslider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0); /* Create JLabels showing current RGB and HSB values. */ redlabel = new JLabel(" R = 0"); greenlabel = new JLabel(" G = 0"); bluelabel = new JLabel(" B = 0"); /* Set the colors of the labels, and make them opaque */
redlabel.setbackground(color.white); redlabel.setforeground(color.red); redlabel.setopaque(true); greenlabel.setbackground(color.white); greenlabel.setforeground(new Color(0,150,0)); greenlabel.setopaque(true); bluelabel.setbackground(color.white); bluelabel.setforeground(color.blue); bluelabel.setopaque(true); /* Set the applet to listen for changes to the JSliders' values */ redslider.addchangelistener(this); greenslider.addchangelistener(this); blueslider.addchangelistener(this); /* Create a JPanel whose background color will always be set to the currently selected color. Otherwise, the panel is empty. */ colorpatch = new JPanel(); colorpatch.setbackground(color.black); /* Create the applet layout, which consists of a row of
three equal-sized regions holding the JSliders, the Labels, and the color patch. The background color of the applet is gray, which will show around the edges and between components. */ setbackground(color.gray); getcontentpane().setbackground(color.gray); getcontentpane().setlayout(new GridLayout(1,3,3,3)); JPanel scrolls = new JPanel(); JPanel labels = new JPanel(); scrolls.setbackground(color.gray); labels.setbackground(color.gray); getcontentpane().add(scrolls); getcontentpane().add(labels); getcontentpane().add(colorpatch); /* Add the JSliders and the JLabels to their respective panels. */ scrolls.setlayout(new GridLayout(3,1,2,2)); scrolls.add(redslider); scrolls.add(greenslider); scrolls.add(blueslider);
labels.setlayout(new GridLayout(3,1,2,2)); labels.add(redlabel); labels.add(greenlabel); labels.add(bluelabel); } // end init(); public void statechanged(changeevent evt) { // This is called when the user has changed the value on // one of the sliders. All the sliders are checked, // the labels are set to display the correct values, // and the color patch is set to correspond to the new color. int r = redslider.getvalue(); int g = greenslider.getvalue(); int b = blueslider.getvalue(); redlabel.settext(" R = " + r); greenlabel.settext(" G = " + g); bluelabel.settext(" B = " + b); colorpatch.setbackground(new Color(r,g,b)); } // end statechanged
} // end class RGBColorChooser Bài 2: Viết chương trình tạo Menu v xử lý sự kiện tại các MenuItem như minh họ s u: Minh họa: Hướng dẫn: Menu File thuộc lớp JMenu chứ các JMenuItem menu New và Open thuộc lớp JMenuItem JMenub r chứ menu JMenu Đặt phím tắt cho các JMenu bằng cách dùng phương thức setmnemonic Ví dụ: menufile.setmnemonic('f'); Đặt phím tắt cho các JMenuItem bằng phương thức setaccelerator Ví dụ: itemnew.setaccelerator( KeyStroke.getKeyStroke("ctrl N") ); E. Kết quả thực hành. - Sinh viên thực h nh ứng dụng trên GUI - Thời gi n thực h nh: 4 tiết F. Đánh giá: - Kiểm tr lại chương trình thử các kết quả - Bắt các lỗi bằng cách sử dụng các phần bắt lỗi: try catch. -------------------Hết-------------------