java - 使用 JOptionPane 时行之间有很多空间

标签 java joptionpane

我正在使用 JOptionPane。我不明白为什么窗口上的行之间有很大的空间...很抱歉我在 JOptionPane 上发布了很多问题...

有人知道我做错了什么吗?

我应该做什么?

public class ControlPannelWindow extends JFrame{
    public void run(){
        EventQueue.invokeLater
        (
                new Runnable() 
                {
                    @Override
                    public void run() {
                        JPanel panel = new JPanel();
                        JPanel subPanelNbActions = new JPanel();
                        JPanel subPanelNbActions2 = new JPanel();
                        Container c = getContentPane();
                        c.setLayout(new FlowLayout());
                        panel.setLayout(new GridLayout(2,1));//+1 for radioButton, +2 for NB_ACTION
                        try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} 
                        catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){}

                        panel.add(subPanelNbActions);
                        panel.add(subPanelNbActions2);

                        String s = "                                                                                                                                                ";
                        JLabel labelNbAction = new JLabel(s);
                        JLabel labelNbAction2 = new JLabel(s);

                        labelNbAction.setFont(new Font("DigifaceWide Regular", Font.PLAIN, 10));
                        labelNbAction2.setFont(new Font("DigifaceWide Regular", Font.PLAIN, 10));

                        Timer t = new Timer
                                (
                                        500, new ActionListener() //500 -> time in ms -> 0.5 seconds
                                        {
                                            @Override
                                            public void actionPerformed(ActionEvent e) 
                                            {
                                                int nbAction = 5;
                                                labelNbAction.setText("Total action : " + Integer.toString(nbAction));
                                                labelNbAction2.setText("Total action : " + Integer.toString(nbAction+1));
                                            }
                                        }
                                );
                        t.setRepeats(true);
                        t.start();

                        subPanelNbActions.add(labelNbAction);
                        subPanelNbActions2.add(labelNbAction2);
                        JOptionPane.showMessageDialog(null, panel, "What do you want to do ?", JOptionPane.INFORMATION_MESSAGE);
                        t.stop();
                    }
                }
                );
    }
}

最佳答案

您的问题不是 JOptionPane,而是您为标签定义的面板!

您可以给它们添加边框,使其对您可见:

panel.setBorder(BorderFactory.createLineBorder(Color.GREEN));
subPanelNbActions.setBorder(BorderFactory.createLineBorder(Color.RED));
subPanelNbActions2.setBorder(BorderFactory.createLineBorder(Color.RED));
labelNbAction.setBorder(BorderFactory.createLineBorder(Color.BLACK));
labelNbAction2.setBorder(BorderFactory.createLineBorder(Color.BLACK));


您需要设置子面板的布局:

JPanel subPanelNbActions = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
JPanel subPanelNbActions2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));

也许这有帮助,需要进一步的说明: How to remove the padding between in the JPanel still using a flow layout?

关于java - 使用 JOptionPane 时行之间有很多空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35629970/

相关文章:

java - JOptionPane.showInternalMessageDialog 导致运行时错误

java - 从 JOptionPane 输入,在 JFrame 的 JTextArea 中输出?

java - ARRAY 和 JOptionPane 的条件

java - 使用Spring JpaRepository(由hibernate支持),有没有办法根据GrantedAuthority有条件地过滤字段?

java - Jenkins windows ssh slave 访问被拒绝

java - ImapMailReceiver 在只读文件夹上没有存储尝试(失败)[THROTTLED];

java - 找不到符号 println

java - 使用 hibernate @Version 控制进行 DTO 实体映射

java - 如何区分参数和局部变量

Java - 添加到继承的 JOptionPane