paggy110 2011-02-17 17:56
浏览 536
已采纳

在java中 如何使按钮点击后 弹出一个新的对话框?

package jlabel;

import java.awt.Color;
import javax.swing.JFrame;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.File;
import java.io.InputStream;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculagraph{

public static void main(String [] args){
    JFrame frame = new JFrame("闹钟");
    JTextField time = new JTextField(30);
    JLabel timeLab = new JLabel("定时时间设定(请输入数字) :");
    JTextArea event = new JTextArea(3,10);
    //JTextField event = new JTextField(80);
    JLabel eventLab = new JLabel("事件提醒设定 :");
    JButton but = new JButton("确 定");
    but.setBounds(300,120,80,20);
    String picPath = "D:" + File.separator + "0852193.jpg";
    File file = new File(picPath);
    InputStream input = null;
    byte b[] = new byte[(int)file.length()];
    try{
        input = new FileInputStream(file);
        input.read(b);
        input.close();
    }catch(Exception e){
        e.printStackTrace();

    }

    Icon icon = new ImageIcon(b);
    JLabel lab = null;
    lab = new JLabel("",icon,JLabel.LEFT );
    Font fnt = new Font("Serief",Font.ITALIC + Font.BOLD,28);
    lab.setFont(fnt);
    lab.setBackground(Color.YELLOW);
    lab.setForeground(Color.RED);
    timeLab.setBounds(200,20,170,20);
    eventLab.setBounds(200,50,100,20);
    time.setBounds(380,20,150,25);
    event.setBounds(310,50,220,50);
    frame.add(timeLab);
    frame.add(time);
    frame.add(eventLab);
    frame.add(event);
    frame.add(but);
    frame.add(lab);
    frame.setSize(600,200);
    frame.setBackground(Color.WHITE);
    frame.setLocation(300,200);
    frame.setLocation(300,200);
    frame.setVisible(true);






}

}

               这是我写的代码,可是不知道如何是确定按钮点击后,出现一个新的对话框。还有就是如何在新对话框弹出之后,前一个对话自动关闭? 谢谢高手们!
  • 写回答

1条回答 默认 最新

  • ayling520 2011-02-18 13:02
    关注

    [code="java"]
    import java.awt.Color;
    import javax.swing.JFrame;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.FileInputStream;
    import java.io.File;
    import java.io.InputStream;
    import javax.swing.Icon;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;

    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JTextArea;

    public class TTest
    {
    public static void main(String[] args)
    {

        final JFrame frame = new JFrame("闹钟");
    
    
        JTextField time = new JTextField(30);
        JLabel timeLab = new JLabel("定时时间设定(请输入数字) :");
        JTextArea event = new JTextArea(3, 10);
        // JTextField event = new JTextField(80);
        JLabel eventLab = new JLabel("事件提醒设定 :");
        JButton but = new JButton("确 定");
    
        but.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JFrame jf = new JFrameTest();
                frame.setVisible(false);
                jf.setVisible(true);
            }
        });
        but.setBounds(300, 120, 80, 20);
        String picPath = "D:" + File.separator + "0852193.jpg";
        File file = new File(picPath);
        InputStream input = null;
        byte b[] = new byte[(int) file.length()];
        try
        {
            input = new FileInputStream(file);
            input.read(b);
            input.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
    
        }
    
        Icon icon = new ImageIcon(b);
        JLabel lab = null;
        lab = new JLabel("", JLabel.LEFT);
        Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 28);
        lab.setFont(fnt);
        lab.setBackground(Color.YELLOW);
        lab.setForeground(Color.RED);
        timeLab.setBounds(200, 20, 170, 20);
        eventLab.setBounds(200, 50, 100, 20);
        time.setBounds(380, 20, 150, 25);
        event.setBounds(310, 50, 220, 50);
        frame.add(timeLab);
        frame.add(time);
        frame.add(eventLab);
        frame.add(event);
        frame.add(but);
        frame.add(lab);
        frame.setSize(600, 200);
        frame.setBackground(Color.WHITE);
        frame.setLocation(300, 200);
        frame.setLocation(300, 200);
        frame.setVisible(true);
    
    }
    

    }

    class JFrameTest extends JFrame
    {
    public JFrameTest()
    {
    init();
    }

    public void init()
    {
        this.setSize(300, 300);
        this.setTitle("弹出框");
    

    // this.setVisible(true);
    }
    }
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!