2016-05-04 3 views
0

メッセージのJLabelを表示された四角形(各四角はJLabel)のグリッドの下に表示しようとしています。 JPanelにメッセージを追加しようとしないと、4x12グリッドの格子がうまく表示されます。しかし、メッセージJLabelをグリッドの下または上に追加しようとすると、グリッドのグリッドが2つに分割され(最初の四角形の後に)、1つの四角形の列が左に表示され、次にそこに表示されますギャップであり、他の3つの四角の列が表示されます。メッセージが下に表示されます。このメッセージを追加すると、正方形のグリッドがこのように分割されるのはなぜですか?私は下に私のコードを入れました。助けをありがとうございました。グリッドの下にメッセージJLabelを追加しようとすると、GridBagLayoutが奇妙なことをする

public void displayGrid(JButton button) { 

    JLabel instructionsLabel = new JLabel ("Select the locations of the four objects that you saw previously. Be careful - when you select one box, you can't take it back."); 

    try { 
     squareImage = ImageIO.read(this.getClass().getResource("stimulus(0).gif")); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    for(int i = 0; i < 16; i++){ 
     c.gridx = i % 4; 
     c.gridy = i/4; 
     squareLabels[i] = new JLabel(new ImageIcon(squareImage)); 
     panel.add(squareLabels[i], c); 
     squareLabels[i].addMouseListener(this); 
    } 

    panel.validate(); 

    c.gridy += 1; 
    c.gridx = 0; 
    panel.add(instructionsLabel, c); 
    panel.validate(); 
} 

答えて

0

私はあなたが別ののGridBagConstraintsオブジェクトを使用する必要があると思う - あなたは上記のやっているとあなたは同じインスタンスを再利用することはできません。

+0

GridBagConstraintsを意味しますか?私は同じインスタンスを使用することが全体のアイデアだと思った。 2つの異なるGridBagConstraintsを使用している場合、相互に干渉しないようにすることはできません。 – lb91

+0

'GridBagConstraints'の再利用に問題はありません:https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html – Berger

+0

さて、私はかなり問題があったと確信しています。過去に同じGridBagConstraintsオブジェクトを使用していましたが、私が間違っていると受け入れる意思があります。コード内の同じスレッド上でIOとSwingのモデル操作をきわめて明確にする - SwingWorkerを使用してIOタスクを管理することを検討してください。また、あなたは 'c.gridy = i/4;'を持っています。そこには私のバグのように見えます - これは設計によるものですか? – Lee

1

JLabelの4 * 4行列の後に、5行目の最初の列にinstructionsLabelを追加しています。あなたのinstructionsLabelがあなたのsquareLabels[i]よりも長い長さを持っているので、GridBagLayoutのデフォルトの動作はinstructionsLabelに合わせて最初の列を伸ばしてしまうため

問題があります。

あなたはinstructionsLabelを追加する前に、あなたのGridBagConstraintsオブジェクトcためgridWidthを設定する必要があります。

c.gridy += 1; 
c.gridx = 0; 
constraint.gridwidth = 4; 
panel.add(instructionsLabel, c); 

あなたが複数の列にinstructionsLabelを配置するレイアウトマネージャを言っているconstraint.gridwidth = 4;を設定することにより。 (ここで私はexaclty 4列を設定します。)

あなたはGridBagLayoutとそのConstraintsより良いの振る舞いを理解するのに役立つだろうあなたのJLabel sのいくつかのLineBorderを追加します。

[UPDATE] あなたがメインのレイアウトとしてBorderLayout使用し、その後、BorderLayoutの中心にあなたのJLabel sおよび他のコンポーネントとJPanelを追加することができます。それでJPanelGridBagLayoutを使ってください。この例を参照してください:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class TestGridBagLAyout extends JFrame { 

    private static final long serialVersionUID = -392403850862179703L; 
    private static final int FRAME_WIDTH = 200; 
    private static final int FRAME_HEIGHT = 200; 

    public TestGridBagLAyout() { 
     setSize(FRAME_WIDTH, FRAME_HEIGHT); 
     // 
     setForeground(Color.RED); 
     // 
     setLayout(new BorderLayout()); 
     add(initCenterPanel()); 
    } 

    private JPanel initCenterPanel() { 
     GridBagLayout gridBagLayout = new GridBagLayout(); 
     JPanel centerPanel = new JPanel(gridBagLayout); 
     centerPanel.setBackground(Color.WHITE); 
     // 
     GridBagConstraints constraint = new GridBagConstraints(0, 0, 1, 1, 0d, 0d, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0); 
     // 
     JLabel[] squareLabels = new JLabel[16]; 
     for(int i = 0; i < 16; i++){ 
      constraint.gridx = i % 4; 
      constraint.gridy = i/4; 
      squareLabels[i] = new JLabel("Test"); 
      centerPanel.add(squareLabels[i], constraint); 
     } 
     // 
     JLabel instructionsLabel = new JLabel("This is long instruction!"); 
     constraint.gridy += 1; 
     constraint.gridx = 0; 
     constraint.gridwidth = 4; 
     centerPanel.add(instructionsLabel, constraint); 
     // 
     return centerPanel; 
    } 
} 

幸運。

+0

あなたは詳しく教えてもらえますか?あなたがここで何をしているのか分かりません。 – lb91

+0

ありがとうございます。なぜあなたは今それを提案したのか理解しています。私はgridwidthの値を変更し、グリッドは4x4を表示します。しかし、グリッドはJPanelの中​​央に表示されていません(中央の左側にあります)。なぜこれが当てはまるのか、それをJPanelの中​​心に移動する方法を知っていますか? – lb91

+0

JLabelのセンタリングに関するアップデートを参照してください。この回答が役立つ場合は、回答としてマークすることができます。 – STaefi

関連する問題