【问题标题】:Gui JCombobox Text gets blurredGui JCombobox 文本变得模糊
【发布时间】:2015-01-02 05:46:40
【问题描述】:

最近我开始接触一些 Java 的 Gui 编程。我注意到当我创建一个 JComboBox 并尝试在我的 gui 中显示文本没有完整显示时。很多时候它是模糊的,如下图所示。我尝试增加 GridBagConstraint 的大小,但它仍然会发生。一旦我按下它,按钮也会发生这种情况。

第 1 类:

    public class load {

        private JFrame frame;

        public static void main(String args[]) throws InvocationTargetException,
                InterruptedException {

            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager
                                .getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    // Create the UI here
                    load window = new load();

                    window.frame.setVisible(true);
                }
            });
        }

        private void loadGui() {
            JButton create = new JButton();
            create.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub

                    SelectionView a = new SelectionView();

                    // VVPRIMARY ERROR CURRENTLY VV
                    // unable to setvisible false without the nextframe losing pixel
                    frame.setVisible(false);
                    frame.dispose();


                }

            });

            frame.setSize(400, 400);
            frame.add(create);


        }

    }

第 2 类:

public class SelectionView extends JFrame {

    public SelectionView() {
        // intialize frame
        JFrame selection = new JFrame("Sport Selection");
        JPanel a = new JPanel();

        a.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        selection.setSize(300, 500);

        final JComboBox box = createDropdown();
        JButton load = new JButton("Load");

        load.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                int value = box.getSelectedIndex();

                switch (value) {
                case 0:
                    TableTennisView a = new TableTennisView();
                    break;
                case 1:
                    BasketBallView b = new BasketBallView();
                    break;
                default:
                    System.out.println("Nothing");
                    break;
                }
            }

        });
        // create comboBox


        a.add(box, c);

        a.add(load, c);

        selection.add(a);
        selection.setVisible(true);

    }

    /**
     * Method CreateDropDown
     * 
     * Creates the dropdown menu for the selection view
     * 
     * @return the dropdown menu used in the view
     */
    public JComboBox createDropdown() {
        String[] sport = { "Table Tennis", "BasketBall" };

        JComboBox cb = new JComboBox(sport);

        return cb;
    }
}

【问题讨论】:

    标签: java swing user-interface jcombobox


    【解决方案1】:

    确保您在事件调度线程的上下文中初始化(和更新)UI。

    更多详情请见Initial Threads

    EventQueue.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }
            // Create the UI here
        }
    });
    

    此外,如果可能,在您建立基本 UI 之后,最后在窗口上调用 setVisible。如果 UI 是动态的,则需要在要添加组件的 容器上调用 revalidaterepaint,或者考虑使用 CardLayout

    某些视频驱动程序可能会在某些平台上导致问题,如果问题仍然存在,请考虑提供 runnable example 来证明您的问题。这将减少混乱并获得更好的响应

    【讨论】:

    • 感谢您的提示!据我所知,重新验证或重新绘制正在修复它。我将更多地研究线程,因为我几乎没有进入那个话题。完成后,我打算将所有组件放在 CardLayout 中,并分别关注每个单独的部分,然后再将它们放在一起。
    • Swing 和许多 GUI 框架一样,在它自己的线程中运行并且不是线程安全的,如果你不确保你在 GUI 线程中运行,可能会发生奇怪而奇妙的事情(事件调度线程)
    • 嗯,我尝试添加您提供的行,它可以工作,但仍然缺少像素。我实际上已经制作了一个以前版本的 gui 编程,它以前不可避免地工作过,但现在给我同样的并发错误。此外,我已经更新了我的视频驱动程序,因为这是我一直在使用的。
    • 考虑提供一个runnable example 来证明您的问题。这将导致更少的混乱和更好的响应
    • 据我观察,我认为我的 java 已损坏。我在 Java 应用程序中的整体像素已损坏。
    猜你喜欢
    • 2015-11-11
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多