package work;
//完成
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class jOptionPane extends JFrame implements ActionListener{
JOptionPane jop;
JButton jbt = new JButton("选择颜色");
public jOptionPane()
{
super("MyoptionPane");
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
this.getContentPane().add(jbt);
jbt.addActionListener(this);
jop = new JOptionPane();
int n ;
n = JOptionPane.showConfirmDialog(null, "是否要运行程序","询问",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
if(n == JOptionPane.OK_OPTION )
{
this.setVisible(true);
}
else
System.exit(0);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new jOptionPane();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Color color = JColorChooser.showDialog(jop, "颜色" ,Color.GRAY);
this.getContentPane().setBackground(color);
}
}
运行展示
- 开始运行
- 点击确定后
3. 随便选择一个颜色