GridBagLayout을 사용하여 JFrame에서 여러 번 복제되는 'Preset'이라는 JPanel을 만듭니다. 각 프리셋에는 여러 행 (JPanels)이 있습니다. 내 목표는 첫 번째 줄만 표시되지만 편집 단추를 클릭하면 모두 표시됩니다. 지금 편집 버튼이 작동하지만 줄 사이에 큰 공간이 있습니다. 여분의 선이 붕괴 될 때 각 프리셋은 서로 위에 놓이게됩니다 (공백 없음). 다음 그림에서 내가 말하는 것을 볼 수 있습니다.GridBagLayout의 간격을 수정하는 방법
이는 모습입니다 :
이것은 내가 그것을보고하는 방법입니다 :
내가 구라 두 가방에 뭔가를 할 필요가 꽤 확신하지만 나도 몰라 무엇을 . 여러 자습서를 읽고 필자가 생각한대로 작성했지만 행운은 없습니다. 고맙습니다.
package SSCCE;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class UI extends JFrame{
private final static int HEIGHT = 600;
private final static int WIDTH = 730;
private JPanel pane; //Pane that stores accounts
private JScrollPane scroller;
private Preset[] branches;
public static void main(String[] args) {
JFrame frame = new UI();
}
public UI(){
//Make the UI close when the exit button is clicked
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Sets the size of the UI
this.setSize(WIDTH, HEIGHT);
//Set the layout and add components to it
this.setLayout(new BorderLayout());
//Reads in the settings and sets up the branches
populateBranches();
pane = new JPanel();
pane.setLayout(new GridLayout(branches.length,1));
for (int i = 0; i < branches.length; i++){
pane.add(branches[i]);
}
//Create the center pane of the BorderLayout
scroller = new JScrollPane(pane);
scroller.createVerticalScrollBar();
this.add(scroller,BorderLayout.CENTER);
//Makes the UI visible
this.setVisible(true);
}
private void populateBranches(){
//Populates the branches array based on what is read in, or not read in from the file
branches = new Preset[15];
for (int i = 0; i < branches.length; i++){
branches[i] = new Preset();
branches[i].setEnabled(false);
}
}
public class Preset extends JPanel{
private JTextField eName;
private JButton edit;
private JButton activate;
private JComboBox printer;
private JPanel line1;
private JPanel line2;
private String branchName;
private String ipAddress;
private boolean enableAll;
public Preset(){
eName = new JTextField(20);
edit = new JButton("Edit");
activate = new JButton("Activate");
JPanel nameContainer = new JPanel();
nameContainer.setLayout(new FlowLayout());
nameContainer.add(eName);
printer = new JComboBox();
//
line1 = new JPanel();
line1.setLayout(new FlowLayout());
line1.add(nameContainer);
line1.add(edit);
line1.add(activate);
//
line2 = new JPanel();
line2.setLayout(new BorderLayout());
line2.add(printer, BorderLayout.WEST);
GridBagLayout grid = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();
cons.fill = GridBagConstraints.BOTH;
this.setLayout(grid);
cons.ipady = 100;
cons.ipadx = 100;
cons.weighty = 0D;
cons.gridx = 0;
cons.gridy = 0;
cons.gridwidth = 2;
cons.gridheight = 1;
grid.setConstraints(line1, cons);
this.add(line1);
cons.ipady = 100;
cons.ipadx = 100;
cons.weighty = 0D;
cons.gridx = 0;
cons.gridy = 1;
cons.gridwidth = 2;
cons.gridheight = 1;
grid.setConstraints(line2, cons);
this.add(line2);
//Enable all components
enableAll = true;
}
}
}
당신이보고 싶은 것을 보여주는 이미지를 조롱 할 작은 도움이? 나는 당신이하려는 것을 당신의 설명에서 이해하지 못합니다. – tuckermi
게시물을 업데이트하여 도움이되기를 바랍니다. – Kryptos
SSCCE 코드를 업데이트했습니다 – Kryptos