Java 如何组合JLabel和JFrame?

Java 如何组合JLabel和JFrame?,java,swing,jpanel,jlabel,Java,Swing,Jpanel,Jlabel,所以现在当我运行程序时,两个不同的面板打开了。一个是JPanel,一个是JFrame。我想知道如何将两者结合起来,或者只是将JLabel放在JPanel上,然后将它放在我已经拥有的JFrame上 import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax

所以现在当我运行程序时,两个不同的面板打开了。一个是JPanel,一个是JFrame。我想知道如何将两者结合起来,或者只是将JLabel放在JPanel上,然后将它放在我已经拥有的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());
此外,您可能希望将所有代码移动到构造函数中。因此,您的主要功能可能如下所示:

public static void main (String args[]){

     new MainQuestions();
}
public class MainQuestions  {
 public static void main (String args[]){
  JFrame frame=new JFrame();
  YourClass labelPanel = new YourClass();
  frame.setLayout(new BorderLayout());
  frame.add(labelPanel,BorderLayout.SOUTH);
  setVisible(true);
 }

 class YourClass extends JPanel {
  YourClass(){
   //add label there
  }
}
然后在构造函数中,移动所有代码:

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);


}

}

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

您可以扩展JPanel。大概是这样的:

public static void main (String args[]){

     new MainQuestions();
}
public class MainQuestions  {
 public static void main (String args[]){
  JFrame frame=new JFrame();
  YourClass labelPanel = new YourClass();
  frame.setLayout(new BorderLayout());
  frame.add(labelPanel,BorderLayout.SOUTH);
  setVisible(true);
 }

 class YourClass extends JPanel {
  YourClass(){
   //add label there
  }
}

我犯了很多错误。我对编码相当陌生,如果能为我做到这一点,我会很高兴:P@user2997369为我做了什么D坏方法:D@user2997369嗯,stackoverflow不是找人做作业的地方。如果你想学习如何做到这一点,可以在youtube上查找JFrame教程或搜索相关问题。围绕这个话题有很多问题(比如两年前我的问题,大多数问题都是这样的)。你说“一个是JPanel,一个是JFrame”是什么意思?实际上,我在JFrame中看到一个JPanel和一个MessageDialog。请精确你的目标。。。