圖片看起來很模糊,隱約看到需要一個(gè)登錄窗口,那就分享一下以前練習(xí)的登錄窗口demo吧。
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比恩平網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式恩平網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋恩平地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。
先上效果圖:
登錄界面
源碼如下:
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(){
? //設(shè)置窗口標(biāo)題
? setTitle("登錄界面");
? //設(shè)置一個(gè)初始面板,填充整個(gè)窗口
? JPanel loginPanel = new JPanel();
? //設(shè)置背景顏色
? 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);//設(shè)置窗口大小
? setLocation(Screen.getCenterPosition(LOGIN_WIDTH, LOGIN_HEIGHT));//設(shè)置窗口位置
? setDefaultCloseOperation(EXIT_ON_CLOSE);//設(shè)置窗口默認(rèn)關(guān)閉方式
? 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)可以設(shè)置
以下為改方法的用法:
setDefaultCloseOperation
public void setDefaultCloseOperation(int operation)設(shè)置用戶在此窗體上發(fā)起
"close" 時(shí)默認(rèn)執(zhí)行的操作。必須指定以下選項(xiàng)之一:
DO_NOTHING_ON_CLOSE(在 WindowConstants 中定義):不執(zhí)行任何操作;要求程序在已注冊的
WindowListener 對象的 windowClosing 方法中處理該操作。
HIDE_ON_CLOSE(在 WindowConstants 中定義):調(diào)用任意已注冊的 WindowListener
對象后自動隱藏該窗體。
DISPOSE_ON_CLOSE(在 WindowConstants 中定義):調(diào)用任意已注冊 WindowListener
的對象后自動隱藏并釋放該窗體。
EXIT_ON_CLOSE(在 JFrame 中定義):使用 System exit
方法退出應(yīng)用程序。僅在應(yīng)用程序中使用。
默認(rèn)情況下,該值被設(shè)置為 HIDE_ON_CLOSE。更改此屬性的值將導(dǎo)致激發(fā)屬性更改事件,其屬性名稱為
"defaultCloseOperation"。
注:當(dāng) Java 虛擬機(jī) (VM) 中最后一個(gè)可顯示窗口被釋放后,虛擬機(jī)可能會終止
要實(shí)現(xiàn)你說的,應(yīng)該采用
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);
//設(shè)置關(guān)閉時(shí)什么也不做
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
//監(jiān)聽關(guān)閉按鈕的點(diǎn)擊操作
this.addWindowListener(new WindowAdapter(){
//new 一個(gè)WindowAdapter 類 重寫windowClosing方法
//WindowAdapter是個(gè)適配器類 具體看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();
}
}
package?image;
import?java.awt.BorderLayout;
import?java.awt.Color;
import?java.awt.Dimension;
import?java.awt.GridLayout;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
import?javax.swing.SwingUtilities;
public?class?PanelRunner?extends?JFrame
{
private?static?final?long?serialVersionUID?=?1L;
private?static?void?initPanels?(?JFrame?pr?)
{
for?(?int?i?=?0;?i??5;?i++?)
{
JPanel?panel?=?new?JPanel?();
switch?(i)
{
case?0:
panel.setBackground?(Color.RED);
panel.setPreferredSize?(new?Dimension?(200,?100));
pr.add?(panel,?BorderLayout.NORTH);
break;
case?1:
panel.setBackground?(Color.YELLOW);
panel.setPreferredSize?(new?Dimension?(200,?300));
pr.add?(panel,?BorderLayout.EAST);
break;
case?2:
panel.setBackground?(Color.ORANGE);
panel.setPreferredSize?(new?Dimension?(200,?100));
pr.add?(panel,?BorderLayout.SOUTH);
break;
case?3:
panel.setBackground?(Color.WHITE);
panel.setPreferredSize?(new?Dimension?(200,?300));
pr.add?(panel,?BorderLayout.WEST);
break;
case?4:
pr.add?(panel,?BorderLayout.CENTER);
panel.setPreferredSize?(new?Dimension?(200,?100));
panel.setLayout?(new?GridLayout?(1,?2));
for?(?int?j?=?0;?j??2;?j++?)
{
JPanel?subPanel?=?new?JPanel?();
subPanel.setPreferredSize?(new?Dimension?(200,?100));
Color?color?=?j?==?0???Color.BLUE?:?Color.GREEN;
subPanel.setBackground?(color);
panel.add?(subPanel);
}
break;
default:
break;
}
}
}
private?static?void?initFrame?(?JFrame?pr?)
{
pr.setLayout?(new?BorderLayout?());
pr.setSize?(600,?300);
pr.setLocationRelativeTo?(null);
pr.setResizable?(false);
pr.setDefaultCloseOperation?(JFrame.EXIT_ON_CLOSE);
}
public?static?void?main?(?String[]?args?)
{
SwingUtilities.invokeLater?(new?Runnable?()
{
@Override
public?void?run?()
{
PanelRunner?pr?=?new?PanelRunner?();
initFrame?(pr);
initPanels?(pr);
pr.setVisible?(true);
}
});
}
}