fork download
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package SwingPacket;
  7.  
  8. import java.awt.Button;
  9. import java.awt.Color;
  10. import java.awt.FlowLayout;
  11. import java.awt.Font;
  12. import java.awt.GridLayout;
  13. import java.awt.HeadlessException;
  14. import java.awt.event.ActionEvent;
  15. import java.awt.event.ActionListener;
  16. import javax.swing.BorderFactory;
  17. import javax.swing.BoxLayout;
  18. import javax.swing.ButtonGroup;
  19. import javax.swing.JButton;
  20. import javax.swing.JCheckBox;
  21. import javax.swing.JFrame;
  22. import javax.swing.JLabel;
  23. import javax.swing.JOptionPane;
  24. import javax.swing.JPanel;
  25. import javax.swing.JRadioButton;
  26. import javax.swing.JScrollPane;
  27. import javax.swing.JTextArea;
  28. import javax.swing.JTextField;
  29. import javax.swing.border.Border;
  30. import javax.swing.border.TitledBorder;
  31.  
  32. /**
  33.  *
  34.  * @author AM
  35.  */
  36. public class DemoSwing extends JFrame {
  37.  
  38. JLabel jlbten,jlbdiachi;
  39.  
  40. JTextField jtxten;
  41.  
  42. JTextArea jtadiachi;
  43.  
  44. JButton jbt;
  45.  
  46. JRadioButton jcb1,jcb2,jcb3,jcb4;
  47.  
  48. JCheckBox jcba,jcbb,jcbc,jcbd;
  49. public DemoSwing(String title) throws HeadlessException {
  50. super(title);
  51. this.setSize(400,250);
  52.  
  53. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54. this.setLocationRelativeTo(null);
  55. this.setVisible(true);
  56. creatAndShow();
  57.  
  58. }
  59.  
  60. private void creatAndShow() {
  61.  
  62. //Tạo panle cha , thiết lập bố cục theo dòng
  63. JPanel jpn = new JPanel();
  64. jpn.setLayout(new FlowLayout());
  65.  
  66. //Tạo panel chứa các ô nhập liệu và label
  67. JPanel jpnthongtin = new JPanel(new GridLayout(3,2,10,10));
  68. Border border = BorderFactory.createLineBorder(Color.RED);
  69.  
  70. //Tạo border
  71. TitledBorder borderTitle = BorderFactory.createTitledBorder(border,"THÔNG TIN");
  72. jpnthongtin.setBorder(borderTitle);
  73.  
  74. jlbten = new JLabel("Nhập tên :");
  75. jlbten.setForeground(Color.yellow);
  76. jlbdiachi = new JLabel("Nhập ý kiến : ");
  77. jlbdiachi.setForeground(Color.YELLOW);
  78. Font font = new Font("Font.BOLD",20,20);
  79. jlbten.setFont(font);
  80. jlbdiachi.setFont(font);
  81. jlbten.setOpaque(true);
  82. jlbdiachi.setOpaque(true);
  83. jlbten.setBackground(Color.red);
  84. jlbdiachi.setBackground(Color.red);
  85.  
  86.  
  87.  
  88.  
  89. jtxten = new JTextField(15);
  90. jtadiachi = new JTextArea(4,10);
  91. JScrollPane jsc = new JScrollPane(jtadiachi,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  92.  
  93. jpnthongtin.add(jlbten);
  94. jpnthongtin.add(jtxten);
  95.  
  96. jpnthongtin.add(jlbdiachi);
  97. jpnthongtin.add(jsc);
  98.  
  99.  
  100. jpn.add(jpnthongtin);
  101.  
  102.  
  103. //Tạo panel thứ 3 chứa ô submit
  104. JPanel jpndefine = new JPanel(new FlowLayout());
  105. jbt = new JButton("SUBMIT");
  106. jbt.setFont(font);
  107. jpndefine.add(jbt);
  108.  
  109. jpn.add(jpndefine);
  110.  
  111.  
  112. //Gán sự kiện
  113. jbt.addActionListener(new action());
  114.  
  115. //Check box đánh giá chất lượng dịch vụ
  116. JPanel jpncheck1 = new JPanel(new GridLayout(2, 2));
  117. Border border1 = BorderFactory.createLineBorder(Color.BLUE);
  118. TitledBorder title = BorderFactory.createTitledBorder(border1,"Đánh giá chất lượng dịch vụ");
  119. jpncheck1.setBorder(title);
  120.  
  121. jcb1 = new JRadioButton("Rất hài lòng");
  122. jcb1.setFont(font);
  123. jcb2 = new JRadioButton("Hài lòng");
  124. jcb2.setFont(font);
  125. jcb3 = new JRadioButton("Tạm chấp nhận");
  126. jcb3.setFont(font);
  127. jcb4 = new JRadioButton("Không được tốt");
  128. jcb4.setFont(font);
  129.  
  130. ButtonGroup btg = new ButtonGroup();
  131. btg.add(jcb1);
  132. btg.add(jcb2);
  133. btg.add(jcb3);
  134. btg.add(jcb4);
  135.  
  136. jpncheck1.add(jcb1);
  137. jpncheck1.add(jcb2);
  138. jpncheck1.add(jcb3);
  139. jpncheck1.add(jcb4);
  140.  
  141.  
  142.  
  143.  
  144. //checkbox sở thích
  145. JPanel jpncheck2 = new JPanel(new GridLayout(2, 2));
  146. Border border2 = BorderFactory.createLineBorder(Color.BLUE);
  147. TitledBorder title2 = BorderFactory.createTitledBorder(border2,"Sở thích");
  148. jpncheck2.setBorder(title2);
  149.  
  150. jcba = new JCheckBox("Bơi lội");
  151. jcba.setFont(font);
  152. jcbb = new JCheckBox("Đá bóng");
  153. jcbb.setFont(font);
  154. jcbc = new JCheckBox("Xem phim");
  155. jcbc.setFont(font);
  156. jcbd = new JCheckBox("Du lịch");
  157. jcbd.setFont(font);
  158.  
  159.  
  160.  
  161. jpncheck2.add(jcba);
  162. jpncheck2.add(jcbb);
  163. jpncheck2.add(jcbc);
  164. jpncheck2.add(jcbd);
  165.  
  166.  
  167. jpn.add(jpncheck1);
  168. jpn.add(jpncheck2);
  169. this.getContentPane().add(jpn);
  170. }
  171.  
  172. class action implements ActionListener
  173. {
  174.  
  175. @Override
  176. public void actionPerformed(ActionEvent e) {
  177. JOptionPane.showConfirmDialog(rootPane,"Thông tin bạn đã được gửi đến chúng tôi");
  178. }
  179.  
  180. }
  181.  
  182. public static void main(String[] args) {
  183. new DemoSwing("My window");
  184. }
  185.  
  186.  
  187. }
  188.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:36: error: class DemoSwing is public, should be declared in a file named DemoSwing.java
public class DemoSwing extends JFrame {
       ^
1 error
stdout
Standard output is empty