简体   繁体   中英

How to combine a JLabel and JFrame?

So as of now when I run the program two different panels open up. One is a JPanel and one is a JFrame. I was wondering how to either combine the two or just take the JLabel on the JPanel and put it on the JFrame I already have?

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

    }

}

I find many errors in your code. For starts:

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

Should be:

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

Also, you might want to move all your code to a Constructor. So your main may look like this:

public static void main (String args[]){

     new MainQuestions();
}

Then inside the constructor, move all your code:

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


}

}

I haven't run this edited code yet. Try it out by typing it yourself and debugging.

You may extend JPanel. Something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM