查看: 1009|回复: 4
|
Java pop up window......问题。。。。。
[复制链接]
|
|
有谁能够帮小弟。。。我想create一个popup window.
可是怎样试都没有成功。 已经试了6小时。
internet上的exmple看不懂.
i am calling the window from a class to a class.
i am using JFrame. but sometimes it will pop up with empty interface. i want some button and textfield in the pop up window but i am really stuck.
i already try the internal frame still cant get the thing right.
this how i do.
i create a main class (main.java) call the pop up window from another class(new.java).
很赶着要了。。。。我花了很多时间了。。。。
希望各位能帮我下。。谢谢。。。。。 |
|
|
|
|
|
|
|
发表于 24-2-2006 09:15 AM
|
显示全部楼层
我用会你之前的source, 应该是你要的吧? Try to click Add New Record menu......
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class DSAP extends JFrame implements ActionListener{
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenu editMenu;
private JMenuItem addNewRecordMenuItem;
private JMenuItem openRecordMenuItem;
private JMenuItem exitMenuItem;
private JMenuItem updateMenuItem;
private JMenuItem deleteMenuItem;
private JButton pressButton;
public DSAP (){
initComponents();
}
private void initComponents() {
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
addNewRecordMenuItem = new JMenuItem("Add New Record");
addNewRecordMenuItem.addActionListener(this);
openRecordMenuItem = new JMenuItem("Open Record");
exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(this);
fileMenu.add(addNewRecordMenuItem);
fileMenu.add(openRecordMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
editMenu = new JMenu("Edit");
updateMenuItem = new JMenuItem("Update Record");
deleteMenuItem = new JMenuItem("Delete Record");
editMenu.add(updateMenuItem);
editMenu.add(deleteMenuItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
pressButton = new JButton("HELOOOOOOOoo");
getContentPane().setLayout(new BorderLayout(5,10));
getContentPane().add(pressButton,BorderLayout.SOUTH);
this.setTitle("DSAP - Asssignment");
this.setSize(800,600); //x,y
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setJMenuBar(menuBar);
}
public void actionPerformed(ActionEvent e){
String actionCommand = e.getActionCommand();
if(e.getSource() instanceof JMenuItem){
if("Exit".equals(actionCommand)){
System.exit(0);
}else if(actionCommand.endsWith("Add New Record")){
PopupDialog popupDialog = new PopupDialog(this);
popupDialog.setVisible(true);
}
}
}
public static void main(String[] args){
DSAP g = new DSAP();
g.setVisible(true);
}
}
==========================================
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class PopupDialog extends JDialog{
private JTextField passwordText;
private JButton okButton;
public PopupDialog(Frame frame){
super(frame,"Pop Up Dialog");
setModal(true); //set always on top
initComponents();
}
private void initComponents(){
passwordText = new JTextField("SAMPLE TEXT");
okButton = new JButton("Ok");
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(passwordText,BorderLayout.CENTER);
this.getContentPane().add(okButton,BorderLayout.SOUTH);
this.pack();
this.setResizable(false);
this.setSize(new Dimension(350,140));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
close();
}
});
}
private void close(){
dispose();
}
} |
|
|
|
|
|
|
|

楼主 |
发表于 24-2-2006 04:00 PM
|
显示全部楼层
黑木头bro....
你真厉害。。。。
我还有一个问题。。。。
let say i got two class A & B
from class A i call Class B contructor to make a pop up window.
and in the pop up window is a entery form.
eg : B b = new B();
and all the textfield and combo box in the class B i set it to public.
then from class B i call back class A method.
eg A a;
a.method();
when i compile the program. it got no error at all.
but when i run the program. it got error.
eg:
java.lang.NullPointerException
at B.actionPerformed(B.java:94)
我真的给java弄得没有话说了。。。。。
麻烦黑木bro。。。。。。
谢谢!!! |
|
|
|
|
|
|
|
发表于 24-2-2006 07:29 PM
|
显示全部楼层
帮人帮到底。。。Click Add New Record -> Popup Window is shown, then click okButton
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
public class ClassA extends JFrame implements ActionListener{
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenu editMenu;
private JMenuItem addNewRecordMenuItem;
private JMenuItem openRecordMenuItem;
private JMenuItem exitMenuItem;
private JMenuItem updateMenuItem;
private JMenuItem deleteMenuItem;
private JButton pressButton;
public ClassA(){
initComponents();
}
private void initComponents() {
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
addNewRecordMenuItem = new JMenuItem("Add New Record");
addNewRecordMenuItem.addActionListener(this);
openRecordMenuItem = new JMenuItem("Open Record");
exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(this);
fileMenu.add(addNewRecordMenuItem);
fileMenu.add(openRecordMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
editMenu = new JMenu("Edit");
updateMenuItem = new JMenuItem("Update Record");
deleteMenuItem = new JMenuItem("Delete Record");
editMenu.add(updateMenuItem);
editMenu.add(deleteMenuItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
pressButton = new JButton("HELOOOOOOOoo");
getContentPane().setLayout(new BorderLayout(5,10));
getContentPane().add(pressButton,BorderLayout.SOUTH);
this.setTitle("DSAP - Asssignment");
this.setSize(800,600); //x,y
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setJMenuBar(menuBar);
}
public void actionPerformed(ActionEvent e){
String actionCommand = e.getActionCommand();
if(e.getSource() instanceof JMenuItem){
if("Exit".equals(actionCommand)){
System.exit(0);
}else if(actionCommand.endsWith("Add New Record")){
ClassB classB = new ClassB(this);
classB.setVisible(true);
}
}
}
public void aMethod(){
JOptionPane.showMessageDialog(this,"YOU CALLED ME?");
}
public static void main(String[] args){
ClassA g = new ClassA();
g.setVisible(true);
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class ClassB extends JDialog{
private JTextField passwordText;
private JButton okButton;
private ClassA classA;
public ClassB(ClassA aClassA){
super(aClassA,"Pop Up Dialog");
setModal(true); //set always on top
initComponents();
this.classA =aClassA;
}
private void initComponents(){
passwordText = new JTextField("SAMPLE TEXT");
okButton = new JButton("Ok");
okButton.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
handleOkClicked();
}
});
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(passwordText,BorderLayout.CENTER);
this.getContentPane().add(okButton,BorderLayout.SOUTH);
this.pack();
this.setResizable(false);
this.setSize(new Dimension(350,140));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
close();
}
});
}
protected void handleOkClicked() {
classA.aMethod();
}
private void close(){
dispose();
}
}
[ 本帖最后由 黑木头 于 24-2-2006 07:31 PM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 24-2-2006 07:49 PM
|
显示全部楼层
谢谢黑木头bro。。。。。。
你的java真了得。。。。
佩服。。。。。真让我开眼界了。。。。。
谢谢。。大恩大德有机会一定报。。。。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|