简单图形界面

以前博客不用了!!!

1窗口界面:在窗口中加入标签,文本行,按钮三个组件;

2窗口功能:在文本行中输入一行字符串后,点击按钮,文本行中的字符串出现在标签中。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class S1 extends JFrame{
 private JLabel label = new JLabel();
 private JTextField jtf = new JTextField();
 private JButton jbt = new JButton("OK");
 
 public S1() {
  JPanel p = new JPanel(new GridLayout(3,1));
  p.add(label);
  p.add(jtf);
  p.add(jbt);
  
  add(p, BorderLayout.CENTER);
  
  jbt.addActionListener(new ButtonListener());
 }
 public class ButtonListener implements ActionListener{
  public void actionPerformed(ActionEvent e) {
   String text = jtf.getText();
   label.setText(text);
  }
 } 
 public static void main(String[] args) {
  JFrame frame = new S1();
  frame.setTitle("图形界面实验1");
  frame.setSize(300,300);
  frame.setLocationRelativeTo(null);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
 }
}

 

posted @ 2017-11-07 09:00  小仙女63  阅读(218)  评论(0编辑  收藏  举报