Gui JCombobox文字变得模糊

时间:2015-01-02 05:46:40

标签: java swing user-interface jcombobox

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

enter image description here

第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;
    }
}

1 个答案:

答案 0 :(得分: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。如果用户界面是动态的,您需要在要添加组件的容器上调用revalidaterepaint,或者考虑使用CardLayout < / p>

某些视频驱动程序可能会在某些平台上出现问题,如果问题仍然存在,请考虑提供可显示问题的runnable example。这将减少混淆和更好的响应