本篇文章给大家谈谈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怎么点按钮关闭
增加一个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自定义类加载器的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。