有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java消息框错误,OK按钮字符串被截断

在下面的java消息框示例应用程序中,“OK”按钮显示为三个点(…)。它在JDK 6.0上运行正常。但是这个问题出现在JDK 7.0上。如何解决这个问题

Jave源代码:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ShowDialogBox{
  JFrame frame;
  public static void main(String[] args){
  ShowDialogBox db = new ShowDialogBox();
  }

  public ShowDialogBox(){
  frame = new JFrame("Show Message Dialog");
  JButton button = new JButton("Click Me");
  button.addActionListener(new MyAction());
  frame.setLayout(new FlowLayout());
  frame.add(button);
  frame.setSize(400, 400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public class MyAction implements ActionListener{
  public void actionPerformed(ActionEvent e){
  JOptionPane.showMessageDialog(frame,"Eggs are not supposed to be green.","Inane warning",JOptionPane.WARNING_MESSAGE);
  }
  }
}

共 (2) 个答案

  1. # 1 楼答案

    在JDK 7.0中,它对我来说很好。请确保您的JDK没有损坏

  2. # 2 楼答案

    试试这个:

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class ShowDialogBox {
    
        private JFrame frame;
    
        public static void main(String[] args) {
            ShowDialogBox box = new ShowDialogBox();
        }
    
        public ShowDialogBox() {
            frame = new JFrame("Show Message Dialog");
            JButton button = new JButton("Click Me");
            button.addActionListener(new MyAction());
            frame.setLayout(new FlowLayout());
            frame.add(button);
            frame.setSize(400, 400);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        class MyAction implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent arg0) {
                JOptionPane.showConfirmDialog(frame,
                        "Eggs are not supposed to be green.", "Inane warning",
                        JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
            }
        }
    }