![](/img/trans.png)
[英]Positioning of Swing Components Java - JPanel, JButton, JFrame
[英]Why is my second JButton not visible on the JFrame in Java swing code?
第二个按钮 (b2) 在 JFrame 上不可见。
所有 3 个组件 l1、l2 和 b2 在 JFrame 上都可见,但 JButton b2 不可见。
public void Joptionpane(int a)
{
l1=new JLabel("Your score is :" + a);
l2=new JLabel("Do you want to continue ?");
b1= new JButton("Yes");
b2= new JButton("NO");
add(l1);
add(l2);
add(b1);
add(b2);
l1.setBounds(150,10,400,50);
l2.setBounds(150,60,400,50);
b1.setBounds(200,130,100,50);
b2.setBounds(300,130,100,50);
我已将布局设置为空
setVisible(true);
setLayout(null);
setTitle("Flappy Bird");
setSize(700,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
从代码来看,似乎是应用程序。 需要这样的东西:
这是一个JOptionPane
,它非常适合该任务,而不是JFrame
,它不是。
代码如下:
String text = "<html><p>Your score is: 42."
+ "<p>Do you want to continue?";
int result = JOptionPane.showConfirmDialog(
null, text, "Flappy Bird", JOptionPane.YES_NO_OPTION);
if (result==JOptionPane.YES_OPTION) {
System.out.println("Continue!");
// TODO! Start another game
} else {
System.out.println("Exit!");
System.exit(0);
}
setVisible(true); setLayout(null);
也许您应该在告诉框架绘制所有内容之前将布局设置为 null ...
正如我们所做的那样:在使面板可见之前设置大小。
对不起,但这一切都没有道理。 你不能在破壳之前在锅里煎鸡蛋,但这就是你正在做的。
setLayout(null); setSize(700,300); setVisible(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.