您的位置:首页 > 编程语言 > Java开发

【java】java swing初识:事件触发&登陆界面

2017-05-03 16:31 405 查看
欢迎大家到我的个人主页点击打开链接,一起交流、学习,一起进步~
1.界面输出字符:hello world!

package vaniot.com;
import javax.swing.*;
public class HelloWorld {

public static void createAndShow(){
//好看的界面风格
JFrame.setDefaultLookAndFeelDecorated(true);
//创建及设置窗口
JFrame frame=new JFrame("HelloWorld");
//使JTable中关闭窗口后让后台程序也一起关闭,若缺少内存将会越占越满
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//窗口显示的内容
JLabel label=new JLabel("hello world!");
frame.getContentPane().add(label);
//显示窗口
frame.pack();
frame.setVisible(true);
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//显示窗口
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndShow();
}
});
}

}


[align=justify]效果截图:[/align]



[align=justify]
[/align]
2.界面由按钮触发,输出字符:hello world!!

package vaniot.com;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class HelloWorld2 {
public static void main(String[] args)throws Exception{
NewFrame frame=new NewFrame();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class NewFrame extends JFrame{
private JButton button;
private JTextField text;
public NewFrame(){
super();
this.setSize(300,300);
//布局
this.getContentPane().setLayout(null);
//添加按钮
this.add(this.getButton(),null);
this.add(this.getText(),null);
this.setTitle("hello world");
}

private JButton getButton() {
// TODO Auto-generated method stub
if(button==null){
button=new JButton();
button.setBounds(20,20,50,20);
button.setText("hi");
//添加监听类
button.addActionListener(new HelloButton());
}
return button;
}
private JTextField getText(){
if(text==null){
text=new JTextField();
text.setBounds(20,50,100,20);
}
return text;
}

private class HelloButton implements ActionListener{
public void actionPerformed(ActionEvent e){
//System.out.println("hello world!!");
text.setText("hello world!!");
}
}

}

效果截图:





3.用户登录界面,输入用户名及密码

package vaniot.com;
import javax.swing.*;
public class LoginExample {

public static void placeComponents(JPanel panel){
//布局管理器
panel.setLayout(null);
//user:
JLabel userlabel=new JLabel("User:");
//设置组件位置,setBounds(x,y,width,height)
userlabel.setBounds(10,20,80,25);
panel.add(userlabel);
//创建文本域用于用户输入
JTextField usertext=new JTextField(20);
usertext.setBounds(100,20,165,25);
panel.add(usertext);
//password:
JLabel pwlabel=new JLabel("password:");
pwlabel.setBounds(10,60,80,25);
panel.add(pwlabel);
//password输入域,输入以点号代替
JPasswordField pwtext=new JPasswordField(20);
pwtext.setBounds(100,60,165,25);
panel.add(pwtext);
//登录按钮
JButton loginButton=new JButton("login");
loginButton.setBounds(10,100,80,25);
panel.add(loginButton);

}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame("login example");
//设置窗口大小
frame.setSize(400,400);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
//创建面板
JPanel panel=new JPanel();
frame.add(panel);
placeComponents(panel);
//界面可见
frame.setVisible(true);
}
}


效果截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java swing 界面