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.Color;
  9. import java.awt.FlowLayout;
  10. import java.awt.Font;
  11. import java.awt.GridLayout;
  12. import java.awt.HeadlessException;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.ActionListener;
  15. import javax.swing.BorderFactory;
  16. import javax.swing.BoxLayout;
  17. import javax.swing.JButton;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JOptionPane;
  21. import javax.swing.JPanel;
  22. import javax.swing.JScrollPane;
  23. import javax.swing.JTextArea;
  24. import javax.swing.JTextField;
  25. import javax.swing.border.Border;
  26. import javax.swing.border.TitledBorder;
  27.  
  28. /**
  29.  *
  30.  * @author AM
  31.  */
  32. public class DemoSwing extends JFrame {
  33.  
  34. JLabel jlbten,jlbdiachi;
  35.  
  36. JTextField jtxten;
  37.  
  38. JTextArea jtadiachi;
  39.  
  40. JButton jbt;
  41.  
  42.  
  43.  
  44. public DemoSwing(String title) throws HeadlessException {
  45. super(title);
  46. this.setSize(400,250);
  47.  
  48. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49. this.setLocationRelativeTo(null);
  50. this.setVisible(true);
  51. creatAndShow();
  52.  
  53. }
  54.  
  55. private void creatAndShow() {
  56.  
  57. //Tạo panle cha , thiết lập bố cục theo dòng
  58. JPanel jpn = new JPanel();
  59. jpn.setLayout(new FlowLayout());
  60.  
  61. //Tạo panel chứa các ô nhập liệu và label
  62. JPanel jpnthongtin = new JPanel(new GridLayout(3,2,10,10));
  63. Border border = BorderFactory.createLineBorder(Color.RED);
  64.  
  65. //Tạo border
  66. TitledBorder borderTitle = BorderFactory.createTitledBorder(border,"THÔNG TIN");
  67. jpnthongtin.setBorder(borderTitle);
  68.  
  69. jlbten = new JLabel("Nhập tên :");
  70. jlbten.setForeground(Color.yellow);
  71. jlbdiachi = new JLabel("Nhập ý kiến : ");
  72. jlbdiachi.setForeground(Color.YELLOW);
  73. Font font = new Font("Font.BOLD",20,20);
  74. jlbten.setFont(font);
  75. jlbdiachi.setFont(font);
  76. jlbten.setOpaque(true);
  77. jlbdiachi.setOpaque(true);
  78. jlbten.setBackground(Color.red);
  79. jlbdiachi.setBackground(Color.red);
  80.  
  81.  
  82.  
  83.  
  84. jtxten = new JTextField(15);
  85. jtadiachi = new JTextArea(4,10);
  86. JScrollPane jsc = new JScrollPane(jtadiachi,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  87.  
  88. jpnthongtin.add(jlbten);
  89. jpnthongtin.add(jtxten);
  90.  
  91. jpnthongtin.add(jlbdiachi);
  92. jpnthongtin.add(jsc);
  93.  
  94.  
  95. jpn.add(jpnthongtin);
  96.  
  97.  
  98. //Tạo panel thứ 3 chứa ô submit
  99. JPanel jpndefine = new JPanel(new FlowLayout());
  100. jbt = new JButton("SUBMIT");
  101. jbt.setFont(font);
  102. jpndefine.add(jbt);
  103.  
  104. jpn.add(jpndefine);
  105.  
  106.  
  107. //Gán sự kiện
  108. jbt.addActionListener(new action());
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. this.getContentPane().add(jpn);
  117. }
  118.  
  119. class action implements ActionListener
  120. {
  121.  
  122. @Override
  123. public void actionPerformed(ActionEvent e) {
  124. JOptionPane.showConfirmDialog(rootPane,"Thông tin bạn đã được gửi đến chúng tôi");
  125. }
  126.  
  127. }
  128.  
  129. public static void main(String[] args) {
  130. new DemoSwing("My window");
  131. }
  132.  
  133.  
  134. }
  135.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:32: 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