20.对话框
效果:


package com.lvshitech.gui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class JOptionPaneDemo {
public JFrame aFrame;
public JButton aButton;
public JOptionPane anOptionPane;
static JLabel aLabel;
public JOptionPaneDemo() {
aFrame = new JFrame("使用JOptionPane");
Container container = aFrame.getContentPane();
aButton = new JButton("显示对话框");
aLabel = new JLabel("您作出的选择是:", JLabel.CENTER);
aButton.addActionListener(new OptionListener());
anOptionPane = new JOptionPane();
aFrame.setSize(300, 200);
container.add(aButton, BorderLayout.NORTH);
container.add(aLabel, BorderLayout.SOUTH);
aFrame.setVisible(true);
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 点击按钮触发事件
public class OptionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String []choices = {"喜欢", "不喜欢"};
int n = anOptionPane.showOptionDialog(aFrame, "您喜欢音乐吗?", "请选择",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, choices, choices[0]);
if (n == 0) {
JOptionPaneDemo.aLabel.setText("您作出的选择是:" + choices[0] + "音乐");
} else {
JOptionPaneDemo.aLabel.setText("您作出的选择是:" + choices[1] + "音乐");
}
}
}
public static void main(String[] args) {
new JOptionPaneDemo();
}
}

浙公网安备 33010602011771号