java自定义jdialog(java自定义类加载器)

本篇文章给大家谈谈java自定义jdialog,以及java自定义类加载器对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

1、java.swing中的JDialog类的问题2、java自动生成的jdialog怎么点按钮关闭3、Java中JDialog如何调整大小,要可行4、有谁熟悉Java JDialog页面布局 帮我完成布局可以给五百分

java.swing中的JDialog类的问题

关于模态对话框和非模态对话框的解释:

模态对话框(Modal Dialogue Box,又叫做模式对话框),是指在用户想要对对话框以外的应用程序进行操作时,必须首先对该对话框进行响应。一般来说,Windows应用程序中,对话框分为模态对话框和非模态对话框两种。二者的区别在于当对话框打开时,是否允许用户进行其他对象的操作。

参考代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

//窗口类

public class MyFrame extends JFrame implements ActionListener {

JButton jb1, jb2;

public MyFrame() {

jb1 = new JButton(“模态对话框”);

jb1.addActionListener(this);

jb2 = new JButton(“非模态对话框”);

jb2.addActionListener(this);

JPanel jp = new JPanel();

jp.add(jb1);

jp.add(jb2);

add(jp, BorderLayout.NORTH);

setTitle(“窗口”);//窗口标题

setSize(300, 200);//窗口大小

setLocationRelativeTo(null);//居中

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//当点击窗口右上角的关闭按钮时,退出虚拟机,结束

}

public static void main(String[] args) {

MyFrame mf = new MyFrame();

mf.setVisible(true);

}

@Override

public void actionPerformed(ActionEvent e) {

JButton jb = (JButton) e.getSource();

//判断按钮是哪一个被点击了

if (jb1 == jb) {

MyDialog md1 = new MyDialog(this, “模态对话框”);

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

md1.setVisible(true);//对话框可见

} else if (jb2 == jb) {

MyDialog md2 = new MyDialog(this, “非模态对话框”);

md2.setVisible(true);

}

}

}

//对话框类

class MyDialog extends JDialog {

public MyDialog(Frame owner, String title) {

super(owner, title);// 调用JDialog父类的构造方法

//JDialog(Dialog owner, String title) 

       // 创建一个具有指定标题和指定所有者的对话框。

JLabel jl = new JLabel(“弹出的窗口”);

add(jl);

setSize(200, 120);//大小

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

setLocationRelativeTo(null);//居中

setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);//当点击对话框右上角的关闭按钮时,销毁该对话框

}

}

效果图

java自定义jdialog(java自定义类加载器)

java自动生成的jdialog怎么点按钮关闭

增加一个close按钮:

button = new JButton(“Close”);

button.addActionListener(this);

add(button);

pack();

setVisible(true);

Java中JDialog如何调整大小,要可行

 JDidalog有一个setBound()方法可以更改对话框的大小,示例如下:

package com.han;  

  

import java.awt.*;  

import java.awt.event.ActionEvent;  

import java.awt.event.ActionListener;  

  

import javax.swing.*;  

  

/** 

 * This program demonstrates the creation of a JDialog from a super-window. 

 * The created dialog is on the mode “Modal”. 

 * @author han 

 * 

 */  

public class SwingJDialog {  

    public SwingJDialog(){  

        final JFrame jf=new JFrame(“弹出窗体实验”);  

        // Some methods defined by Toolkit query the native operating system directly.  

        Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();   

        int Swing1x=500;  

        int Swing1y=300;  

        jf.setBounds(screensize.width/2-Swing1x/2,screensize.height/2-Swing1y/2,Swing1x,Swing1y);  

        jf.setVisible(true);  

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

        Container c=jf.getContentPane();  

        c.setBackground(Color.pink);  

        c.setLayout(null);  

  

        Dimension Swing1size=jf.getSize();  

        JButton jb=new JButton(“弹出对话窗”);  

        int jbx=100;  

        int jby=30;  

        jb.setBounds(Swing1size.width/2-jbx/2,Swing1size.height/2-jby/2,jbx,jby);  

        //jb.setBounds(Swing1x/2-jbx/2,Swing1y/2-jby/2,jbx,jby);  

        c.add(jb);  

  

        class Dialog1 {  

            JDialog jd=new JDialog(jf,”JDialog窗体”,true);  

            Dialog1(){  

  

                jd.setSize(300,200);  

                Container c2=jd.getContentPane();  

                c2.setLayout(null);  

                JLabel jl=new JLabel(“只是一个对话框”);  

                jl.setBounds(0,-20,100,100);  

                JButton jbb=new JButton(“确定”);  

                jbb.setBounds(100,100,60,30);  

                c2.add(jl);  

                c2.add(jbb);  

                jbb.addActionListener(new ActionListener(){  

                    @Override  

                    public void actionPerformed(ActionEvent e) {  

                        jd.dispose();  

                        //System.exit(0);  

                    }  

  

                });  

                System.out.println(“OK”);  

  

                jd.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);  

  

            }  

        }  

  

        jb.addActionListener(new ActionListener(){  

            public void actionPerformed(ActionEvent e){  

                new Dialog1().jd.setVisible(true);//弹出对话框  

                System.out.println(“OK2”);  

            }  

        });  

        System.out.println(“OK3”);  

    }  

  

    public static void main(String[] args){  

        new SwingJDialog();  

    }  

}

有谁熟悉Java JDialog页面布局 帮我完成布局可以给五百分

按照你的要求编写的Java程序如下

VFlowLayout.java(用到的垂直流布局文件)

import java.awt.Component;

import java.awt.Container;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Insets;

public class VFlowLayout extends FlowLayout{

 private static final long serialVersionUID = 1L;

 public static final int TOP = 0;

 public static final int MIDDLE = 1;

 public static final int BOTTOM = 2;

 int hgap;

 int vgap;

 boolean hfill;

 boolean vfill;

 public VFlowLayout(){

  this(TOP, 5, 5, true, false);

 }

 public VFlowLayout(boolean hfill, boolean vfill){

  this(TOP, 5, 5, hfill, vfill);

 }

 public VFlowLayout(int align){

  this(align, 5, 5, true, false);

 }

 public VFlowLayout(int align, boolean hfill, boolean vfill){

  this(align, 5, 5, hfill, vfill);

 }

 public VFlowLayout(int align, int hgap, int vgap, boolean hfill, boolean vfill){

  setAlignment(align);

  this.hgap = hgap;

  this.vgap = vgap;

  this.hfill = hfill;

  this.vfill = vfill;

 }

 public Dimension preferredLayoutSize(Container target){

  Dimension tarsiz = new Dimension(0, 0);

  for (int i = 0; i  target.getComponentCount(); i++){

   Component m = target.getComponent(i);

   if (m.isVisible()){

    Dimension d = m.getPreferredSize();

    tarsiz.width = Math.max(tarsiz.width, d.width);

    if (i  0){

     tarsiz.height += hgap;

    }

    tarsiz.height += d.height;

   }

  }

  Insets insets = target.getInsets();

  tarsiz.width += insets.left + insets.right + hgap * 2;

  tarsiz.height += insets.top + insets.bottom + vgap * 2;

  return tarsiz;

 }

 public Dimension minimumLayoutSize(Container target){

  Dimension tarsiz = new Dimension(0, 0);

  for (int i = 0; i  target.getComponentCount(); i++){

   Component m = target.getComponent(i);

   if (m.isVisible()){

    Dimension d = m.getMinimumSize();

    tarsiz.width = Math.max(tarsiz.width, d.width);

    if (i  0){

     tarsiz.height += vgap;

    }

    tarsiz.height += d.height;

   }

  }

  Insets insets = target.getInsets();

  tarsiz.width += insets.left + insets.right + hgap * 2;

  tarsiz.height += insets.top + insets.bottom + vgap * 2;

  return tarsiz;

 }

 public void setVerticalFill(boolean vfill){

  this.vfill = vfill;

 }

 public boolean getVerticalFill(){

  return vfill;

 }

 public void setHorizontalFill(boolean hfill){

  this.hfill = hfill;

 }

 public boolean getHorizontalFill(){

  return hfill;

 }

 private void placethem(Container target, int x, int y, int width, int height, int first, int last){

  int align = getAlignment();

  if (align == MIDDLE){

   y += height / 2;

  }

  if (align == BOTTOM){

   y += height;

  }

  for (int i = first; i  last; i++){

   Component m = target.getComponent(i);

   Dimension md = m.getSize();

   if (m.isVisible()){

    int px = x + (width – md.width) / 2;

    m.setLocation(px, y);

    y += vgap + md.height;

   }

  }

 }

 public void layoutContainer(Container target){

  Insets insets = target.getInsets();

  int maxheight = target.getSize().height – (insets.top + insets.bottom + vgap * 2);

  int maxwidth = target.getSize().width – (insets.left + insets.right + hgap * 2);

  int numcomp = target.getComponentCount();

  int x = insets.left + hgap, y = 0;

  int colw = 0, start = 0;

  for (int i = 0; i  numcomp; i++){

   Component m = target.getComponent(i);

   if (m.isVisible()){

    Dimension d = m.getPreferredSize();

    if ((this.vfill)  (i == (numcomp – 1))){

     d.height = Math.max((maxheight – y), m.getPreferredSize().height);

    }

    if (this.hfill){

     m.setSize(maxwidth, d.height);

     d.width = maxwidth;

    }else{

     m.setSize(d.width, d.height);

    }

    if (y + d.height  maxheight){

     placethem(target, x, insets.top + vgap, colw, maxheight – y, start, i);

     y = d.height;

     x += hgap + colw;

     colw = d.width;

     start = i;

    }else{

     if (y  0){

      y += vgap;

     }

     y += d.height;

     colw = Math.max(colw, d.width);

    }

   }

  }

  placethem(target, x, insets.top + vgap, colw, maxheight – y, start, numcomp);

 }

}

C.java(主程序)

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Graphics2D;

import java.awt.GridLayout;

import java.awt.Image;

import java.awt.Insets;

import java.awt.Transparency;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.border.TitledBorder;

class MyDialog extends JDialog implements ActionListener{

 JPanel jp=new JPanel();

 TitledBorder tb=new TitledBorder(“预览”);

 JButton []jb=new JButton[9];

 JLabel jl=new JLabel(“单击下方图示或使用按钮可”,JLabel.CENTER);

 JLabel jl2=new JLabel(“应用边框”,JLabel.CENTER);

 JPanel jp1=new JPanel();

 JPanel jp3=new JPanel();

 JPanel jp4=new JPanel();

 JPanel jp5=new JPanel();

 JPanel jp6=new JPanel();

 JPanel jp7=new JPanel();

 JPanel jp8=new JPanel();

 JPanel jp9=new JPanel();

 JPanel jp2=new JPanel();

 JLabel jl1=new JLabel(“应用于(L):”);

 JComboBoxString jcb=new JComboBoxString();

 JButton jb1=new JButton(“选项…”);

 ImageIcon ii1=new ImageIcon(“male.png”);

 ImageIcon ii2=new ImageIcon(“female.png”);

 ImageIcon ii3=new ImageIcon();

 BufferedImage image = new BufferedImage(50,50,BufferedImage.TYPE_INT_RGB);

 MyDialog(){

  setTitle(“预览”);

  Graphics2D g2d = image.createGraphics(); 

  image = g2d.getDeviceConfiguration().createCompatibleImage(50, 50, Transparency.TRANSLUCENT); 

  g2d.dispose(); 

  ii1.setImage(ii1.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));

  ii2.setImage(ii2.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));

  ii3.setImage(image.getScaledInstance(50,50,Image.SCALE_DEFAULT));

  for(int i=0;i9;i++){

   jb[i]=new JButton();

   jb[i].setOpaque(false);  

   jb[i].setContentAreaFilled(false);  

   jb[i].setMargin(new Insets(0, 0, 0, 0));  

   jb[i].setFocusPainted(false);  

   jb[i].setBorderPainted(false);  

   jb[i].setBorder(null); 

   jb[i].addActionListener(this);

  }

  jb[0].setIcon(ii1);jb[1].setIcon(ii1);jb[2].setIcon(ii1);

  jb[4].setIcon(ii1);jb[5].setIcon(ii1);jb[6].setIcon(ii1);

  jb[3].setIcon(ii2);jb[7].setIcon(ii2);jb[8].setIcon(ii3);

  jp3.setLayout(new VFlowLayout(VFlowLayout.MIDDLE));

  jp3.add(jb[0]);jp3.add(jb[1]);jp3.add(jb[2]);

  jp4.setLayout(new BorderLayout());

  jp5.add(jb[4]);jp5.add(jb[5]);jp5.add(jb[6]);

  jp7.add(jb[8]);

  jp4.add(jb[3],BorderLayout.WEST);

  jp4.add(jb[7],BorderLayout.EAST);

  jp4.add(jp5,BorderLayout.CENTER);

  jp8.setLayout(new GridLayout(2,1));

  jp8.add(jl);jp8.add(jl2);

  jp1.setLayout(new BorderLayout());

  jp1.add(jp8,BorderLayout.NORTH);

  jp1.add(jp7,BorderLayout.EAST);

  jp1.add(jp3,BorderLayout.WEST);

  jp1.add(jp6,BorderLayout.CENTER);

  jp1.add(jp4,BorderLayout.SOUTH);

  jcb.addItem(“表格”);

  jp2.setLayout(new GridLayout(3,1));

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

  jp9.add(jb1);

  jp2.add(jl1);jp2.add(jcb);jp2.add(jp9);

  jp.setLayout(new BorderLayout());

  jp.add(jp1,BorderLayout.CENTER);

  jp.add(jp2,BorderLayout.SOUTH);

  jp.setBorder(tb);

  add(jp,BorderLayout.CENTER);

  setSize(500, 500);

  setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

  setLocationRelativeTo(null);

  setModal(true);

  setVisible(true);

 }

 @Override

 public void actionPerformed(ActionEvent e) {

  if(e.getSource()==jb[0]){

   jp6.setBackground(Color.RED);

  }

  if(e.getSource()==jb[1]){

   jp6.setBackground(Color.ORANGE);

  }

  if(e.getSource()==jb[2]){

   jp6.setBackground(Color.YELLOW);

  }

  if(e.getSource()==jb[4]){

   jp6.setBackground(Color.GREEN);

  }

  if(e.getSource()==jb[5]){

   jp6.setBackground(Color.CYAN);

  }

  if(e.getSource()==jb[6]){

   jp6.setBackground(Color.BLUE);

  }

 }

}

public class C extends JFrame implements ActionListener{

 JButton jb=new JButton(“Open Dialog”);

 C(){

  this.setLayout(new FlowLayout());

  this.add(jb);

  jb.addActionListener(this);

  this.setSize(200,200);

  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  this.setLocationRelativeTo(null);

  this.setVisible(true);

 }

 public static void main(String[] args) {

  new C();

 }

 @Override

 public void actionPerformed(ActionEvent e) {

  if(e.getSource()==jb){

   MyDialog md=new MyDialog();

  }

 }

}

运行结果

关于java自定义jdialog和java自定义类加载器的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年4月1日 16:17:52
下一篇 2024年4月1日 16:24:08

相关推荐

  • java多任务,java 多任务

    线程在java编程中的作用 线程同步的真实意思,其实是“排队”:几个线程之间要排队,一个一个对共享资源进行操作,而不是同时进行操作。因此,关于线程同步,需要牢牢记住的第一点是:线程同步就是线程排队。同步就是排队。线程同步的目的就是避免线程“同步”执行。 在Java语言中,不仅语言本身有多线程的支持,可以方便地生成多线程的程序,而且运行环境也利用多线程的应用程…

    2024年5月18日
    3400
  • javasocket心跳实现,java实现心跳机制

    java是否可以实现心跳的程序 1、Timer貌似可以实现,给你个demo看看是不是你想要的,具体的实现就要你自己去查阅api或者去百度博客什么的了。。 2、\x0d\x0a心跳包就是在客户端和服务器间定时通知对方自己状态的一个自己定义的命令字,按照一定的时间间隔发送,类似于心跳,所以叫做心跳包。 3、心跳的也有现成的,不过依赖别的类。大概贴一下,就是这么个…

    2024年5月18日
    5200
  • javaerm是什么意思,java ee什么意思

    企业风险管理(ERM)又是什么啊? ERM(Enterprise Rights Management,企业权限管理)也被称为EDRM(Enterprise Digital Rights Management,企业数字权限管理),主要解决数字化资产(电子数据)的有效管理与控制问题。 企业风险管理(ERM):应用于整个企业的一项流程,旨在:识别潜在的, 一旦发生…

    2024年5月18日
    2900
  • java开发笔试题目,java面试sql笔试题目

    jJava方向综合笔试题1 1、正确答案:A 解析:原型不同于最终系统,它只实现所选择的部分功能,仅是为了试验或是演示而用,部分功能需求可以忽略或者模拟实现,因此适用于需求不确定性高的系统。正确答案:A 解析:本题考查变更控制的相关知识。 2、(1)满足交换律。将运算表中的运算结果作为一个矩阵,可以知道该矩阵是一个对称矩阵,所以满足交换律。或者对每一对元素都…

    2024年5月18日
    3600
  • 哈尔滨java,哈尔滨Java考级

    为什么哈尔滨有好几家Java培训机构都叫甲骨文?如何区分哪家才是真的… 1、数据库部分,基础的sql语句,sql语句调优,索引,数据库引擎,存储过程,触发器,事务等。 前端部分, HTML5 CSS3 JS, HTML DOM Jquery BootStrap等。 2、甲骨文签约有很多公司,不是都有授权的。如果想学,可以让机构提供一下授权书。有很…

    2024年5月18日
    3700
  • java获取前一天年月日,shell获取前一天日期年月日

    java如何得到年月日。 1、两种方法,通过Date类或者通过Calendar类,Date类比较简单,但是要得到细致的字段的话Calendar类比较方便。 2、使用new Date()获取当前日期,new Date().getTime()获取当前毫秒数 计算公式,等于获取的当前日期减去或者加上一天的毫秒数。一天的毫秒数的计算公式:24小时*60分钟*60秒*…

    2024年5月18日
    4100
  • java线程中的线程,java中线程的使用

    java中什么叫做线程?什么叫多线程?多线程的特点是什么? 1、在 Java 中,线程(Thread)是指程序执行的一条路径,是进程中的一个实体。Java 中的线程是轻量级的,可以同时运行多个线程,这就是多线程(Multithreading)。 2、线程又称为轻量级进程,它和进程一样拥有独立的执行控制,由操作系统负责调度,区别在于线程没有独立的存储空间,而是…

    2024年5月18日
    3400
  • java随机数方法,java随机数怎么生成

    关于Java里产生1-6随机数的方法 import java.util.Random 导入Random包 Random random =new Random();int a=random.nextInt(6)+1;nextInt是产生在 0(包括)和指定值(不包括)之间的int值,所以最后加个1,就是产生1到6之间的int值。 余 6 就是 5,再加 1 就…

    2024年5月18日
    4000
  • java天数加一,算天数的时候什么时候加一

    急!!!在JAVA中如何使获取到的时间日加一 1、利用Calendar类,新创建一个Calendar对象,就是当前时间,然后用add方法,添加一个DAY_OF_YEAR类型的1,就可以了。 2、按你的要求,每天都会重置一次,所以,这个得你自己实现。 3、public class TestDate{public static void main(String[…

    2024年5月18日
    3600
  • 包含java.util.base64jar的词条

    java中怎么把list的信息写入到xml 1、有很多种方法,用DOM或SAX, 还有JAXB等。 2、把 StringWriter sWriter = new StringWriter();改成输出一个文件就可以了。 3、就是将LIST中的内容写入到XML文件中。如下就是我整个方法。 4、不知道你做的东西有没有用到别的框架什么的,我知道ajax可以将后台返…

    2024年5月18日
    3700

发表回复

登录后才能评论



关注微信