佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 985|回复: 4

Java pop up window......问题。。。。。

[复制链接]
发表于 23-2-2006 11:56 PM | 显示全部楼层 |阅读模式
有谁能够帮小弟。。。我想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).

很赶着要了。。。。我花了很多时间了。。。。
希望各位能帮我下。。谢谢。。。。。
回复

使用道具 举报


ADVERTISEMENT

发表于 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真了得。。。。

佩服。。。。。真让我开眼界了。。。。。

谢谢。。大恩大德有机会一定报。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-9-2024 01:36 AM , Processed in 0.123159 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表