java 弹出窗口编程

//default title and icon
            JOptionPane.showMessageDialog(frame,
            "Eggs are not supposed to be green.",
            "Message");
            
	
//custom title, warning icon
            JOptionPane.showMessageDialog(frame,
            "Eggs are not supposed to be green.",
            "Inane warning",
            JOptionPane.WARNING_MESSAGE);
            
	
//custom title, error icon
            JOptionPane.showMessageDialog(frame,
            "Eggs are not supposed to be green.",
            "Inane error",
            JOptionPane.ERROR_MESSAGE);
            
	
//custom title, no icon
            JOptionPane.showMessageDialog(frame,
            "Eggs are not supposed to be green.",
            "A plain message",
            JOptionPane.PLAIN_MESSAGE);
            
	
//custom title, custom icon
            JOptionPane.showMessageDialog(frame,
            "Eggs are not supposed to be green.",
            "Inane custom dialog",
            JOptionPane.INFORMATION_MESSAGE,
            icon);
            

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java代码实现。注释中有详细的解释。 ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener { private JLabel numberLabel, resultLabel; // 输入数字和输结果的标签 private JTextField numberField, resultField; // 输入数字和输结果的文本框 private JRadioButton squareButton, factorialButton; // 选择平方和阶乘的单选按钮 private JButton calculateButton; // 计算按钮 public Calculator() { // 设置窗口标题 setTitle("Calculator"); // 设置布局为网格布局 setLayout(new GridLayout(4, 2)); // 添加输入数字和输结果的标签和文本框 numberLabel = new JLabel("Number:"); add(numberLabel); numberField = new JTextField(); add(numberField); resultLabel = new JLabel("Result:"); add(resultLabel); resultField = new JTextField(); resultField.setEditable(false); // 输结果的文本框设置为只读 add(resultField); // 添加选择平方和阶乘的单选按钮 squareButton = new JRadioButton("Square"); add(squareButton); factorialButton = new JRadioButton("Factorial"); add(factorialButton); // 添加计算按钮 calculateButton = new JButton("Calculate"); calculateButton.addActionListener(this); // 计算按钮添加监听器 add(calculateButton); // 将平方和阶乘的单选按钮放在同一个按钮组中,保证只有一个被选中 ButtonGroup group = new ButtonGroup(); group.add(squareButton); group.add(factorialButton); // 设置窗口大小和可见性 setSize(300, 150); setVisible(true); } // 实现 ActionListener 接口的 actionPerformed 方法 public void actionPerformed(ActionEvent e) { // 获取输入的数字 int number = Integer.parseInt(numberField.getText()); // 判断选择的是平方还是阶乘 if (squareButton.isSelected()) { // 计算平方 int result = number * number; resultField.setText(Integer.toString(result)); // 将计算结果设置到输结果的文本框中 } else if (factorialButton.isSelected()) { // 计算阶乘 int result = 1; for (int i = 1; i <= number; i++) { result *= i; } resultField.setText(Integer.toString(result)); // 将计算结果设置到输结果的文本框中 } } public static void main(String[] args) { new Calculator(); } } ``` 运行程序,会一个窗口,用户可以在窗口中输入一个整数,然后通过单选按钮选择计算平方还是阶乘,点击计算按钮后会在窗口中输计算结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值