선택할 최적의 레이아웃을 찾으려고합니다. 나는로 JPanel
을 확장하여 만든 grid
이 있습니다Java Swing을 사용하여 레이아웃
public class TestPane extends JPanel{
// code
}
나는 텍스트/숫자를 표시 할
- 라벨이 필요합니다.
- 사용자가 옵션을 선택하기위한 드롭 다운 목록입니다.
- 사용자가 일부 매개 변수를 입력 할 수있는 텍스트 필드입니다.
편집
이 그림의 초안이다. 너무 깔끔한 것에 대해 유감스럽게 생각합니다.
사용할 레이아웃에 대한 제안.
레이블과 grid
은 알고리즘 계산으로 계속 업데이트됩니다.
편집 - grid
구성 요소가 크므로 왼쪽 또는 오른쪽 중 하나 일 필요가 있다고 생각했습니다. 남은 구성 요소는 다른 행에있을 수 있습니다.
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
public class GridBagLayoutExample {
JFrame guiFrame;
public static void main(String[] args) {
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new GridBagLayoutExample();
}
});
}
public GridBagLayoutExample()
{
guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("GridBagLayout Example");
guiFrame.setSize(600,300);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
//creating a border to highlight the component areas
Border outline = BorderFactory.createLineBorder(Color.black);
//create GribBagLayout and the GridBagLayout Constraints
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();
cons.fill = GridBagConstraints.BOTH;
JPanel compPanel = new JPanel();
compPanel.setLayout(gridBag);
cons.gridx = 2;
cons.gridy = 2;
JLabel randomLbl = new JLabel("In Xanadu did Kubla Khan, "
+ "A stately pleasure-dome decree");
randomLbl.setBorder(outline);
gridBag.setConstraints(randomLbl, cons);
compPanel.add(randomLbl);
cons.ipady = 100;
cons.ipadx = 100;
cons.weighty = 1.0;
cons.gridx = 0;
cons.gridy = 0;
JLabel tallLbl = new JLabel("Tall and Long");
tallLbl.setBorder(outline);
gridBag.setConstraints(tallLbl, cons);
compPanel.add(tallLbl);
cons.ipady = 50;
cons.ipadx = 100;
cons.weightx = 0;
cons.gridx = 0;
cons.gridy = 1;
JButton hello = new JButton("Hello");
gridBag.setConstraints(hello, cons);
compPanel.add(hello);
cons.ipady = 100;
cons.ipadx = 10;
cons.gridx = 1;
cons.gridy = 1;
JButton goodbye = new JButton("GoodBye");
gridBag.setConstraints(goodbye, cons);
compPanel.add(goodbye);
cons.weightx = 0;
cons.gridx = 0;
cons.gridy = 2;
JButton eh = new JButton("eh?");
gridBag.setConstraints(eh, cons);
compPanel.add(eh);
guiFrame.add(compPanel);
guiFrame.setVisible(true);
}
}
을 . 그리드를 레이블과 텍스트 상자와 별도로 또는 함께 사용해야합니까? 디자인 관점에서 어느 것이 좋습니다?
추천 정보가 충분하지 않습니다. 모든 구성 요소를 한 행 또는 여섯 개의 별도 행에 넣을 지 여부는 알 수 없습니다. [레이아웃 관리자] (http://docs.oracle.com/javase/tutorial/uiswing/layout)에서 스윙 튜토리얼을 읽으십시오. /visual.html). 그래서 스스로 결정할 수 있습니다. 다른 레이아웃 관리자가있는 패널을 중첩하여 원하는 레이아웃을 얻을 수 있음을 기억하십시오. – camickr
MIG 레이아웃을 살펴보십시오. 타사 라이브러리이지만 GUI 응용 프로그램을 빠르고 쉽게 작성할 수 있습니다. http://www.miglayout.com/ –
@HovercraftFullOfEels 위의 편집을 참조하십시오. – Dinesh