重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
JAVA彈窗,有下面常見的2種方法實現:
成都創新互聯公司主營四川網站建設的網絡公司,主營網站建設方案,app軟件開發,四川h5小程序開發搭建,四川網站營銷推廣歡迎四川等地區企業咨詢
通過JDialog(模式窗口) 類來實現.里面的寫法類似JFrame
重點方法提示:?setModal(true);
//當設置為true表示,如果不關閉這個彈窗,那么主界面的其他組件都無法操作,該彈窗置于其他窗口的前面
//當設置為false表示,可以繞開本彈窗,對主界面的其他組件進行操作
優點: 功能強大, ?擴展性強
缺點: 代碼量大.
示例圖
通過JOptionPane(提示框) 來實現
效果圖如下
優點: 代碼量少,簡單,方便, 普通場景已經夠用
缺點: 擴展性不夠, 復雜邏輯難以實現.
下面寫一個具體案例
場景:當用于對文本域的文字,進行操作后,那么退出時,提示用戶, 是否要保存已經更改后的內容. ?如果用戶沒有修改內容,那么不用提示
重點代碼
addDocumentListener--用于實現對文本內容發生改變時進行響應
addWindowListener---用于實現對窗口進行操作時進行響應
完整代碼如下
import?java.awt.Font;
import?java.awt.event.*;
import?javax.swing.*;
import?javax.swing.event.*;
public?class?JDDemo?extends?JFrame?implements?DocumentListener,WindowListener{
JTextArea?jta;
boolean?flag;
public?JDDemo()?{
jta?=?new?JTextArea();//文本域
jta.setText("床前明月光");//文本域的文字--可以通過IO加載txt文檔的文字
jta.setFont(new?Font("宋體",Font.BOLD,?20));//文本域的字體
jta.setLineWrap(true);//設置自動換行
jta.getDocument().addDocumentListener(this);//添加文檔變化事件的響應.比如修改,刪除等
JScrollPane?jsp?=?new?JScrollPane(jta);//滾動面板(當文字太多時,顯示滾動條)
add(jsp);
setTitle("主窗口");//標題
setSize(300,?260);//大小
setLocationRelativeTo(null);//居中
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//點擊窗口的關閉按鈕時,執行windowClosing的代碼
addWindowListener(this);
setVisible(true);//窗口可見
}
public?static?void?main(String[]?args)?{
new?JDDemo();
}
//實現WindowListener接口,需要重寫下面的6個方法,?windowClosing專門處理關閉時的方法
public?void?windowOpened(WindowEvent?e)?{
}
public?void?windowClosing(WindowEvent?e)?{
if(flag){
int?n?=?JOptionPane.showConfirmDialog(null,?"已經更改了內容,需要保存后再退出嗎?",?"提示",JOptionPane.YES_NO_OPTION);
//n等于-1表示關閉了彈出的對話框等情況的默認值
//n等于0(JOptionPane.YES_OPTION)表示選擇了Yes
//n等于1(JOptionPane.NO_OPTION)表示選擇了No
if(n==JOptionPane.YES_OPTION){
//把文字保存到文件的代碼省略...
System.out.println("正在使用IO進行保存..ing");
closeFrame();//關閉窗口并退出
}else?if(n==JOptionPane.NO_OPTION){
System.out.println("放棄保存修改.馬上退出");
closeFrame();
}
}else{
closeFrame();
}
}
public?void?windowClosed(WindowEvent?e)?{
}
public?void?windowIconified(WindowEvent?e)?{
}
public?void?windowDeiconified(WindowEvent?e)?{
}
public?void?windowActivated(WindowEvent?e)?{
}
public?void?windowDeactivated(WindowEvent?e)?{
}
//文檔事件,有下面三個,如果觸發其中一個,都可以認為修改了文檔,所以需要在退出時進行提示,是否保存
public?void?insertUpdate(DocumentEvent?e)?{//插入
flag=true;
}
public?void?removeUpdate(DocumentEvent?e)?{//刪除
flag=true;
}
public?void?changedUpdate(DocumentEvent?e)?{//改變
flag=true;
}
//關閉窗口的方法
public?void?closeFrame(){
this.setVisible(false);//窗口不可見
this.dispose();//窗口銷毀
System.exit(0);//JVM虛擬機退出
}
}
運行效果圖:
彈出消息,可以使用 TrayIcon 的 displayMessage() 方法,代碼如下:
import?java.awt.*;
import?java.awt.TrayIcon.MessageType;
import?javax.swing.JButton;
import?javax.swing.JFrame;
public?class?App?extends?JFrame?{
private?static?final?long?serialVersionUID?=?1L;
private?TrayIcon?trayIcon;
public?App()?{
this.setSize(300,?200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new?FlowLayout());
JButton?btnTest?=?new?JButton("彈出消息");
btnTest.addActionListener(e?-?{
//?彈出消息
trayIcon.displayMessage("Tray?Demo",?"Hello?World!",?MessageType.INFO);
});
this.add(btnTest);
if?(SystemTray.isSupported())?{
SystemTray?tray?=?SystemTray.getSystemTray();
Image?image?=?Toolkit.getDefaultToolkit().getImage("images/tray16.png");
this.trayIcon?=?new?TrayIcon(image);
try?{
tray.add(trayIcon);
}?catch?(AWTException?e)?{
e.printStackTrace();
}
}
}
public?static?void?main(String[]?args)?{
new?App().setVisible(true);
}
}
定義一個按鈕的OnClick事件
里面用寫方法調用彈出窗口
代碼
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Frame1 extends JFrame
{
private JButton jButton1=new JButton();
public Frame1 ()
{
try {
jbInit();
}
catch(Exception exception) {
exception.printStackTrace();
}
this.setVisible(true);
}
private void jbInit () throws Exception
{
this.setBounds(300,180,400,300);
getContentPane().setLayout(null);
jButton1.setBounds(new Rectangle(127, 120, 139, 36));
jButton1.setMnemonic('C');
jButton1.setText("點我(C)");
jButton1.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(jButton1);
}
public static void main (String[] args)
{
Frame1 frame1=new Frame1();
}
public void jButton1_actionPerformed (ActionEvent e)
{
this.setVisible(false);
JFrame jf1=new JFrame("子窗口");
jf1.setBounds(100,50,800,600);
jf1.setDefaultCloseOperation(jf1.EXIT_ON_CLOSE);
jf1.setVisible(true);
}
}
代碼缺一行:
。。。
authorTextArea.setPreferredSize(new Dimension(40, 80));
authorFrame.add(authorTextArea);
。。。
以上完了后,需要加一個
authorFrame.setVisible(true);
至于這個框的大小,你再調調哈,相互學習~,三年沒做過了~
可以嘗試嗲用其他語言 比如python python的界面就是電腦本地的界面。
或者java本地調用,eclipse界面就是java本地調用做的,eclipse里面有jar包直接拿來用就可以了