【问题标题】:Dynamic JOptionPane.showMessageDialog in SwingSwing 中的动态 JOptionPane.showMessageDialog
【发布时间】:2012-11-05 10:29:57
【问题描述】:

创建某种基本消息框并在其上刷新文本的最简单方法是什么?

【问题讨论】:

  • "然后刷新上面的文字?" 你的意思是在选项窗格仍然打开的时候改变它吗?
  • 使用 JLabel 作为 JOptionPane 的“消息”组件

标签: java swing joptionpane


【解决方案1】:

您可以使用 javax.swing.JOptionPane 类创建和显示的最简单的对话框是消息对话框。这可以通过静态方法来完成:showMessageDialog(frame, message, title, type),其中:

  1. “frame”是作为父框架使用的框架对象。
  2. “message”是要在对话框中显示的消息字符串。 3.“title”是要用作对话框标题的标题字符串。
  3. “type”是表示特定消息对话框类型的整数代码。有效类型代码在 JOptionPane 类:INFORMATION_MESSAGE、WARNING_MESSAGE、 ERROR_MESSAGE 和 PLAIN_MESSAGE。

这是一个示例程序

import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JOptionPaneShowMessageDialog implements ActionListener {
   JFrame myFrame = null;
   public static void main(String[] a) {
      (new JOptionPaneShowMessageDialog()).test();
   }
   private void test() {
      myFrame = new JFrame("showMessageDialog Test");
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      myFrame.setBounds(50,50,250,150);
      myFrame.setContentPane(new JDesktopPane());
      JMenuBar myMenuBar = new JMenuBar();
      JMenu myMenu = getDialogMenu();
      myMenuBar.add(myMenu);
      myFrame.setJMenuBar(myMenuBar);
      myFrame.setVisible(true);
   }
   private JMenu getDialogMenu() {
      JMenu myMenu = new JMenu("Dialogs");
      JMenuItem myItem = new JMenuItem("Information");
      myItem.addActionListener(this);
      myMenu.add(myItem);
      myItem = new JMenuItem("Warning");
      myItem.addActionListener(this);
      myMenu.add(myItem);
      myItem = new JMenuItem("Error");
      myItem.addActionListener(this);
      myMenu.add(myItem);
      myItem = new JMenuItem("Plain");
      myItem.addActionListener(this);
      myMenu.add(myItem);
      return myMenu;
   }
   public void actionPerformed(ActionEvent e) {
      String menuText = ((JMenuItem) e.getSource()).getText();
      int messageType = JOptionPane.INFORMATION_MESSAGE;
      if (menuText.equals("Information")) {
         messageType = JOptionPane.INFORMATION_MESSAGE;
      } else if (menuText.equals("Warning")) {
         messageType = JOptionPane.WARNING_MESSAGE;
      } else if (menuText.equals("Error")) {
         messageType = JOptionPane.ERROR_MESSAGE;
      } else if (menuText.equals("Plain")) {
         messageType = JOptionPane.PLAIN_MESSAGE;
      }

      System.out.println("Before displaying the dialog: "+menuText);
      JOptionPane.showMessageDialog(myFrame, 
         "This is message dialog box of type: "+menuText,
         menuText+" Message", messageType);
      System.out.println("After displaying the dialog: "+menuText);
   }
}

【讨论】:

  • 你是什么意思......你问了一个非常模棱两可的问题,然后忽略了澄清请求。如果你让自己难以帮助,那么人们很快就会放弃尝试帮助你,因为他们觉得这很令人沮丧……这是人类的天性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-25
  • 1970-01-01
  • 1970-01-01
  • 2015-12-19
  • 2012-08-03
  • 1970-01-01
相关资源
最近更新 更多