2017-12-27 72 views
-1

私の目標は、画像のみを中央に移動することです。
しかし、イメージを右または左に移動すると、ラベルとテキストフィールドもイメージ用に指定した量だけプッシュされます。
イメージの移動中にラベルとテキストフィールドが動かないようにする方法はありますか?ここ
は、私がこれまでにやったことです: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(); 
} 

enter image description here

+0

[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/)を投稿してください。 –

答えて

1

上記のコードはどのように両方のweightx/y & gridx/y仕事の誤解を示唆しています。このMCVEは、左上にラベルとテキストフィールドを残して、ボタンを画像の中央に合わせるように番号を修正します。

import java.awt.*; 
import java.awt.image.*; 
import javax.swing.*; 
import javax.swing.border.*; 

public class CenteredImageInGridBag { 

    private JComponent ui = null; 

    CenteredImageInGridBag() { 
     initUI(); 
    } 

    public final void initUI() { 
     if (ui != null) { 
      return; 
     } 

     ui = new JPanel(new BorderLayout(4, 4)); 
     ui.setBorder(new EmptyBorder(4, 4, 4, 4)); 

     JPanel center = new JPanel(); 
     ui.add(center); 
     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)); 

     JLabel fromLabel = new JLabel("From : "); 
     JTextField fromField = new JTextField("to", 30); 

     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.weightx = 0; 
     gbc.weighty = 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 = 0; 
     gbc.weighty = 0; 
     gbc.fill = GridBagConstraints.NONE; 
     gbc.anchor = GridBagConstraints.LINE_START; 
     gbc.gridwidth = 1; 
     center.add(fromField, gbc); 

     JButton trainImage = new JButton(); 
     BufferedImage image = 
       new BufferedImage(40,10,BufferedImage.TYPE_INT_RGB); 
     trainImage.setIcon(new ImageIcon(image)); 

     gbc.gridx = 1; 
     gbc.gridy = 1; 
     gbc.weightx = 1; 
     gbc.weighty = 1; 
     gbc.fill = GridBagConstraints.NONE; 
     gbc.anchor = GridBagConstraints.CENTER; 
     gbc.gridwidth = 3; 
     gbc.ipadx = 5; 
     center.add(trainImage, gbc); 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String[] args) { 
     Runnable r =() -> { 
      try { 
       UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName()); 
      } catch (Exception useDefault) { 
      } 
      CenteredImageInGridBag o = new CenteredImageInGridBag(); 

      JFrame f = new JFrame(o.getClass().getSimpleName()); 
      f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
      f.setLocationByPlatform(true); 

      f.setContentPane(o.getUI()); 
      f.pack(); 
      f.setMinimumSize(f.getSize()); 

      f.setVisible(true); 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
関連する問題