-1
我的目標是僅將圖像移動到中心位置。
但是,當我將圖像向右或向左移動時,from
標籤和文本字段也會被我爲圖像指定的量推送。
有什麼辦法可以防止標籤和文本字段在圖片移動時移動?
這是我做的,到目前爲止:GridBagLayout:如何移動指定組件
// Center
center = new JPanel();
center.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
Border innerC = BorderFactory.createTitledBorder("Travel details");
Border outsideC = BorderFactory.createEmptyBorder(5,5,5,5);
center.setBorder(BorderFactory.createCompoundBorder(outsideC,innerC));
fromLabel = new JLabel("From : ");
fromField = new JTextField("to",30);
fromLabel.setFont(font);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 4.0;
gbc.weighty = 4.0;
gbc.insets = new Insets(5,4,5,5);
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.LINE_END;
center.add(fromLabel,gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.gridwidth = 1;
center.add(fromField,gbc);
JButton trainImage = new JButton();
trainImage.setIcon(new ImageIcon("train.jpg"));
gbc.gridx = 1;
gbc.gridy = 20;
gbc.weightx = 10;
gbc.weighty = 20;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = 3;
gbc.ipadx = 5;
center.add(trainImage,gbc);
this.add(center, BorderLayout.CENTER);
this.setVisible(true);
主要方法:
public static void main(String[] args) {
gui g = new gui();
}
爲了更好地提供幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –