import javax.swing.JOptionPane;public class Test{
public static void main(String args[]){
optionPane MPane = new optionPane();
MPane.showMessageDialog(null, "xxxxxx");
System.out.println(MPane.getString());
}
}class optionPane extends JOptionPane{
public String getString(){
return "String From JOptionPane";
}
}看明白了吗?
public static void main(String args[]){
optionPane MPane = new optionPane();
MPane.showMessageDialog(null, "xxxxxx");
System.out.println(MPane.getString());
}
}class optionPane extends JOptionPane{
public String getString(){
return "String From JOptionPane";
}
}看明白了吗?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DialogTest extends JDialog
{
private boolean okPressed;
public DialogTest(JFrame owner, String title)
{
super(owner, title, true);
initDialog();
} private void initDialog()
{
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
JButton okButton = new JButton("确定");
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
okPressed = true;
DialogTest.this.dispose();
}
});
JButton cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
okPressed = false;
DialogTest.this.dispose();
}
});
JPanel panel = new JPanel();
panel.add(okButton);
panel.add(cancelButton);
getContentPane().add(panel, BorderLayout.SOUTH);
} public boolean showDialog()
{
okPressed = false;
setVisible(true);
return okPressed;
} public static void main(String[] args)
{
DialogTest dlg = new DialogTest(null, "test");
dlg.setSize(320, 240);
dlg.setLocation(240, 180);
if(dlg.showDialog())
System.out.println("ok pressed");
else
System.out.println("cancel pressed"); System.out.println("dialog closed");
System.exit(0);
}
}