2016-12-30 5 views
3

저는 JFrame에서 출력물로 그리드를 얻었고 A, B, C 등의 텍스트를 그리드 왼쪽과 1, 2, 3 등과 같이 세로로 표시하는 방법을 알아 내려고했습니다. 위에.GridLayout에 텍스트를 표시하는 방법은 무엇입니까?

이 내 출력 : http://imgur.com/a/rDs2Y

이 코드입니다 :

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

    public class battleShips extends JFrame{ 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

JButton applyButton = new JButton("New Game"); 
GridLayout experimentLayout = new GridLayout(0,6); 

public battleShips(String name) { 
    super(name); 
    setResizable(false); 
} 



public void addComponentsToPane(final Container pane) { 

    final JPanel compsToExperiment = new JPanel(); 
    compsToExperiment.setLayout(experimentLayout); 
    JPanel controls = new JPanel(); 
    controls.setLayout(new GridLayout(2,3)); 

    //Set up components preferred size 
    JButton b = new JButton("Just fake button"); 
    Dimension buttonSize = b.getPreferredSize(); 
    compsToExperiment.setPreferredSize(new Dimension((int)(buttonSize.getWidth() * 2.5)+5, 
      (int)(buttonSize.getHeight() * 3.5)+50*2)); 

    //Add buttons to experiment with Grid Layout 
    for (int i = 0; i<36; i++) 
    { 
    compsToExperiment.add(new JButton("~")); 
    } 

    //Add controls to set up horizontal and vertical gaps 

    controls.add(applyButton); 

    //Process the Apply gaps button press 
    applyButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 

      //Set up the layout of the buttons 
      experimentLayout.layoutContainer(compsToExperiment); 
     } 
    }); 
    pane.add(compsToExperiment, BorderLayout.NORTH); 
    pane.add(new JSeparator(), BorderLayout.CENTER); 
    pane.add(controls, BorderLayout.SOUTH); 

} 

private static void createAndShowGUI() { 
    //Create and set up the window. 
    battleShips frame = new battleShips("Battleships"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    //Set up the content pane. 
    frame.addComponentsToPane(frame.getContentPane()); 
    //Display the window. 
    frame.pack(); 
    frame.setVisible(true); 
} 




public static void main(String[] args) { 
    /* Use an appropriate Look and Feel */ 
    try { 
     //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
     UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
    } catch (UnsupportedLookAndFeelException ex) { 
     ex.printStackTrace(); 
    } catch (IllegalAccessException ex) { 
     ex.printStackTrace(); 
    } catch (InstantiationException ex) { 
     ex.printStackTrace(); 
    } catch (ClassNotFoundException ex) { 
     ex.printStackTrace(); 
    } 
    /* Turn off metal's use of bold fonts */ 
    UIManager.put("swing.boldMetal", Boolean.FALSE); 

    //Schedule a job for the event dispatch thread: 
    //creating and showing this application's GUI. 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(); 
     } 
    }); 

도움을 주시면 감사하겠습니다!

답변

2

어쩌면 이렇게 할 수 있을까요?

enter image description here

package gnu; 

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

public class Main extends JFrame { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    JButton applyButton = new JButton("New Game"); 
    GridLayout experimentLayout = new GridLayout(0, 6); 

    public Main(String name) { 
     super(name); 
     setResizable(false); 
    } 


    public void addComponentsToPane(final Container pane) { 

     final JPanel compsToExperiment = new JPanel(); 
     compsToExperiment.setLayout(experimentLayout); 
     JPanel controls = new JPanel(); 
     controls.setLayout(new GridLayout(2, 3)); 

     //Set up components preferred size 
     JButton b = new JButton("Just fake button"); 
     Dimension buttonSize = b.getPreferredSize(); 
     compsToExperiment.setPreferredSize(new Dimension((int) (buttonSize.getWidth() * 2.5) + 5, 
       (int) (buttonSize.getHeight() * 3.5) + 50 * 2)); 

     //Add buttons to experiment with Grid Layout 

     for (int i = 1; i < 7; i++) { 
      if(i==1) compsToExperiment.add(new Label("")); 
      else compsToExperiment.add(new Label("" + (i-1))); 
     } 

     for (int i = 0; i < 30; i++) { 
      if (i == 0) compsToExperiment.add(new Label("A")); 
      else if (i == 6) compsToExperiment.add(new Label("B")); 
      else if (i == 12) compsToExperiment.add(new Label("C")); 
      else if (i == 18) compsToExperiment.add(new Label("D")); 
      else if (i == 24) compsToExperiment.add(new Label("E")); 
      else compsToExperiment.add(new JButton("~")); 
     } 

     //Add controls to set up horizontal and vertical gaps 

     controls.add(applyButton); 

     //Process the Apply gaps button press 
     applyButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 

       //Set up the layout of the buttons 
       experimentLayout.layoutContainer(compsToExperiment); 
      } 
     }); 
     pane.add(compsToExperiment, BorderLayout.NORTH); 
     pane.add(new JSeparator(), BorderLayout.CENTER); 
     pane.add(controls, BorderLayout.SOUTH); 

    } 

    private static void createAndShowGUI() { 
     //Create and set up the window. 
     Main frame = new Main("Battleships"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //Set up the content pane. 
     frame.addComponentsToPane(frame.getContentPane()); 
     //Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 


    public static void main(String[] args) { 
    /* Use an appropriate Look and Feel */ 
     try { 
      //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
      UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
     } catch (UnsupportedLookAndFeelException ex) { 
      ex.printStackTrace(); 
     } catch (IllegalAccessException ex) { 
      ex.printStackTrace(); 
     } catch (InstantiationException ex) { 
      ex.printStackTrace(); 
     } catch (ClassNotFoundException ex) { 
      ex.printStackTrace(); 
     } 
    /* Turn off metal's use of bold fonts */ 
     UIManager.put("swing.boldMetal", Boolean.FALSE); 

     //Schedule a job for the event dispatch thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 
+1

완벽한 친구, 건배! – KingF

+1

궁금합니다. '~'중 하나를 누르면 기호가 변경됩니다. – KingF

+1

@KingF '~'의 기호 변경은보기를 업데이트 할 때 수행 할 수 있습니다.'repaint()'메소드를 사용할 수 있다고 생각합니다. Google로 검색하거나 여기에서 gridlayout 스윙 업데이트보기 또는 유사한 키워드를 검색하거나 새로운 질문을 할 수 있습니다. –