2012-05-07 5 views
0

일련의 버튼을 표시하기 위해 JPanel을 얻으려고합니다. 각 버튼은 내 Sudoku 보드 값과 관련된 값 중 하나입니다. 내 보드를 만든 메뉴를 만든 다음 메뉴 아래의 선택 가능한 옵션을 표시하려고하지만 같은 Jframe에서 보드 전에. 모든 버튼을 JPanel에 놓고 그 패널을 프레임 위에 놓기를 바랬습니다. JPanel은 표시되지만 버튼은 표시되지 않습니다. 한 번에 나는 전시하는 패널을 가지고있다. 그러나 그들 중의 누구도 크기가 아니었고, 많은 것이 있었다. 내가 더 구체적으로해야 할 질문은 일련의 단추 인 내 Sudoku 보드가 들어있는 프레임에 배치 된 JPanel에서 일련의 단추를 표시하기 위해 올바른 코드를 사용하는 코드입니다.JPanel 버튼이 표시되지 않습니다.

이 단일 툴바와 버튼이이 단일 JFrame에 많은 도움이되는 이유는 무엇입니까? 어쨌든 여기 내 JPanel입니다 내 도구 모음에 대한 코드입니다.

class ToolBar extends JPanel { 
    // instance initializer (constructor equivalent) 

    public ToolBar() { 
     super(); 
     this.setLayout(myLayout); 

     myLayout.setAlignment(FlowLayout.TRAILING); 
     Button[] panelButton = new Button[size]; 

     //Rectangle rec = new Rectangle(330,45,BUTTON, BUTTON); 
     //setBounds(rec); 
     setPreferredSize(new Dimension(330, 45)); 

     for(int i = 0; i < size; i++) { 
      Rectangle r = new Rectangle(12, 12, 22, 22); 
      center = new ImageIcon(view.drawSymbol(i)); 

      panelButton[i]= new Button(); 
      panelButton[i].setIcon(center); 
      panelButton[i].setOpaque(true); 
      panelButton[i].setBorder(BorderFactory.createLineBorder(Color.black)); 
      panelButton[i].setBounds(r); 

      this.add(panelButton[i]); 
      this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
      this.setVisible(true); 
     } 
    } 
}; 

답변

3

도구 모음에서 setBounds을 사용하여 표시하고 배경을 빨간색으로 설정하고 AWT 버튼을 Swing JButton으로 대체 한 다음 버튼에 일부 텍스트도 설정했습니다. 나는 컴파일하기 위해 내 테스트 코드에 뭔가를 주석 있지만 다시 아래에 넣어 :

class ToolBar extends JPanel { 
    // instance initializer (constructor equivalent) 

    public ToolBar() { 
     super(); 
     this.setLayout(myLayout); 

     myLayout.setAlignment(FlowLayout.TRAILING); 
     JButton[] panelButton = new JButton[5]; 
     this.setBackground(Color.red); 
     this.setBounds(0, 0, 200, 200); 

     //Rectangle rec = new Rectangle(330,45,BUTTON, BUTTON); 
     //setBounds(rec); 
     setPreferredSize(new Dimension(330, 45)); 

     for (int i = 0; i < 5; i++) { 
      Rectangle r = new Rectangle(22, 22);     
      panelButton[i] = new JButton(); 
      panelButton[i].setText("  "); 
      panelButton[i].setIcon(new ImageIcon(view.drawSymbol(i))); 
      panelButton[i].setOpaque(true); 
      panelButton[i].setBorder(BorderFactory.createLineBorder(Color.black)); 
      panelButton[i].setBounds(r); 
      this.add(panelButton[i]); 
      this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
      this.setVisible(true); 
     } 
    } 
}; 

나는 또한 아래의 전체 테스트 코드를 게시하고있다 : 라인 this.add(new ToolBar());에 대한

import java.awt.Color; 
import java.awt.ComponentOrientation; 
import java.awt.Dimension; 
import java.awt.Rectangle; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JPanel; 

/* 
* To change this template, choose Tools | Templates and open the template in 
* the editor. 
*/ 
/** 
* 
* @author hahahaha 
*/ 
public class NewJFrame extends javax.swing.JFrame { 

    /** 
    * Creates new form NewJFrame 
    */ 
    public NewJFrame() { 
     initComponents(); 
     this.add(new ToolBar()); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 400, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 300, Short.MAX_VALUE) 
     ); 

     pack(); 
    }// </editor-fold> 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* 
     * Set the Nimbus look and feel 
     */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* 
     * If Nimbus (introduced in Java SE 6) is not available, stay with the 
     * default look and feel. For details see 
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* 
     * Create and display the form 
     */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new NewJFrame().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    // End of variables declaration 
    class ToolBar extends JPanel { 
     // instance initializer (constructor equivalent) 

     public ToolBar() { 
      super(); 
      //this.setLayout(myLayout); 

      //myLayout.setAlignment(FlowLayout.TRAILING); 
      JButton[] panelButton = new JButton[5]; 
      this.setBackground(Color.red); 
      this.setBounds(0, 0, 200, 200); 

      //Rectangle rec = new Rectangle(330,45,BUTTON, BUTTON); 
      //setBounds(rec); 
      setPreferredSize(new Dimension(330, 45)); 

      for (int i = 0; i < 5; i++) { 
       Rectangle r = new Rectangle(22, 22); 
       //center = new ImageIcon(view.drawSymbol(i)); 
       panelButton[i] = new JButton(); 
       panelButton[i].setText("  "); 
       //panelButton[i].setIcon(center); 
       panelButton[i].setOpaque(true); 
       panelButton[i].setBorder(BorderFactory.createLineBorder(Color.black)); 
       panelButton[i].setBounds(r); 
       this.add(panelButton[i]); 
       this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
       this.setVisible(true); 
      } 
     } 
    }; 
} 

봐 어디 JFrame을 인스턴스화하고 툴바를 JFrame에 추가하십시오.

조언의 조각 : 가능한 한 많은

않도록 AWT 구성 요소

2

setPreferredSize 또는 setBounds를 사용하지 마십시오. LayoutManager가 위치와 치수를 처리하도록합니다.

필요에 따라 직접 구현하는 대신 JToolBar를 사용하는 것이 좋습니다.

관련 문제