图片看起来很模糊,隐约看到需要一个登录窗口,那就分享一下以前练习的登录窗口demo吧。
先上效果图:
登录界面
源码如下:
AbsoluteLoginFrame.java
public class AbsoluteLoginFrame extends JFrame {
private static final int LOGIN_WIDTH = 600
private static final int LOGIN_HEIGHT = 400
private static final long serialVersionUID = -2381351968820980500L
public AbsoluteLoginFrame(){
//设置窗口标题
setTitle("登录界面")
//设置一个初始面板,填充整个窗口
JPanel loginPanel = new JPanel()
//设置背景颜色
loginPanel.setBackground(new Color(204, 204, 204))//#CCC
loginPanel.setLayout(null)
JPanel centerPanel = new JPanel()
centerPanel.setBackground(Color.WHITE)
centerPanel.setBounds(114, 70, 360, 224)
centerPanel.setLayout(null)
JLabel jLabel = new JLabel("用户名:")
jLabel.setOpaque(true)
jLabel.setBackground(Color.YELLOW)
jLabel.setBounds(60, 60, 54, 20)
JLabel label = new JLabel("密 码:")
label.setOpaque(true)
label.setBackground(Color.CYAN)
label.setBounds(60, 90, 54, 20)
JTextField textField = new JTextField(15)
textField.setBounds(130, 60, 166, 21)
JPasswordField passwordField = new JPasswordField(15)
passwordField.setBounds(130, 90, 166, 21)
JButton jButton = new JButton("登录")
jButton.setBounds(148, 120, 62, 28)
centerPanel.add(jLabel)
centerPanel.add(label)
centerPanel.add(textField)
centerPanel.add(jButton)
centerPanel.add(passwordField)
loginPanel.add(centerPanel)
getContentPane().add(loginPanel)//将初始面板添加到窗口中
setSize(LOGIN_WIDTH, LOGIN_HEIGHT)//设置窗口大小
setLocation(Screen.getCenterPosition(LOGIN_WIDTH, LOGIN_HEIGHT))//设置窗口位置
setDefaultCloseOperation(EXIT_ON_CLOSE)//设置窗口默认关闭方式
setResizable(false)
setVisible(true)
}
public static void main(String[] args) {
new AbsoluteLoginFrame()
}
}
Screen.java
public class Screen {
private int width
private int height
public Screen(){
Toolkit toolkit = Toolkit.getDefaultToolkit()
Dimension screenSize = toolkit.getScreenSize()
this.width = screenSize.width
this.height = screenSize.height
}
public static Point getCenterPosition(int width, int height){
Screen screen = new Screen()
int x = (screen.getWidth() - width) / 2
int y = (screen.getHeight() - height) / 2
return new Point(x, y)
}
public int getWidth() {
return width
}
public void setWidth(int width) {
this.width = width
}
public int getHeight() {
return height
}
public void setHeight(int height) {
this.height = height
}
}
方法一:类 JFrame
javax.swing.JFrame
JFrame中的方法void setDefaultCloseOperation(int)可以设置
以下为改方法的用法:
setDefaultCloseOperation
public void setDefaultCloseOperation(int operation)设置用户在此窗体上发起
"close" 时默认执行的操作。必须指定以下选项之一:
DO_NOTHING_ON_CLOSE(在 WindowConstants 中定义):不执行任何操作;要求程序在已注册的
WindowListener 对象的 windowClosing 方法中处理该操作。
HIDE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册的 WindowListener
对象后自动隐藏该窗体。
DISPOSE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册 WindowListener
的对象后自动隐藏并释放该窗体。
EXIT_ON_CLOSE(在 JFrame 中定义):使用 System exit
方法退出应用程序。仅在应用程序中使用。
默认情况下,该值被设置为 HIDE_ON_CLOSE。更改此属性的值将导致激发属性更改事件,其属性名称为
"defaultCloseOperation"。
注:当 Java 虚拟机 (VM) 中最后一个可显示窗口被释放后,虚拟机可能会终止
要实现你说的,应该采用
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
方法二:
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JFrame
import javax.swing.JOptionPane
public class Test extends JFrame {
public Test(){
this.setTitle("title")
this.setSize(300,200)
this.setLocation(100,100)
//设置关闭时什么也不做
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE)
//监听关闭按钮的点击操作
this.addWindowListener(new WindowAdapter(){
//new 一个WindowAdapter 类 重写windowClosing方法
//WindowAdapter是个适配器类 具体看jdk的帮助文档
public void windowClosing(WindowEvent e) {
//这里写对话框
if(JOptionPane.showConfirmDialog(null,
"退出","提
示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){
System.exit(0)
}
}
})
this.setVisible(true)
}
public static void main(String[] args) {
new Test()
}
}
import java.awt.*import java.awt.event.*
import javax.swing.*
import java.net.*
import java.io.*
public class ClientDemo01 {
public static void main(String[] args){
JFrame f=new JFrame("AA")
JPanel p1=new JPanel()
JPanel p2=new JPanel()
JTextArea ta=new JTextArea(15,30)
ta.setEditable(false) //文本域只读
JScrollPane sp=new JScrollPane(ta) //滚动窗格
JTextField tf=new JTextField(20)
JButton b=new JButton("发送")
p1.add(sp)
p2.add(tf)
p2.add(b)
f.add(p1,"Center")
f.add(p2,"South")
f.setBounds(300,300,360,300)
f.setVisible(true)
f.setResizable(false)
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
Socket socket=null
BufferedInputStream bis=null
BufferedOutputStream bos=null
try{
socket=new Socket("192.168.0.4",5000)
bis=new BufferedInputStream(socket.getInputStream())
bos=new BufferedOutputStream(socket.getOutputStream())
MyThread01 mt=new MyThread01(bis,ta)
mt.start()
}catch(Exception e){
e.printStackTrace()
}
b.addActionListener(new ButtonActionListener01(tf,ta,bos))
}
}
class ButtonActionListener01 implements ActionListener{
JTextField tf
JTextArea ta
BufferedOutputStream bos
public ButtonActionListener01(JTextField tf,JTextArea ta,BufferedOutputStream bos){
this.tf=tf
this.ta=ta
this.bos=bos
}
public void actionPerformed(ActionEvent e){
String message=tf.getText()
if(!message.equals("")){
tf.setText("") //清空文本框
ta.append("AA:"+message+"\n") //添加到文本域并换行
try{
bos.write(message.getBytes())
bos.flush()
}catch(Exception ex){
System.out.println("发送失败")
}
}
}
}
class MyThread01 extends Thread{
BufferedInputStream bis
JTextArea ta
public MyThread01(BufferedInputStream bis,JTextArea ta){
this.bis=bis
this.ta=ta
}
public void run(){
try{
while(true){
byte[] b=new byte[100]
int length=bis.read(b)
String message=new String(b,0,length)
ta.append("BB:"+message+"\n")
}
}catch(Exception e){
e.printStackTrace()
}
}
} import java.awt.*
import java.awt.event.*
import javax.swing.*
import java.net.*
import java.io.*
public class ServerDemo01{
public static void main(String[] args){
JFrame f=new JFrame("BB")
JPanel p1=new JPanel()
JPanel p2=new JPanel()
JTextArea ta=new JTextArea(12,30) //文本域,第一个参数为行数,第二个参数为列数
ta.setEditable(false) //文本域只读
JScrollPane sp=new JScrollPane(ta) //滚动窗格
JTextField tf=new JTextField(20)
JButton b=new JButton("发送")
p1.add(sp)
p2.add(tf)
p2.add(b)
f.add(p1,"Center")
f.add(p2,"South")
f.setBounds(300,300,360,300)
f.setVisible(true)
f.setResizable(false)
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
ServerSocket server=null
Socket socket=null
BufferedInputStream bis=null
BufferedOutputStream bos=null
try{
server=new ServerSocket(5000)
//ta.append("等待AA连接...\n")
socket=server.accept()
//ta.append("AA已连接\n")
bis=new BufferedInputStream(socket.getInputStream())
bos=new BufferedOutputStream(socket.getOutputStream())
MyThread1 mt=new MyThread1(bis,ta)
mt.start()
}catch(Exception e){
e.printStackTrace()
}
b.addActionListener(new ButtonActionListener1(tf,ta,bos))
}
}
class ButtonActionListener1 implements ActionListener{
JTextField tf
JTextArea ta
BufferedOutputStream bos
public ButtonActionListener1(JTextField tf,JTextArea ta,BufferedOutputStream bos){
this.tf=tf
this.ta=ta
this.bos=bos
}
public void actionPerformed(ActionEvent e){
String message=tf.getText() //获取文本框中的内容
if(!message.equals("")){
tf.setText("") //清空文本框
ta.append("BB:"+message+"\n") //添加到文本域并换行
try{
bos.write(message.getBytes())
bos.flush()
}catch(Exception ex){
System.out.println("发送失败!")
}
}
}
}
class MyThread1 extends Thread{
BufferedInputStream bis
JTextArea ta
public MyThread1(BufferedInputStream bis,JTextArea ta){
this.bis=bis
this.ta=ta
}
public void run(){
try{
while(true){
byte[] b=new byte[100]
int length=bis.read(b)
String message=new String(b,0,length)
ta.append("AA:"+message+"\n")
}
}catch(Exception e){
e.printStackTrace()
}
}
}