对话框中的文本丢失 [英] The text in dialog box is missing

查看:149
本文介绍了对话框中的文本丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java程序。我使用Eclipse作为我的工具。另外我已经安装了Java 7 Update 51,Java SE Development Kit 7 Update 51.我的代码打开选择文件的对话框。它的工作原理,但问题是对话框上的按钮或文本框上的文本缺少某个时间。



有人会告诉我如何解决这个问题。感谢提前



有我的代码:

  package MyPackage; 

import java.awt.event。*;
import javax.swing。*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser。*;
import java.io. *;

public class MainForm扩展JFrame实现ActionListener {

/ **
* @param args
* /

public static void main(String [] args){
// TODO自动生成的方法存根
new MainForm();

}

public MainForm(){
super(Example);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

//命名JMenu&添加项
JMenu menu = new JMenu(File);
menu.add(makeMenuItem(Open));
menu.add(makeMenuItem(Save));
menu.add(makeMenuItem(Quit));

//添加JMenu bar
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
setJMenuBar(menuBar);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
// setSize(600,300);
// setLocation(200,200);
setVisible(true);
}

public void actionPerformed(ActionEvent e){

//菜单项操作
String command = e.getActionCommand();

if(command.equals(Quit)){
System.exit(0);
} else if(command.equals(Open)){
//打开菜单项动作
JFileChooser fileChooser = new JFileChooser();
int returnVal = fileChooser.showOpenDialog(MainForm.this);
if(returnVal == fileChooser.APPROVE_OPTION){
文件文件= fileChooser.getSelectedFile();

//加载文件
} else if(returnVal == JFileChooser.CANCEL_OPTION){
// do something else
}
}


else if(command.equals(Save)){
//保存菜单项动作
System.out.println(保存菜单项点击);
}
}

私有JMenuItem makeMenuItem(String name){
JMenuItem m = new JMenuItem(name);
m.addActionListener(this);
return m;
}


}


解决方案

您的代码中的问题是正在执行哪个线程。大多数Swing方法只能在一个非常具体的线程中执行,这个线程叫做事件调度线程。因为您没有正确地执行此操作,您的应用程序将会出现不一致的错误。越大越容易出现问题。



要在EDT中正确执行,您需要将主要方法更改为:

  public static void main(String [] args)
{
SwingUtilities.invokeLater(new Runnable(){
@覆盖
public void run()
{
new MainForm();
}
});
}

invokeLater 执行MainForm构造函数到EDT,以便GUI初始化代码在正确的线程中执行。



我不知道这是否会解决问题,肯定会解决未来不可重复的错误行为。我曾经使用 JFileChooser 亲自看到一些线程问题,有或者已经有几个报告的这个类的线程管理的错误。



请注意,这是Swing的核心规则,即使是 Hello world 符合。


Hi I have a java program. I use Eclipse as my tool. Also I have installed Java 7 Update 51, Java SE Development Kit 7 Update 51. My code to open the dialog box for selecting the file. It works, but the problem is the text on button or textbox on dialog box is missing sometime.

Would some one tell me how to solve this issue. Thanks in advance

There is my code:

package MyPackage;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;
import java.io.*;

public class MainForm extends JFrame implements ActionListener  {

/**
 * @param args
 */

public static void main(String[] args) {
    // TODO Auto-generated method stub
    new MainForm();

}

public MainForm(){
    super("Example");
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    // Name the JMenu & Add Items
    JMenu menu = new JMenu("File");
    menu.add(makeMenuItem("Open"));
    menu.add(makeMenuItem("Save"));
    menu.add(makeMenuItem("Quit"));

    // Add JMenu bar
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    setJMenuBar(menuBar);
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    //setSize(600, 300);
    //setLocation(200, 200);
    setVisible(true);
}

public void actionPerformed(ActionEvent e) {

    // Menu item actions
    String command = e.getActionCommand();

    if (command.equals("Quit")) {
        System.exit(0);
    } else if (command.equals("Open")) {
        // Open menu item action
        JFileChooser fileChooser = new JFileChooser();     
        int returnVal = fileChooser.showOpenDialog(MainForm.this);
        if (returnVal ==  fileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();

            // Load file
        } else if (returnVal == JFileChooser.CANCEL_OPTION ) {
            // Do something else
        } 
    } 


     else if (command.equals("Save")) {
        // Save menu item action
        System.out.println("Save menu item clicked");
    }
}

private JMenuItem makeMenuItem(String name) {
    JMenuItem m = new JMenuItem(name);
    m.addActionListener(this);
    return m;
}


}

解决方案

A problem in your code is in which thread is being executed. Most Swing methods can only be executed in a very specific thread called Event Dispatch Thread. Because you are not doing this correctly your application will tend to have inconsistent errors. The larger it gets the easier it is that something goes wrong.

To properly execute in the EDT you need to change your main method to:

public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run()
        {
            new MainForm();
        }
    });
}

invokeLater schedules the execution of the MainForm constructor to the EDT, so that the GUI initialization code is executed in the proper thread.

I don't know if this alone will solve the problem but will surely solve future non-reproducible missbehaviours. I've personally seen some threading problems other times with JFileChooser, there are or have been several reported bugs with the thread management of this class.

Note that this is a core rule of Swing that even the Hello world complies.

这篇关于对话框中的文本丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆