gpt4 book ai didi

Java帮助: JLabel's

转载 作者:行者123 更新时间:2023-12-01 09:29:28 25 4
gpt4 key购买 nike

我创建了这个程序,它显示 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");
}
}

最佳答案

可以通过为 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

for the label in March added it into one because only contained what I format strings with <html> </html> and line with <ul><li>content</li></ul>

关于Java帮助: JLabel's,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39565861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com