1

I was wondering how to add a JLabel to the bottom right corner of this JFrame? I am also fairly new to coding so I will be taking this as a learning experience.:)

public class MainQuestions  {

    public static void main (String args[]){
        JFrame frame=new JFrame();
        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);
        }
    }   
}
Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
user2997369
  • 57
  • 1
  • 6

3 Answers3

2

I made an example for you, using BorderLayout

public class Test {


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI(); 
            }
        });
    }

    private static void createAndShowGUI() {
        JFrame f = new JFrame("Test label");
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JLabel label = new JLabel("Test");
        //this set component orientation to the right!
        label.setHorizontalAlignment(JLabel.TRAILING);
        f.add(label,BorderLayout.SOUTH);
        f.setLocationByPlatform(true);
        f.pack();
        f.setVisible(true);
    }
}

Output:

enter image description here

For more information about swing start reading this tutorials

nachokk
  • 14,363
  • 4
  • 24
  • 53
0

Something like this should work. Ask me if you have problems :)

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);
StreamingBits
  • 137
  • 11
0

here is some pretty small Example which should help you to understand this:

public class Example extends JFrame {

    public Example() {
        setLayout(new BorderLayout());
        add(new JButton("Button"), BorderLayout.PAGE_START);
        add(new JLabel("Label"), BorderLayout.PAGE_END);
        setSize(800, 600);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Example();
    }
}
Klemens Morbe
  • 595
  • 9
  • 24