如何组合JLabel和JFrame?

时间:2014-03-16 16:25:13

标签: java swing jpanel jlabel

截至目前,当我运行该程序时,会打开两个不同的面板。一个是JPanel,一个是JFrame。我想知道如何将两者结合起来或者只是将JPLnel上的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

    }

}

2 个答案:

答案 0 :(得分:1)

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

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

应该是:

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

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

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


}

}

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

答案 1 :(得分:1)

您可以扩展JPanel。像这样:

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
  }
}