java - 我不能点击这个 JTextField 来编辑它,为什么不呢?

标签 java swing java-7 jtextfield declarative-services

更新

只有在 OS X 上运行时才会发生这种情况。Windows 上不存在此问题。我正在使用 Eclipse Kepler 和 Oracle 1.7.0_45.jdk


下面的代码是从 OSGI 声明式服务组件运行的基本 GUI。问题是当 GUI 启动时,我无法与 JTextField loginField 交互。我可以与 JButton loginButton 进行交互。如果我运行与 Java 应用程序相同的代码,我可以交互/编辑 JTextField。所以这个问题一定是由于它是作为 OSGi 包启动的。任何人都可以阐明问题所在吗?

医生设备.java

package ie.ucd.sh.doctor;

import org.osgi.service.component.ComponentContext;
import ie.ucd.sh.db.context.DbContext;
import java.awt.EventQueue;

/**
 * This class runs the GUI for the Doctor Device.  It connects to the database using the abstraction
 * provided by the ie.ucd.sh.db.context bundle and displays information on the doctor that has logged in
 * into the device.
 * @author Conor Griffin
 */
public class DoctorDevice {

    protected DbContext dbContext;
    DoctorGUI window;

    public void activate(ComponentContext ctxt) {

        /**
         * Launch the swing GUI and set its DbContext
         */
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    window = new DoctorGUI();
                    window.frame.setVisible(true);
                    window.dbContext = dbContext;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    protected void deactivate(ComponentContext ctxt) {
        window.terminate();
    }

    public DbContext getContext() {
        return dbContext;
    }

    public void setContext(DbContext dbContext) {
        this.dbContext = dbContext;
        System.out.println("DbContext was set to " + dbContext);
    }

}

DoctorGUI.java

package ie.ucd.sh.doctor;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import java.awt.GridLayout;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import ie.ucd.sh.db.context.DbContext;
import ie.ucd.sh.db.context.Doctor;
import ie.ucd.sh.db.context.Entity.Type;
import ie.ucd.sh.db.context.Nurse;
import ie.ucd.sh.db.context.Patient;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTabbedPane;

public class DoctorGUI {

    protected JFrame frame;
    protected DbContext dbContext;
    private static DateFormat df = new SimpleDateFormat("HH:mm:ss");
    private Doctor currentUser;
    private JTextField loginField;

    /**
     * Create the application.
     */
    public DoctorGUI() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    public void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridLayout(0, 2, 0, 0));

        JPanel inputPanel = new JPanel();
        frame.getContentPane().add(inputPanel);
        inputPanel.setLayout(null);

        JButton loginButton = new JButton("Login");
        loginButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                currentUser = login();
            }
        });
        loginButton.setBounds(137, 6, 82, 29);
        inputPanel.add(loginButton);

        loginField = new JTextField();
        loginField.setBounds(6, 5, 134, 28);
        inputPanel.add(loginField);
        loginField.setToolTipText("Enter your ID");
        loginField.setColumns(10);
        loginField.setEditable(true);

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        frame.getContentPane().add(tabbedPane);
        loginField.requestFocusInWindow();
    }


    /**
     * Login the doctor
     * @return
     */
    private Doctor login() {
        int id = 0;
        Doctor doctor = null;
        try {
            id = Integer.parseInt(loginField.getText());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            String title = "Invalid Value";
            String message = "ID must be numeric";
            JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
            return doctor;
        }

        doctor = (Doctor)dbContext.get(Type.Doctor, id);
        System.out.println(doctor.getFirstName() + " " + doctor.getLastName());

        return doctor;
    }

    /**
     * Set the JFrame to invisible and throw it away
     */
    public void terminate() {
        frame.setVisible(false);
        frame.dispose();
    }
}

DbContextClient.xml - OSGi 组件定义

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.2.0" activate="activate" deactivate="deactivate" name="DbContextClient">
   <implementation class="ie.ucd.sh.doctor.DoctorDevice"/>
   <reference bind="setContext" cardinality="1..1" interface="ie.ucd.sh.db.context.DbContext" name="DbContext" policy="static" unbind="getContext"/>
</scr:component>

最佳答案

经过数小时研究添加到运行配置参数的此参数解决了我的问题:-noSplash

我的 eclipse 设置:enter image description here

关于java - 我不能点击这个 JTextField 来编辑它,为什么不呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20151166/

相关文章:

java - 如何使用java删除或删除Excel工作表中的空行

java - Java 8中 map 收集器的合并功能添加到列表中

java - 使用 primefaces 上传文件时出现编码问题

java - 将数据从一个非常大的文本文件导入到 JTable

java - 在 Java 中创建自定义 JButton

java - 尝试序列化包含数组的 Avro GenericRecord 时出现 NullPointerException

java - Swing BorderLayout 不堆叠

java - 如何在 Java 中格式化非纯日期?

java - 文件.createDirectory() : FileAlreadyExistsException

android - Android 是否计划支持 Java7?