如何添加JLabel?

时间:2014-03-15 20:33:12

标签: java swing jlabel

我想知道如何在JLabel的右下角添加JFrame?我对编码也很陌生,所以我将把它作为一种学习经验。:)

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

3 个答案:

答案 0 :(得分:2)

我使用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);
    }
}

输出:

enter image description here

有关摇摆开始阅读此tutorials

的详细信息

答案 1 :(得分:0)

这样的事情应该有效。问我你是否有问题:)

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

答案 2 :(得分:0)

这里有一些非常小的例子可以帮助你理解这个:

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