2012-05-06 2 views
0

これは私が想定していたよりもはるかに難しいと判明しています。私がしたいのは、このJPanelをフレームの左側に「貼り付ける」ことだけです。ここに私がこれまで持っているものがあります。 (私は可能な場合はGridBagLayoutを使用したくない(私はJavaの初心者です))。JPanelをJavaのフレームのLEFT側に揃えたいのですが

labels = new JPanel(); 
JLabel currentWordLA = new JLabel("Current word:"); 
JLabel triedLettersLA = new JLabel("Tried letters:"); 
JLabel triesLeftLA = new JLabel("Tries remaining:"); 
JButton restart = new JButton("Reset"); 

labels.setLayout(new BoxLayout(labels, BoxLayout.Y_AXIS)); 
labels.add(currentWordLA); 
labels.add(triedLettersLA); 
labels.add(triesLeftLA); 
scorePanel.add(labels, BorderLayout.WEST); 

編集:申し訳ありませんが、ここに完全なコードがあります。これはフレーム内のパネル内のパネルのようなものです。フレームを左に揃えようとしているだけです。ありがとう!

import java.awt.event.*; 
import java.awt.*; 
import javax.swing.*; 

public class Hangman { 

    JFrame frame; 
    private String[] wordList = { 
     "computer", "java", "activity", "alaska", "appearance", "article", 
     "automobile", "basket", "birthday", "canada", "central", "character", 
     "chicken", "chosen", "cutting", "daily", "darkness", "diagram", 
     "disappear", "driving", "effort", "establish", "exact", 
     "establishment", "fifteen", "football", "foreign", "frequently", 
     "frighten", "function", "gradually", "hurried", "identity", 
     "importance", "impossible", "invented", "italian", "journey", 
     "lincoln", "london", "massage", "minerals", "outer", "paint", 
     "particles", "personal", "physical", "progress", "quarter", 
     "recognise", "replace", "rhythm", "situation", "slightly", 
     "steady", "stepped", "strike", "successful", "sudden", 
     "terrible", "traffic", "unusual", "volume", "yesterday"}; 
    private String mysteryWord; 
    private boolean finished = false; 
    private boolean won = false; 
    private Button a[]; 

    public static void main(String[] args) { 
     Hangman gui = new Hangman(); 
     gui.go(); 
    } 

    class myDrawPanel extends JPanel { 

     public void paintComponent(Graphics g) { 
      setBackground(Color.white); 
      g.setColor(Color.gray); 
      g.fillRect(50, 200, 150, 20); 
      g.fillRect(90, 20, 10, 200); 
      g.fillRect(90, 20, 60, 10); 
      g.setColor(Color.black); 
      g.fillRect(145, 20, 5, 25); 
     } 
    } 

    public void go() { 
     frame = new JFrame("Hangman"); 
     JPanel topPanel = new JPanel(); 
     myDrawPanel noosePanel = new myDrawPanel(); 
     JPanel bottomPanel = new JPanel(); 
     JPanel scorePanel = new JPanel(); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new GridLayout(2, 0)); 
     bottomPanel.setLayout(new GridLayout(0, 2)); 
     scorePanel.setSize(20, 100); 

     noosePanel.setBorder(BorderFactory.createTitledBorder("Your progress.")); 
     topPanel.setBorder(BorderFactory.createTitledBorder("Your arsenal.")); 
     scorePanel.setBorder(BorderFactory.createTitledBorder("Your score.")); 
     frame.add(topPanel); 
     frame.add(bottomPanel); 
     bottomPanel.add(scorePanel); 
     bottomPanel.add(noosePanel); 

     //The Stats section. 
     JPanel labels = new JPanel(); 
     JLabel currentWordLA = new JLabel("Current word:"); 
     JLabel triedLettersLA = new JLabel("Tried letters:"); 
     JLabel triesLeftLA = new JLabel("Tries remaining:"); 
     JButton restart = new JButton("Reset"); 

     labels.setLayout(new BoxLayout(labels, BoxLayout.Y_AXIS)); 
     labels.add(currentWordLA); 
     labels.add(triedLettersLA); 
     labels.add(triesLeftLA); 
     scorePanel.add(labels, BorderLayout.WEST); 

     int i; 
     StringBuffer buffer; 
     a = new Button[26]; 
     topPanel.setLayout(new GridLayout(4, 0, 10, 10)); 

     // create all 26 buttons 
     for (i = 0; i < 26; i++) { 
      buffer = new StringBuffer(); 
      buffer.append((char) (i + 65)); 
      a[i] = new Button(buffer.toString()); 
      a[i].setSize(100, 100); 
      //a[i].addActionListener(this); 
      topPanel.add(a[i]); 
     } 

     frame.setSize(500, 500); 
     frame.setResizable(false); 
     frame.setVisible(true); 
    } 
} 
+1

は、あなたが今まで持っているすべてのことですか? 'BorderLayout'が正しく動作するためには、パネルにコンポーネントを追加する必要があります。 –

+0

'go()'の代わりにコンストラクタを実装することができます。 – 11684

答えて

2

BorderLayout.CENTERに何かを追加してみてください、次のように:

scorepanel.add(new JPanel(), BorderLayout.CENTER); //I guess scorepanel is your frame 
    //your code here 
2

私はあなたのレイアウトが好き。あなたの例をいくつか変更しました:

  • MyDrawPanelは描画用です。 getPreferredSizeを上書きする場合は、setSize()を大量に呼び出す代わりにpack()を使用できます。 paintComponentの中で、パネルのgetWidthgetHeightに関連する図面を作成すると、どのサイズのものでも正しく表示されます。

  • 私はラベルにJLabel.LEFTを使用しました。レイアウトがどこにあるかを確認できるように背景色を灰色にしました。さらにhereがあります。

  • MyDrawPanelのような標準的な名前付けを使用すると、他の人がコードを読みやすくなります。

コード:

import java.awt.event.*; 
import java.awt.*; 
import javax.swing.*; 

public class Hangman { 

    JFrame frame; 
    private String[] wordList = { 
     "computer", "java", "activity", "alaska", "appearance", "article", 
     "automobile", "basket", "birthday", "canada", "central", "character", 
     "chicken", "chosen", "cutting", "daily", "darkness", "diagram", 
     "disappear", "driving", "effort", "establish", "exact", 
     "establishment", "fifteen", "football", "foreign", "frequently", 
     "frighten", "function", "gradually", "hurried", "identity", 
     "importance", "impossible", "invented", "italian", "journey", 
     "lincoln", "london", "massage", "minerals", "outer", "paint", 
     "particles", "personal", "physical", "progress", "quarter", 
     "recognise", "replace", "rhythm", "situation", "slightly", 
     "steady", "stepped", "strike", "successful", "sudden", 
     "terrible", "traffic", "unusual", "volume", "yesterday"}; 
    private String mysteryWord; 
    private boolean finished = false; 
    private boolean won = false; 
    private Button a[]; 

    public static void main(String[] args) { 
     Hangman gui = new Hangman(); 
     gui.go(); 
    } 

    class MyDrawPanel extends JPanel { 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(256, 256); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      setBackground(Color.white); 
      g.setColor(Color.gray); 
      g.fillRect(50, 200, 150, 20); 
      g.fillRect(90, 20, 10, 200); 
      g.fillRect(90, 20, 60, 10); 
      g.setColor(Color.black); 
      g.fillRect(145, 20, 5, 25); 
     } 
    } 

    public void go() { 
     frame = new JFrame("Hangman"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new GridLayout(2, 0)); 

     JPanel topPanel = new JPanel(); 
     topPanel.setBorder(BorderFactory.createTitledBorder("Your arsenal.")); 
     MyDrawPanel noosePanel = new MyDrawPanel(); 
     noosePanel.setBorder(BorderFactory.createTitledBorder("Your progress.")); 
     JPanel bottomPanel = new JPanel(new GridLayout(0, 2)); 
     JPanel scorePanel = new JPanel(new GridLayout()); 
     scorePanel.setBorder(BorderFactory.createTitledBorder("Your score.")); 

     frame.add(topPanel); 
     frame.add(bottomPanel); 
     bottomPanel.add(scorePanel); 
     bottomPanel.add(noosePanel); 

     //The Stats section. 
     JPanel labels = new JPanel(); 
     labels.setLayout(new BoxLayout(labels, BoxLayout.Y_AXIS)); 
     labels.setBackground(Color.lightGray); 
     JLabel currentWordLA = new JLabel("Current word:", JLabel.LEFT); 
     JLabel triedLettersLA = new JLabel("Tried letters:", JLabel.LEFT); 
     JLabel triesLeftLA = new JLabel("Tries remaining:", JLabel.LEFT); 

     labels.add(currentWordLA); 
     labels.add(triedLettersLA); 
     labels.add(triesLeftLA); 
     scorePanel.add(labels); 

     int i; 
     StringBuffer buffer; 
     a = new Button[26]; 
     topPanel.setLayout(new GridLayout(4, 0, 10, 10)); 

     // create all 26 buttons 
     for (i = 0; i < 26; i++) { 
      buffer = new StringBuffer(); 
      buffer.append((char) (i + 65)); 
      a[i] = new Button(buffer.toString()); 
      //a[i].addActionListener(this); 
      topPanel.add(a[i]); 
     } 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 
関連する問題