java弹窗成功代码,编程弹窗代码

JAVA的弹窗怎么做?

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虚拟机退出

}

}

运行效果图:

java的SystemTray跳出提示的代码?就像xp的网络连接那样,连接成功会弹出一个网络已连接

弹出消息,可以使用 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);

}

}

java弹出新窗口

定义一个按钮的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);

}

}

java点击菜单项弹出窗口怎么弄

代码缺一行:

。。。

authorTextArea.setPreferredSize(new Dimension(40, 80));

authorFrame.add(authorTextArea);

。。。

以上完了后,需要加一个

authorFrame.setVisible(true);

至于这个框的大小,你再调调哈,相互学习~,三年没做过了~

java业务类执行成功之后,想要弹出对话框提示:操作成功。如何写?

可以尝试嗲用其他语言 比如python python的界面就是电脑本地的界面。

或者java本地调用,eclipse界面就是java本地调用做的,eclipse里面有jar包直接拿来用就可以了


分享标题:java弹窗成功代码,编程弹窗代码
网页路径:http://scjbc.cn/article/hdpgoe.html

其他资讯