[英]JButtons Only Working When Mouse Hovers Over
I am a beginner in Java and programming overall, and I started by making a simple JFrame
with a few buttons. 我是Java和整体编程的初学者,我首先制作了一个带有几个按钮的简单JFrame
。 Now when I created the buttons, the only way they would appear is if I hovered over them with my mouse. 现在,当我创建按钮时,它们出现的唯一方式是如果我将鼠标悬停在它们上方。
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame {
public Main() {
setTitle("Test");
setSize(600, 500);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
JPanel p = new JPanel();
JButton b = new JButton("Derp");
p.add(b);
add(b);
setVisible(true);
revalidate();
repaint();
}
public static void main(String[] args) {
new Main();
}
public void paint(Graphics g) {
g.drawString("Under Construction...", 240, 250);
}
}
Help would be appreciated, also please note I do not know that much about Java and I don't know a descent amount of the languages. 我们将不胜感激,也请注意,我对Java的了解不多,也不熟悉各种语言。 Also, a way to make the buttons have the Aero texture instead of the default one would be nice :) 另外,一种使按钮具有Aero纹理而不是默认纹理的方法会很不错:)
Since you override paint
you need to call super.paint(g);
由于您覆盖了paint
,因此需要调用super.paint(g);
as the first line. 作为第一行。
public void paint(Graphics g) {
super.paint(g); // <-- add this.
g.drawString("Under Construction...", 240, 250);
}
Also, you should move your logic off the main()
thread; 另外,您应该将逻辑移出main()
线程; like 喜欢
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Main();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.