我正在爲我的程序創建一個關於JFrame。我有一個用於該程序的圖標,而且我已將它作爲關於JFrame的第一件事物顯示出來,但我在嘗試將圖像居中時出現問題。如果我做了一些中心調整,就會把所有其他事物的整個對齊關起來。在JFrame中居中放置圖像?
我試圖讓所有的JLabels,除了圖標,左對齊。然後讓圖標對齊中心。
我不得不刪除一些個人信息,無論我刪除了什麼,我把它們放在「[]」之間。
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class About extends JFrame {
public About() {
super("About [PROGRAM]");
setIconImage([PROGRAM].getInstance().setIcon());
JPanel main = new JPanel();
main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
main.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
JLabel icon = new JLabel("", new ImageIcon(getClass().getResource(Constants.ICON_FULL)), JLabel.CENTER);
JLabel name = new JLabel("[PROGRAM]");
JLabel expandedName = new JLabel("[PROGRAM DESCRIPTION]");
JLabel copyright = new JLabel("[COPYRIGHT JUNK]");
JLabel credits = new JLabel("[CREDITS]");
name.setFont(new Font(name.getFont().getFamily(), Font.BOLD, 18));
copyright.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
main.add(icon);
main.add(Box.createRigidArea(new Dimension(0, 10)));
main.add(name);
main.add(expandedName);
main.add(copyright);
main.add(credits);
add(main);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
要在後臺爲中心的圖標?或只是圍繞它的文字居中? – Soronthar
都不是。我想要將圖像和情侶標籤堆疊在一起,但圖像居中。 – samwell