繁体   English   中英

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

[英]JButton only appears after i hover over it

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

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

我有以下代码:

主班

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 的另一个线程中,我发现按钮在折叠时会消失,但我尝试使用更窄的按钮,这些按钮根本没有改变任何东西。

我不添加 Encoder 和 Decoder 类,我认为没有必要,直到有人证明我错了:D

阅读如何使用 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)是不好的(坏的)做法。 改用布局管理器 这将使您的生活更轻松,而且您的 GUI 将更加用户友好,因为它支持调整大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM