高分求助一个小程序弹出对话框的例子 String s = JOptionPane.showInputDialog(this, "Enter something", "Title", JOptionPane.QUESTION_MESSAGE); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 to lxw_6(我是菜鸟我怕谁?):那就自己寫一個. 谁有例子啊?好象小程序不能直接弹出对话框,需要依赖Frame。谁做过啊?谁有例子啊?给我一个吧,谢谢了。帖在这里或者我的E-Mail:[email protected] fatpig521(瘦驼)说得对,你写的Frame的初始大小没有设。在Frmae的显示前,设定一下大小(setSize,或setBounds,JFrame还可以设setPerformedSize???方法名记不清了) 我给你,几个文件如下:------------------------------------------------------1、package mypjjv;import javax.swing.UIManager;import java.awt.*;public class MApplication { boolean packFrame = false; /**Construct the application*/ public MApplication() { Frame1 frame = new Frame1(); //Validate frames that have preset sizes //Pack frames that have useful preferred size info, e.g. from their layout if (packFrame) { frame.pack(); } else { frame.validate(); } //Center the window Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); frame.setVisible(true); } /**Main method*/ public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { e.printStackTrace(); } new MApplication(); }}--------------------------------------------------2、package mypjjv;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import com.sun.java.swing.*;import com.sun.java.swing.plaf.*;import javax.swing.JComponent.*;import javax.swing.border.*;public class Frame1 extends JFrame { JPanel contentPane; XYLayout xYLayout1 = new XYLayout(); JButton jButton2 = new JButton(); JTextField jTextField3 = new JTextField(); JTextField jTextField2 = new JTextField(); JTextField jTextField1 = new JTextField(); JLabel jLabel4 = new JLabel(); JLabel jLabel3 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel1 = new JLabel(); JButton jButton1 = new JButton(); JLabel jLabel5 = new JLabel(); Border border1; JTextField jTextField4 = new JTextField(); JLabel jLabel6 = new JLabel(); JTextField jTextField5 = new JTextField(); JFileChooser jFileChooser1 = new JFileChooser(); /**Construct the frame*/ public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); border1 = new EtchedBorder(EtchedBorder.RAISED,Color.white,new Color(178, 178, 178)); contentPane.setLayout(xYLayout1); this.setSize(new Dimension(640,480)); this.setTitle("Frame Title"); jButton2.setText("ShowDialog"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton2_actionPerformed(e); } }); jButton2.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { jButton2_mouseClicked(e); } }); jTextField3.setText("jTextField3"); jTextField2.setText("jTextField2"); jTextField1.setText("jTextField1"); jLabel4.setFont(new java.awt.Font("SansSerif", 1, 16)); jLabel4.setToolTipText(""); jLabel4.setText("What You Inputed Information"); jLabel3.setText("Address:"); jLabel2.setText("AGE:"); jLabel1.setText("Name:"); jButton1.setText("ShowFileDialog"); jButton1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { jButton1_mouseClicked(e); } }); jLabel5.setText("You Select File is :"); jTextField4.setText("jTextField4"); jLabel6.setText("EMail:"); jTextField5.setText("jTextField5"); contentPane.add(jLabel4, new XYConstraints(185, 41, 277, -1)); contentPane.add(jButton2, new XYConstraints(139, 305, 121, -1)); contentPane.add(jButton1, new XYConstraints(358, 302, 137, -1)); contentPane.add(jTextField3, new XYConstraints(370, 120, 139, -1)); contentPane.add(jLabel3, new XYConstraints(313, 123, -1, -1)); contentPane.add(jLabel5, new XYConstraints(40, 219, -1, -1)); contentPane.add(jLabel1, new XYConstraints(98, 125, 42, -1)); contentPane.add(jTextField4, new XYConstraints(143, 218, 370, -1)); contentPane.add(jTextField1, new XYConstraints(143, 119, 133, -1)); contentPane.add(jLabel2, new XYConstraints(113, 176, -1, -1)); contentPane.add(jTextField2, new XYConstraints(143, 170, 133, -1)); contentPane.add(jLabel6, new XYConstraints(326, 170, -1, -1)); contentPane.add(jTextField5, new XYConstraints(370, 168, 140, -1)); } /**Overridden so we can exit when window is closed*/ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void jButton1_mouseClicked(MouseEvent e) { //jTextField1.setText("ButtonOne"); /*FileDialog FDialog=new FileDialog(this,"Select File",true); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension dialogSize = FDialog.getSize(); if (dialogSize.height > screenSize.height) { dialogSize.height = screenSize.height; } if (dialogSize.width > screenSize.width) { dialogSize.width = screenSize.width; } FDialog.setLocation((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2); FDialog.show();*/ if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) { jTextField4.setText(jFileChooser1.getSelectedFile().getName()); } } void jButton2_mouseClicked(MouseEvent e) { //int selection=JOptionPane.showConfirmDialog(this,"Message","Title",JOptionPane.OK_CANCEL_OPTION, //JOptionPane.WARNING_MESSAGE); //JOptionPane.showMessageDialog(this,"This is My First Message Showing!","Title",JOptionPane.ERROR_MESSAGE); MyDialog dialog=new MyDialog(this,"",true); dialog.setSize(480,350); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension dialogSize = dialog.getSize(); if (dialogSize.height > screenSize.height) { dialogSize.height = screenSize.height; } if (dialogSize.width > screenSize.width) { dialogSize.width = screenSize.width; } dialog.setLocation((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2); //dialog.show(); ConnectInfo Transfer=new ConnectInfo("Ruanwq","22","Mexin Company","[email protected]"); if (dialog.ShowDialog(Transfer)==true) { jTextField1.setText(Transfer.name); jTextField2.setText(Transfer.age); jTextField3.setText(Transfer.address); jTextField5.setText(Transfer.email); } } void jButton2_actionPerformed(ActionEvent e) { }}----------------------------------------------------------3、package mypjjv;import java.awt.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.awt.event.*;/** * Title: * Description: * Copyright: Copyright (c) 2001 * Company: * @author * @version 1.0 */public class MyDialog extends JDialog { JPanel panel1 = new JPanel(); JLabel jLabel1 = new JLabel(); XYLayout xYLayout1 = new XYLayout(); JTextField jTextField1 = new JTextField(); JLabel jLabel2 = new JLabel(); JTextField jTextField2 = new JTextField(); JLabel jLabel3 = new JLabel(); JTextField jTextField3 = new JTextField(); JLabel jLabel4 = new JLabel(); JPanel jPanel1 = new JPanel(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); private boolean OK; JPanel jPanel2 = new JPanel(); JLabel jLabel5 = new JLabel(); JTextField jTextField4 = new JTextField(); public MyDialog(Frame frame, String title, boolean modal) { super(frame, title, modal); try { jbInit(); pack(); } catch(Exception ex) { ex.printStackTrace(); } } public MyDialog() { this(null, "", false); } void jbInit() throws Exception { panel1.setLayout(xYLayout1); jLabel1.setText("Name:"); jTextField1.setText("jTextField1"); jLabel2.setText("AGE:"); jTextField2.setText("jTextField2"); jLabel3.setText("Address:"); jTextField3.setText("jTextField3"); jLabel4.setFont(new java.awt.Font("SansSerif", 1, 16)); jLabel4.setToolTipText(""); jLabel4.setText("Please Inpute Your Information"); this.setModal(true); this.setTitle(""); jButton1.setText("OK"); jButton1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { jButton1_mouseClicked(e); } }); jButton2.setText("Cancel"); jLabel5.setText("EMail:"); jTextField4.setText("jTextField4"); getContentPane().add(panel1); panel1.add(jLabel4, new XYConstraints(100, 17, -1, -1)); panel1.add(jButton2, new XYConstraints(254, 232, 89, -1)); panel1.add(jButton1, new XYConstraints(122, 232, 77, -1)); panel1.add(jTextField1, new XYConstraints(176, 61, 133, -1)); panel1.add(jTextField3, new XYConstraints(179, 146, 132, -1)); panel1.add(jLabel3, new XYConstraints(122, 149, -1, -1)); panel1.add(jLabel1, new XYConstraints(126, 66, 42, -1)); panel1.add(jLabel2, new XYConstraints(141, 110, -1, -1)); panel1.add(jTextField2, new XYConstraints(176, 105, 133, -1)); panel1.add(jLabel5, new XYConstraints(139, 189, -1, -1)); panel1.add(jTextField4, new XYConstraints(180, 186, 132, -1)); this.getContentPane().add(jPanel1, BorderLayout.SOUTH); this.getContentPane().add(jPanel2, BorderLayout.EAST); } void jButton1_mouseClicked(MouseEvent e) { //System.exit(0); OK=true; setVisible(false); } public boolean ShowDialog(ConnectInfo Transfer) { jTextField1.setText(Transfer.name); jTextField2.setText(Transfer.age); jTextField3.setText(Transfer.address); jTextField4.setText(Transfer.email); show(); if(OK) { Transfer.name=jTextField1.getText(); Transfer.age=jTextField2.getText(); Transfer.address=jTextField3.getText(); Transfer.email=jTextField4.getText(); } return OK; }}-------------------------------------------------------------4、package mypjjv;/** * Title: * Description: * Copyright: Copyright (c) 2001 * Company: * @author * @version 1.0 */public class ConnectInfo { public String name; public String age; public String address; public String email; public ConnectInfo(String n,String a,String ad,String e) { name=n; age=a; address=ad; email=e; }}-----------------------------------------------------OK,我是在JB5下写的! 关于 new String(bytes) 的问题 如何用最快最有效的方法解决这个问题 实践中发现的问题 我用Java做的MSN机器人([email protected])加我有分啊 !!!一个非常奇怪的问题!!!!!!!!!!!!!!!! connection只读是什么概念 广州请进,为MM找工作,无以答谢,赠送劳动力免费开发项目以答谢,诚心 求大神帮我看看这个堆排序代码哪儿出问题了呢 想问问关于protected的问题 初学者,想问一下通过什么来学习Java?(100分,说得好的都有。) 请问各位大虾,不用javaMail如何才能发送附件。 session容量有多大?
那就自己寫一個.
谁做过啊?谁有例子啊?给我一个吧,谢谢了。
帖在这里或者我的E-Mail:[email protected]
在Frmae的显示前,设定一下大小(setSize,或setBounds,JFrame还可以设setPerformedSize???方法名记不清了)
------------------------------------------------------
1、
package mypjjv;import javax.swing.UIManager;
import java.awt.*;public class MApplication {
boolean packFrame = false; /**Construct the application*/
public MApplication() {
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
/**Main method*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new MApplication();
}
}
--------------------------------------------------
2、
package mypjjv;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import com.sun.java.swing.*;
import com.sun.java.swing.plaf.*;
import javax.swing.JComponent.*;
import javax.swing.border.*;
public class Frame1 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JButton jButton2 = new JButton();
JTextField jTextField3 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField1 = new JTextField();
JLabel jLabel4 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel1 = new JLabel();
JButton jButton1 = new JButton();
JLabel jLabel5 = new JLabel();
Border border1;
JTextField jTextField4 = new JTextField();
JLabel jLabel6 = new JLabel();
JTextField jTextField5 = new JTextField();
JFileChooser jFileChooser1 = new JFileChooser(); /**Construct the frame*/
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
border1 = new EtchedBorder(EtchedBorder.RAISED,Color.white,new Color(178, 178, 178));
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(640,480));
this.setTitle("Frame Title");
jButton2.setText("ShowDialog");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jButton2_mouseClicked(e);
}
});
jTextField3.setText("jTextField3");
jTextField2.setText("jTextField2");
jTextField1.setText("jTextField1");
jLabel4.setFont(new java.awt.Font("SansSerif", 1, 16));
jLabel4.setToolTipText("");
jLabel4.setText("What You Inputed Information");
jLabel3.setText("Address:");
jLabel2.setText("AGE:");
jLabel1.setText("Name:");
jButton1.setText("ShowFileDialog");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jButton1_mouseClicked(e);
}
});
jLabel5.setText("You Select File is :");
jTextField4.setText("jTextField4");
jLabel6.setText("EMail:");
jTextField5.setText("jTextField5");
contentPane.add(jLabel4, new XYConstraints(185, 41, 277, -1));
contentPane.add(jButton2, new XYConstraints(139, 305, 121, -1));
contentPane.add(jButton1, new XYConstraints(358, 302, 137, -1));
contentPane.add(jTextField3, new XYConstraints(370, 120, 139, -1));
contentPane.add(jLabel3, new XYConstraints(313, 123, -1, -1));
contentPane.add(jLabel5, new XYConstraints(40, 219, -1, -1));
contentPane.add(jLabel1, new XYConstraints(98, 125, 42, -1));
contentPane.add(jTextField4, new XYConstraints(143, 218, 370, -1));
contentPane.add(jTextField1, new XYConstraints(143, 119, 133, -1));
contentPane.add(jLabel2, new XYConstraints(113, 176, -1, -1));
contentPane.add(jTextField2, new XYConstraints(143, 170, 133, -1));
contentPane.add(jLabel6, new XYConstraints(326, 170, -1, -1));
contentPane.add(jTextField5, new XYConstraints(370, 168, 140, -1));
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} void jButton1_mouseClicked(MouseEvent e) {
//jTextField1.setText("ButtonOne");
/*FileDialog FDialog=new FileDialog(this,"Select File",true); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dialogSize = FDialog.getSize();
if (dialogSize.height > screenSize.height) {
dialogSize.height = screenSize.height;
}
if (dialogSize.width > screenSize.width) {
dialogSize.width = screenSize.width;
}
FDialog.setLocation((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2);
FDialog.show();*/
if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this))
{
jTextField4.setText(jFileChooser1.getSelectedFile().getName());
} } void jButton2_mouseClicked(MouseEvent e) {
//int selection=JOptionPane.showConfirmDialog(this,"Message","Title",JOptionPane.OK_CANCEL_OPTION,
//JOptionPane.WARNING_MESSAGE);
//JOptionPane.showMessageDialog(this,"This is My First Message Showing!","Title",JOptionPane.ERROR_MESSAGE);
MyDialog dialog=new MyDialog(this,"",true);
dialog.setSize(480,350); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dialogSize = dialog.getSize();
if (dialogSize.height > screenSize.height) {
dialogSize.height = screenSize.height;
}
if (dialogSize.width > screenSize.width) {
dialogSize.width = screenSize.width;
}
dialog.setLocation((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2); //dialog.show();
ConnectInfo Transfer=new ConnectInfo("Ruanwq","22","Mexin Company","[email protected]");
if (dialog.ShowDialog(Transfer)==true)
{
jTextField1.setText(Transfer.name);
jTextField2.setText(Transfer.age);
jTextField3.setText(Transfer.address);
jTextField5.setText(Transfer.email); } } void jButton2_actionPerformed(ActionEvent e) {
}
}
----------------------------------------------------------
3、
package mypjjv;import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/public class MyDialog extends JDialog {
JPanel panel1 = new JPanel();
JLabel jLabel1 = new JLabel();
XYLayout xYLayout1 = new XYLayout();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField();
JLabel jLabel3 = new JLabel();
JTextField jTextField3 = new JTextField();
JLabel jLabel4 = new JLabel();
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton(); private boolean OK;
JPanel jPanel2 = new JPanel();
JLabel jLabel5 = new JLabel();
JTextField jTextField4 = new JTextField(); public MyDialog(Frame frame, String title, boolean modal) {
super(frame, title, modal);
try {
jbInit();
pack();
}
catch(Exception ex) {
ex.printStackTrace();
}
} public MyDialog() {
this(null, "", false);
}
void jbInit() throws Exception { panel1.setLayout(xYLayout1);
jLabel1.setText("Name:");
jTextField1.setText("jTextField1");
jLabel2.setText("AGE:");
jTextField2.setText("jTextField2");
jLabel3.setText("Address:");
jTextField3.setText("jTextField3");
jLabel4.setFont(new java.awt.Font("SansSerif", 1, 16));
jLabel4.setToolTipText("");
jLabel4.setText("Please Inpute Your Information");
this.setModal(true);
this.setTitle("");
jButton1.setText("OK");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jButton1_mouseClicked(e);
}
});
jButton2.setText("Cancel");
jLabel5.setText("EMail:");
jTextField4.setText("jTextField4");
getContentPane().add(panel1);
panel1.add(jLabel4, new XYConstraints(100, 17, -1, -1));
panel1.add(jButton2, new XYConstraints(254, 232, 89, -1));
panel1.add(jButton1, new XYConstraints(122, 232, 77, -1));
panel1.add(jTextField1, new XYConstraints(176, 61, 133, -1));
panel1.add(jTextField3, new XYConstraints(179, 146, 132, -1));
panel1.add(jLabel3, new XYConstraints(122, 149, -1, -1));
panel1.add(jLabel1, new XYConstraints(126, 66, 42, -1));
panel1.add(jLabel2, new XYConstraints(141, 110, -1, -1));
panel1.add(jTextField2, new XYConstraints(176, 105, 133, -1));
panel1.add(jLabel5, new XYConstraints(139, 189, -1, -1));
panel1.add(jTextField4, new XYConstraints(180, 186, 132, -1));
this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
this.getContentPane().add(jPanel2, BorderLayout.EAST);
} void jButton1_mouseClicked(MouseEvent e) {
//System.exit(0);
OK=true;
setVisible(false);
}
public boolean ShowDialog(ConnectInfo Transfer)
{
jTextField1.setText(Transfer.name);
jTextField2.setText(Transfer.age);
jTextField3.setText(Transfer.address);
jTextField4.setText(Transfer.email);
show();
if(OK)
{
Transfer.name=jTextField1.getText();
Transfer.age=jTextField2.getText();
Transfer.address=jTextField3.getText();
Transfer.email=jTextField4.getText();
}
return OK;
}
}
-------------------------------------------------------------
4、
package mypjjv;/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/public class ConnectInfo {
public String name;
public String age;
public String address;
public String email;
public ConnectInfo(String n,String a,String ad,String e) {
name=n;
age=a;
address=ad;
email=e;
}
}
-----------------------------------------------------
OK,我是在JB5下写的!