JButton 只有在我将鼠标悬停在它上面后才会出现

JButton only appears after i hover over it

第二个按钮在我将鼠标悬停在它上面之前是不可见的。

我知道有关于 JPanels 的这个问题的答案,但它们似乎对我不起作用。

我有以下代码:

主要class

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            Window w = new Window();
        });
    }
}

我的习惯Window

public class Window implements ActionListener {

    JFrame f;
    JButton b1, b2;
    JRootPane jRootPane;

    public Window() {
        f = new JFrame("Ceaser Verschluesselung");
        f.setPreferredSize(new Dimension(480, 150));
        f.setResizable(false);
        f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        f.setLocation(720, 300);
        f.setLayout(null);
        jRootPane = f.getRootPane();

        b1 = new JButton("Verschlüsseln");
        b2 = new JButton("Entschlüsseln");
        b1.setSize(new Dimension(220, 100));
        b1.setLocation(7, 5);
        b1.setFont(new Font("1", Font.BOLD, 25));
        b1.addActionListener(this);
        b2.setSize(new Dimension(220, 100));
        b2.setLocation(237, 5);
        b2.setFont(new Font("1", Font.BOLD, 25));
        b2.addActionListener(this);
        jRootPane.add(b1);
        jRootPane.add(b2);

        f.pack();
        f.setVisible(true);
    }

    public void setVisibility(boolean b) {
        f.setVisible(b);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Verschlüsseln")) {
            new Encoder(this);
        } else if (e.getActionCommand().equals("Entschlüsseln")) {
            new Decoder();
        }
    }
}

我之前在其他项目中遇到过这个问题,但是 SwingUtilities.invokeLater() 到 运行 Window 修复了它。在 JPanels 的另一个线程上,我发现按钮在折叠时会消失,但我尝试使用更窄的按钮,但根本没有改变任何东西。

我不添加编码器和解码器 classes,我认为没有必要,直到有人证明我错了 :D

阅读How to Use RootPanes。您不应将组件添加到 RootPane 中。相反,您应该将它们添加到内容窗格:

Container contentPane;
//....
contentPane = f.getContentPane();

整个class:

public class Window implements ActionListener {

    JFrame f;
    JButton b1, b2;
    Container contentPane;

    public Window() {
        f = new JFrame("Ceaser Verschluesselung");
        f.setPreferredSize(new Dimension(480, 150));
        f.setResizable(false);
        f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        f.setLocation(720, 300);
        f.setLayout(null);
        contentPane = f.getContentPane();

        b1 = new JButton("Verschlüsseln");
        b2 = new JButton("Entschlüsseln");
        b1.setSize(new Dimension(220, 100));
        b1.setLocation(7, 5);
        b1.setFont(new Font("1", Font.BOLD, 25));
        b1.addActionListener(this);
        b2.setSize(new Dimension(220, 100));
        b2.setLocation(237, 5);
        b2.setFont(new Font("1", Font.BOLD, 25));
        b2.addActionListener(this);
        contentPane.add(b1);
        contentPane.add(b2);

        f.pack();
        f.setVisible(true);
    }

    public void setVisibility(boolean b) {
        f.setVisible(b);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Verschlüsseln")) {

        } else if (e.getActionCommand().equals("Entschlüsseln")) {
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Window());
    }
}

此外,使用 setLayout(null) 是不好(坏)的做法。请改用 layout managers。这将使您的生活更轻松,而且您的 GUI 将更加用户友好,因为它支持调整大小。