小码虫虫
2022-01-08 09:16
采纳率: 100%
浏览 37
已结题

这个java Windows弹窗代码怎么修改?

初学java,想写一个Windows弹窗
弹窗包含一个事件按钮两个文本显示区
这是别人给的一个弹窗代码,该怎么改啊?

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

public class MainFrame extends JFrame
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private final String CMD_1 = "cmd_1";
    private final String CMD_2 = "cmd_2";
    private final String CMD_3 = "cmd_3";
    
    private ActionListener actionL;
    private Component thisComponent;
    
    
    public MainFrame()
    {
        try
        {
            thisComponent = this;
            this.setTitle("自定义弹窗");
            this.setSize(800, 600);
            Container container = getContentPane();
            container.setLayout(null);
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            
            actionL = new ActionListener() 
            {
                
                @Override
                public void actionPerformed(ActionEvent ae) 
                {
                    try
                    {
                        
                        String cmd = ae.getActionCommand();
                        if (CMD_1.equals(cmd))
                        {
                            JOptionPane.showMessageDialog(null, "提醒一下", "提示", JOptionPane.INFORMATION_MESSAGE);
                        }
                        else if (CMD_2.equals(cmd))
                        {
                            HrUtil.showConfirmDialog(thisComponent, "提醒一下", JOptionPane.INFORMATION_MESSAGE);
                        }
                    }
                    catch(Exception e)
                    {
                        System.out.println(e);
                    }
                }
            };
            
            JButton btn = new JButton("试一下");
            btn.setActionCommand(CMD_1);
            btn.setBounds(10, 10, 100, 30);
            btn.addActionListener(actionL);
            container.add(btn);
            
            btn = new JButton("试一下");
            btn.setActionCommand(CMD_2);
            btn.setBounds(10, 50, 100, 30);
            btn.addActionListener(actionL);            
            container.add(btn);
            
            JTextField tf = new JTextField();
            tf.setBounds(10, 90, 100, 30);
            container.add(tf);
            
            this.setVisible(true);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题