Java帮助:JLabel的

时间:2016-09-19 05:20:21

标签: java

我创建了这个程序,显示了2张照片和一些关于我自己的事实(JLabel)。但是,关于我自己的事实是在页面右侧。我希望事实(JLabels)正好在堆积的图片下面,如标题下的标题和要点,任何帮助?

忽略声音的导入。最终,我希望程序能够发挥作用。

import java.awt.*;
import sun.audio.*;
import javax.swing.*;

public class AudioandImage extends JFrame {
    public ImageIcon image1;
    public JLabel label1;
    public ImageIcon image2;
    public JLabel label2;
    public JLabel name;
    public JLabel facts;
    public JLabel born;
    public JLabel es;
    public JLabel sport;
    public JLabel lastly;

    AudioandImage() {
        setLayout (new FlowLayout());
        image1 = new ImageIcon(getClass().getResource("losangeles.jpg"));

        label1 = new JLabel(image1);
        add(label1);

        image2 = new ImageIcon(getClass().getResource("elsalvador.jpg"));

        label2 = new JLabel(image2);
        add(label2);

        name = new JLabel("My name is Erik Landaverde");
        add(name);
        facts = new JLabel("Some facts about myself:");
        add(facts);
        born = new JLabel("I was born and raised in South Central Los Angeles");
        add(born);
        es =  new JLabel("Have a Salvadorean background");
        add(es);
        sport = new JLabel("My favorite sport is soccer");
        add(sport);
        lastly = new JLabel("Lastly... I am a programmer!");
        add(lastly);
    }

    public static void main(String[] args) {
        AudioandImage gui = new AudioandImage();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        gui.pack();
        gui.setTitle("A Little About Myself");
    }
}

3 个答案:

答案 0 :(得分:1)

可以通过为JFrame指定FlowLayout()BorderLayout setLayout (new BorderLayout()); // asign layout to JFrame add(label1,BorderLayout.PAGE_START); //Add JLabel 1 to Jframe add(label2,BorderLayout.CENTER); //Add JLabel 2 to Jframe name = new JLabel("<html><ul>My name is : Erik Landaverde " + "<li/>Some facts about myself: </li> " + "<li/>I was born and raised in South Central Los Angeles</li>" + "<li/>Have a Salvadorean background</li>" + "<li/>My favorite sport is soccer</li>" + "<li/>Lastly... I am a programmer!</li></ul></html>", SwingConstants.CENTER); add(name,BorderLayout.PAGE_END); //Add JLabel 3 to Jframe 来设置不同的布局,以实现所需的结果,

<html> </html>
  

三月的标签将其添加到一个,因为只包含我的内容   使用<ul><li>content</li></ul>格式化字符串并使用{{1}}

答案 1 :(得分:0)

流布局不适合您想要显示标签的方式。有关流程布局的教程,请参阅https://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html

您还可以从上面的链接中找到BorderLayout的教程。你可以在这里使用这个布局。使用带有1列的GridLayout创建带有标签的JPanel。然后使用BroderLayout将它放在JFrame的CENTER或SOUTH中。

另一种方法可能是将您的文本编写为单个HTML并将其设置在JTextPane等中。

答案 2 :(得分:0)

将以下代码用于GridBagLayout

Test() {
    setLayout(new GridBagLayout());
    image1 = new ImageIcon(getClass().getClassLoader().getResource("image.jpg"));

    label1 = new JLabel(image1);
    add(label1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));

    name = new JLabel("My name is Erik Landaverde");
    add(name, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
    facts = new JLabel("Some facts about myself:");
    add(facts, new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
    born = new JLabel("I was born and raised in South Central Los Angeles");
    add(born, new GridBagConstraints(0, 4, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
    es = new JLabel("Have a Salvadorean background");
    add(es, new GridBagConstraints(0, 5, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
    sport = new JLabel("My favorite sport is soccer");
    add(sport, new GridBagConstraints(0, 6, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
    lastly = new JLabel("Lastly... I am a programmer!");
    add(lastly, new GridBagConstraints(0, 7, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
}

public static void main(String[] args) {
    Test gui = new Test();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setVisible(true);
    gui.pack();
    gui.setTitle("A Little About Myself");
}