码迷,mamicode.com
首页 > 编程语言 > 详细

java swing示例

时间:2016-06-01 22:57:32      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

该范例主要是JFrame(框架)和Jpanel(画板),在Jpanel容器上添加控件,然后再把Jpanel放进JFrame的容器里面。

 

FrameDemo.java

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;


public class FrameDemo extends JFrame{
    public static void main(String[] args)  throws Exception {
        
        //获取主显示器屏幕大小 
        Toolkit kit = Toolkit.getDefaultToolkit(); 
        Dimension screenSize = kit.getScreenSize(); 
        int screenWidth = screenSize.width; 
        int screenHeight = screenSize.height; 

        //设置界面组件
        ComponentDemo component = new ComponentDemo();
        FrameDemo frameDemo = new FrameDemo();
        frameDemo.setContentPane(component);   //将Panel放入到Frame中
        frameDemo.setSize(400, 500);
        frameDemo.setLocation(screenWidth / 2 - 390 / 2, screenHeight / 2 - 580 / 2);
        frameDemo.setVisible(true);
    
    }

}

 

ComponentDemo.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;


public class ComponentDemo extends JPanel implements ActionListener {
    //JLabel是标签,JTextField是文本框
    private JLabel userLabel = null;
    private JTextField userText = null;

    private JLabel passwordLabel = null;
    private JTextField passwordText = null;
    
    private JButton btnLogin = null;
    
    private JLabel contentLabel = null;
    private JTextPane contentPane = null;

    //组件初始化
    public ComponentDemo() {
        super();
        initialize();
    }

    private void initialize() {

        userLabel = new JLabel();
        /*用setBounds(Rectangle r)画矩形,Rectangle(x,y,weight,height)
            x,y表示矩形的左上角坐标,weight表示矩形长度,height表示高度                          */
        userLabel.setBounds(new java.awt.Rectangle(10, 10, 70, 22));
        userLabel.setText("账号:");
        this.setLayout(null);
        //添加Label(标签)和TextField(文本框)
        this.add(userLabel, null);
        this.add(getUserText(), null);
        
        this.add(getPasswordLabel(), null);
        this.add(getPasswordText(), null);
        
        this.add(getBtnLogin(), null);
        
        this.add(getContentLabel(), null);
        this.add(getContentPaneText(), null);

    }

    private JTextField getUserText() {
        if (userText == null) {
            userText = new JTextField();
            userText.setBounds(new java.awt.Rectangle(80, 10, 100, 22));
        }
        return userText;
    }

    private JLabel getPasswordLabel() {
       if (passwordLabel == null) {
        passwordLabel = new JLabel();
        passwordLabel.setText("密码:");
        passwordLabel.setBounds(new java.awt.Rectangle(210, 10, 70, 22));
      }
    return passwordLabel;
}

    private JTextField getPasswordText() {
       if (passwordText == null) {
         passwordText = new JTextField();
         passwordText.setBounds(new java.awt.Rectangle(280, 10, 100, 22));
       }
    return passwordText;
}


    private JLabel getContentLabel() {
        if (contentLabel == null) {
            contentLabel = new JLabel();
            contentLabel.setText("内容如下:");
            contentLabel.setBounds(new java.awt.Rectangle(10, 70, 70, 22));
        }
        return contentLabel;
    }

    private JTextPane getContentPaneText() {
        if (contentPane == null) {
            contentPane = new JTextPane();
            contentPane.setEditable(false);
            contentPane.setBounds(new java.awt.Rectangle(10, 90, 370, 150));
        }
        return contentPane;
    }




    private JButton getBtnLogin() {
        if (btnLogin == null) {
            btnLogin = new JButton("登录");
            btnLogin.setBounds(new java.awt.Rectangle(150, 45, 100, 22));
            btnLogin.addActionListener(this);         //注册监听事件
        }
        return btnLogin;
    }


    public void actionPerformed(ActionEvent e) {
        //在这里添加按下按钮后的触发事件
        
        
        
        
        
        
    }

}

代码运行结果如下:

技术分享

java swing示例

标签:

原文地址:http://www.cnblogs.com/expiator/p/5551319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!