快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java代码怎么做弹出框 页面弹出框怎么做

这段java 代码怎么添加动作响应事件,使点击它可以弹出一个窗口,窗口中有一句话

你少了一个菜单项,在菜单项上添加监听器。

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了元宝山免费建站欢迎大家使用!

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.SwingUtilities;

public class TestWin extends JFrame {

{

JMenuBar jmb = new JMenuBar();//创建菜单栏

JMenu jm1 = new JMenu("帮助");//创建菜单

jmb.add(jm1);

JMenuItem jmi = new JMenuItem("帮助");//创建菜单项

jm1.add(jmi);//把菜单项添加到菜单中

jmi.addActionListener(new ActionListener() {//给菜单项添加动作监听器

@Override

public void actionPerformed(ActionEvent e) {

JOptionPane.showMessageDialog(TestWin.this, "窗口中有一句话");//显示消息对话框

}

});

setJMenuBar(jmb);

setTitle("时钟");// 标题

setSize(800, 600);

setLocationRelativeTo(null);// 窗口居中

setDefaultCloseOperation(EXIT_ON_CLOSE);// 窗口点击关闭时,退出程序

setVisible(true);// 窗口可见

}

public static void main(String[] args) {

SwingUtilities.invokeLater(() - new TestWin());

}

}

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 Swing里我想实现点击一个按钮或标签弹出浮动框该怎么做?

Java中的浮动框用Window类来实现,弹出后自己设置Location和Size。

也可以用JDialog来实现(需要自己去掉标题栏和边框)。

怎么用Java swing设置一个有是否按钮的弹框

这是一个简单的Swing弹窗带有Yes/No 按钮,还有一个默认的关闭按钮,

下面的通过代码来简单介绍下弹窗的使用,

import javax.swing.JOptionPane;

public class Demo {

public static void main(String[] args) {

int choose = JOptionPane.showConfirmDialog(null, "5+5=10吗?", "提示", JOptionPane.YES_NO_OPTION); // 返回值为0或1

//由于该对话框可以获取返回值, 所以根据返回值的不同,进行不同的处理

if (choose == JOptionPane.YES_OPTION) {

System.out.println("你选择了Yes");

} else if (choose == JOptionPane.NO_OPTION) {

System.out.println("你选择了NO");

} else if (choose == JOptionPane.DEFAULT_OPTION) {

System.out.println("你直接关闭了对话框,没有做出选择!");

}

}

}

java swing Jpanel 怎么添加一个Jdialog弹出框

通过: 弹出框, 顾名思义就是刚开始看不见, 触发某个事件后弹出的一个框

所以 我们要让JPanel响应指定的事件然后弹出 . 比如常见的事件, 鼠标点击JPanel后弹出

弹出框. 一般有两种方法实现

方法一:JOptionPane 创建1个简单的弹出框.(代码量少, 效果简单)

方法二Dialog/JDialog 创建1个弹出框.(代码量长,可以实现复杂的效果)

效果图

参考代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Demo extends JFrame {

JPanel jp;

static final String title = "Message";

static final String content = "Eggs aren't supposed to be green.";

public Demo() {

jp = new JPanel();

jp.setBackground(Color.PINK);

jp.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent e) {

//方法1

JOptionPane.showMessageDialog(null, content, title, JOptionPane.INFORMATION_MESSAGE);

//方法2

new MyDailog(title, content).setVisible(true);// 创建1个对话框,并且设置为可见

}

});

add(jp);

setTitle("测试Demo");// 标题

setSize(280, 280);// 窗口大小

setLocationRelativeTo(null);// 窗口居中

setDefaultCloseOperation(EXIT_ON_CLOSE);// 窗口点击关闭时,退出程序

setVisible(true);// 窗口可见

}

public static void main(String[] args) {

new Demo();

}

}

// 对话框类

class MyDailog extends JDialog implements ActionListener {

String title;

String content;

public MyDailog(String title, String content) {

this.title = title;

this.content = content;

ImageIcon icon = new ImageIcon("tp.png");// 创建1个图标实例

JLabel jlImg = new JLabel(icon);// 1个图片标签,显示图片

JLabel jl = new JLabel(content);// 1个文字标签,显示文本

jl.setForeground(Color.BLUE);// 设置文字的颜色为蓝色

JButton jb = new JButton("确定");// 创建1个按钮

jb.addActionListener(this);// 给按钮添加响应事件

add(jlImg);// 向对话框加入图片标签

add(jl);// 向对话框加入文字标签

add(jb);// 向对话框添加按钮

setLayout(new FlowLayout());// 对话框流式布局

setIconImage(icon.getImage());// 窗口左上角的小图标

setTitle(title);// 设置标题

setModal(true);// 设置为模态窗口

setSize(275, 135);// 设置对话框大小

setLocationRelativeTo(null);// 对话框局域屏幕中央

setResizable(false);// 对话框不可缩放

setDefaultCloseOperation(DISPOSE_ON_CLOSE);// 当对话框窗口的关闭按钮[X]被点击时,销毁对话框

}

// 当确定按钮被点击时会执行下面的方法

@Override

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equals("确定")) {// 判断是不是确定按钮被点击

this.setVisible(false);// 对话框不可见

this.dispose();// 对话框销毁

}

}

}

java编程:我想实现一个功能:点击下来菜单一个按钮时,弹出一个对话框

//Test.java 主窗体

import java.awt.BorderLayout;

import java.awt.EventQueue;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

public class Test extends JFrame {

private JPanel contentPane;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

Test frame = new Test();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public Test() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

JMenuBar menuBar = new JMenuBar();

setJMenuBar(menuBar);

JMenu mnNewMenu = new JMenu("关于读者");

menuBar.add(mnNewMenu);

JMenuItem mntmNewMenuItem = new JMenuItem("关于作者");

mnNewMenu.add(mntmNewMenuItem);

mntmNewMenuItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

new About_Author().setVisible(true);;

}

});

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

contentPane.setLayout(new BorderLayout(0, 0));

setContentPane(contentPane);

}

}

//About_Author.java 作者页面信息窗体

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.JLabel;

public class About_Author extends JFrame{

private final JPanel contentPanel = new JPanel();

/**

* Launch the application.

*/

public static void main(String[] args) {

try {

About_Author dialog = new About_Author();

dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

dialog.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* Create the dialog.

*/

public About_Author() {

setBounds(100, 100, 450, 300);

getContentPane().setLayout(new BorderLayout());

contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

getContentPane().add(contentPanel, BorderLayout.CENTER);

contentPanel.setLayout(null);

{

JLabel label = new JLabel("QQ");

label.setBounds(160, 10, 54, 15);

contentPanel.add(label);

}

{

JLabel lblNewLabel = new JLabel("手机");

lblNewLabel.setBounds(160, 59, 54, 15);

contentPanel.add(lblNewLabel);

}

JLabel label = new JLabel("作者");

label.setBounds(160, 107, 54, 15);

contentPanel.add(label);

{

JPanel buttonPane = new JPanel();

buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));

getContentPane().add(buttonPane, BorderLayout.SOUTH);

{

JButton okButton = new JButton("OK");

okButton.setActionCommand("OK");

buttonPane.add(okButton);

getRootPane().setDefaultButton(okButton);

}

{

JButton cancelButton = new JButton("Cancel");

cancelButton.setActionCommand("Cancel");

buttonPane.add(cancelButton);

}

}

}

}


分享名称:java代码怎么做弹出框 页面弹出框怎么做
分享路径:http://6mz.cn/article/dosojis.html

其他资讯