예,
First Row = Text Field
Second Row = 5 buttons
Third Row = 5 Buttons
데이터를 넣고 보더 레이아웃의 중심에 남아있는 데이터,
방법 테두리 레이아웃의 북쪽에 첫 번째, 두 번째, 세 번째 행을 넣으시겠습니까? 여기에 코드의 문제는, 텍스트 필드 상단 버튼이 표시되어 같은 줄에 표시되어있다
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GridLayoutFrame extends JFrame
{
private JButton[] buttons; // Array of Buttons 1 to 10
private JButton[] topButton; // 10 buttons that has to be placed under Text field 5 button in each row
private JPanel Bottom; // A panel to show everything under first three Row
private JPanel Top; // Top Panel that contains Text Field and 2nd and third Row
private JTextField Input = new JTextField();
private static final String[] names =
{"7", "8", "9", "4", "5", "6","1","2","3"};
private static final String[] topButtons =
{"MC","MR","MS","M+","M-","<-","CE","C","+-","Root"};
// no-argument constructor
public GridLayoutFrame()
{
super ("Calculator");
Bottom = new JPanel();
Top = new JPanel();
Top.setLayout(new GridLayout(3,1,5,5)); // Three Rows (first row must only show a text field
Bottom.setLayout(new GridLayout(5,5,10,10));
topButton = new JButton[topButtons.length];
buttons = new JButton[ names.length ];
Input.setSize(500,500);
Input.setEditable(false);
Input.setBackground(Color.WHITE);
Input.setPreferredSize(new Dimension(300, 30));
for (int count = 0; count < topButtons.length; count++)
{
topButton[ count ] = new JButton(topButtons[count]);
Top.add(topButton[ count ]); // add button to panel
}
for (int count = 0; count < names.length; count++)
{
buttons[ count ] = new JButton(names[count]);
Bottom.add(buttons[ count ]); // add button to panel
}
add(Top,BorderLayout.NORTH);
//add(Bottom,BorderLayout.CENTER);
}
} // end class GridLayoutFrame
내 코드입니다. 도와주세요
새 JPanel을 만드는 대신 텍스트 필드를 BorderLayout.NORTH에 직접 추가 할 수 있습니다. –
@AKJ 네 말이 맞아, 고마워! – nolegs