【发布时间】:2020-06-29 17:38:56
【问题描述】:
第二个按钮在我将鼠标悬停在它上面之前是不可见的。
我知道 JPanel 已经有关于这个问题的答案,但它们似乎对我不起作用。
我有以下代码:
主类
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 的另一个线程上,我发现按钮折叠时会消失,但我尝试使用更窄的按钮,但根本没有改变任何东西。
我没有添加编码器和解码器类,我认为没有必要,直到有人证明我错了:D
【问题讨论】:
标签: java swing jframe window awt