[英]GridBagLayout not aligning images properly
最初我有一個甲板圖像和文本“甲板”正好在圖像下方看起來很好
public class GuiTut extends JPanel {
private GridBagConstraints c = new GridBagConstraints();
private JLabel deckLabel = new JLabel();
public GuiTut() {
setLayout(new GridBagLayout());
try {
deck = ImageIO.read(new File("resources/images/deck.jpg"));
} catch (Exception e) {}
c.gridx = 0;
c.gridy = 0;
JLabel deckPic = new JLabel(new ImageIcon(deck));
add(deckPic, c);
deckLabel.setText("Deck");
c.gridx = 0;
c.gridy = 1;
add(deckLabel, c);
}
但是在我添加gridLayout
面板之后,我的整個GUI設計都搞砸了。 正如您所看到的,我的deck圖像與gridLayout的第一行沒有正確對齊,而我的文本“deck”已被幾個寬闊的空間分隔開。
public class GuiTut extends JPanel {
private GridBagConstraints c = new GridBagConstraints();
private JLabel deckLabel = new JLabel();
private JPanel gridLayoutPanel = new JPanel(new GridLayout(2, 0));
private JLabel[] label = new JLabel[14];
public GuiTut() {
setLayout(new GridBagLayout());
try {
card = ImageIO.read(new File("resources/images/card.jpg"));
} catch (Exception e) {}
for(int i = 0; i < 14; i++) {
label[i] = new JLabel(new ImageIcon(card);
gridLayoutPanel.add(label[i]);
}
try {
deck = ImageIO.read(new File("resources/images/deck.jpg"));
} catch (Exception e) {}
c.gridx = 0;
c.gridy = 0;
JLabel deckPic = new JLabel(new ImageIcon(deck));
add(deckPic, c);
deckLabel.setText("Deck");
c.gridx = 0;
c.gridy = 1;
add(deckLabel, c);
c.gridx = 1;
c.gridy = 0;
add(gridLayoutPanel, c);
}
我想要的是甲板圖像與gridLayout
的第一行對齊,文本"deck"
恰好在甲板圖像的正下方,但我似乎無法得到它。
熱鏈接圖像
對於卡座標簽,您只需設置標簽文本並設置垂直/水平文本點
JLabel label = new JLabel(new ImageIcon(new URL(DECK_URL))); label.setText("DECK"); label.setHorizontalTextPosition(JLabel.CENTER); label.setVerticalTextPosition(JLabel.BOTTOM);
對於卡座定位問題,您可以為外部面板使用不同的布局管理器(如BorderLayout),以及卡座面板的默認流布局。 這些卡仍然是GridLayout。 在需要時嵌套面板通常很有幫助
例
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GuiTest {
private static final String DECK_URL = "http://i.stack.imgur.com/xNffR.png";
private static final String CARD_URL = "http://i.stack.imgur.com/uVS1D.png";
private static JPanel createDeckPanel() {
JPanel panel = new JPanel();
try {
JLabel label = new JLabel(new ImageIcon(new URL(DECK_URL)));
label.setText("DECK");
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.BOTTOM);
panel.add(label);
} catch (Exception ex) {
ex.printStackTrace();
}
return panel;
}
private static JPanel createCenterPanel(int rows, int cols) {
JPanel panel = new JPanel(new GridLayout(rows, cols));
for (int i = 0; i < rows*cols; i++) {
try {
panel.add(new JLabel(new ImageIcon(new URL(CARD_URL))));
} catch (Exception ex) {
ex.printStackTrace();
}
}
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
JPanel panel = new JPanel(new BorderLayout());
panel.add(createDeckPanel(), BorderLayout.LINE_START);
panel.add(createCenterPanel(2, 6));
JOptionPane.showMessageDialog(null, panel);
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.