gpt4 book ai didi

java - 如何结合 JLabel 和 JFrame?

转载 作者:行者123 更新时间:2023-11-30 03:58:41 25 4
gpt4 key购买 nike

所以到目前为止,当我运行该程序时,会打开两个不同的面板。一种是 JPanel,一种是 JFrame。我想知道如何将两者结合起来,或者只是将 JPanel 上的 JLabel 放在我已有的 JFrame 上?

import java.awt.BorderLayout;
import java.awt.Dimension;

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;


public class MainQuestions {
public static void main (String args[]){

JFrame frame=new JFrame();
setLayout(new BorderLayout());
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JLabel bottomRtLabel = new JLabel("BOTTOM RIGHT LABEL");
labelPanel.add(bottomRtLabel);
frame.add(labelPanel,BorderLayout.SOUTH);
frame.setVisible(true);

Object ARRAY[]={"French","English","Portugese","Spanish"};

String answer=(String)JOptionPane.showInputDialog(frame, "What language predominately spoken in Latin American countries?","World Geography Review", JOptionPane.PLAIN_MESSAGE, null, ARRAY, null);

if (answer==null)
{
//System.exit(0);
}
else if (answer.equals("Spanish"))
{
JOptionPane.showMessageDialog(null, "Correct!", "World Geography Review", JOptionPane.PLAIN_MESSAGE,null);
//System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null, "Sorry, wrong answer.", "World Geography Review", JOptionPane.PLAIN_MESSAGE,null);
//System.exit(0);
}

}

private static void setLayout(BorderLayout borderLayout) {
// TODO Auto-generated method stub

}

}

最佳答案

我发现你的代码中有很多错误。对于开始:

JFrame frame=new JFrame();
setLayout(new BorderLayout());

应该是:

JFrame frame=new JFrame();
frame.setLayout(new BorderLayout());

此外,您可能希望将所有代码移至构造函数。所以你的 main 可能看起来像这样:

public static void main (String args[]){

new MainQuestions();
}

然后在构造函数内,移动所有代码:

public MainQuestions(){

JFrame frame=new JFrame();
frame.setLayout(new BorderLayout());
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); // Read up on GridBagLayout
JLabel bottomRtLabel = new JLabel("BOTTOM RIGHT LABEL");
labelPanel.add(bottomRtLabel);
frame.add(labelPanel,BorderLayout.SOUTH);
frame.setVisible(true);

String ARRAY[]={"French","English","Portugese","Spanish"}; // Notice how I changed the type to String

String answer=(String)JOptionPane.showInputDialog(frame, "What language predominately spoken in Latin American countries?","World Geography Review", JOptionPane.PLAIN_MESSAGE, null, ARRAY, null);

if (answer==null)
{
//code
}
else if (answer.equals("Spanish"))
{
JOptionPane.showMessageDialog(null, "Correct!", "World Geography Review", JOptionPane.PLAIN_MESSAGE,null);
}
else
{
JOptionPane.showMessageDialog(null, "Sorry, wrong answer.", "World Geography Review", JOptionPane.PLAIN_MESSAGE,null);
}

System.exit(0);


}

}

我还没有运行这个编辑过的代码。通过自己输入并调试来尝试一下。

关于java - 如何结合 JLabel 和 JFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22439479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com