有 Java 编程相关的问题?

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

java Gui JCombobox文本变得模糊

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

enter image description here

第一类:

    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);


        }

    }

第二类:

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) 个答案

  1. # 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是动态的,则需要在<>强>父< /强>容器中调用^ {CD2>}和^ {CD3>},以添加您的组件或考虑使用^ {CD4>}/P>

    一些视频驱动程序会在某些平台上引起问题,如果问题持续存在,请考虑提供一个{a2}来演示您的问题。这将减少混乱和更好的响应