java - 如何结合 JLabel 和 JFrame?

标签 java swing jpanel jlabel

所以到目前为止,当我运行该程序时,会打开两个不同的面板。一种是 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/

相关文章:

java - 如何让ScheduledThreadPool报错?

java - JAXB 生成 nillable=true

java - 如何仅运行一次 Spring Batch 的 read() 函数

java - 尝试填充 JPanel 时图形中出现 NullPointerException

Java GUI 显示为空白

java - 在 For 循环内的 If 语句内继续

java - 使用 Nimbus 和 Java 8U20 时 JList 中的颜色错误

Java代码没有显示错误,但JFrame不会显示

java - 带有可点击链接的 JOptionPane

java - 同一文件中的多个 JPanel 定义