JButton仅在我将鼠标悬停在其上之后出现

问题描述 投票:0回答:1

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

我知道JPanels在此问题上已有答案,但它们似乎对我没有用。

我有以下代码:

Main class

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            Window w = new 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();
        }
    }
}

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

我不添加Encoder和Decoder类,除非有人证明我错了,否则我认为它不是必需的:D

java jframe window awt
1个回答
0
投票

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

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

全班:

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将支持调整大小,因此它对用户更友好。

© www.soinside.com 2019 - 2024. All rights reserved.