Java 增加Gridlayout中的行大小
上面的框架包含两行gridlayout。第一行是文本区域,第二行是带有两个复选框的面板。我想增加第一排的高度,第一排应该是总高度的75%,第二排应该是25%。我该怎么做?以下是我的代码片段:Java 增加Gridlayout中的行大小,java,swing,grid-layout,Java,Swing,Grid Layout,上面的框架包含两行gridlayout。第一行是文本区域,第二行是带有两个复选框的面板。我想增加第一排的高度,第一排应该是总高度的75%,第二排应该是25%。我该怎么做?以下是我的代码片段: setLayout(new GridLayout(2, 0, 0, 0)); Panel text_panel = new Panel(); add(text_panel); text_panel.setLayout(new GridLayout(1, 0, 0,
setLayout(new GridLayout(2, 0, 0, 0));
Panel text_panel = new Panel();
add(text_panel);
text_panel.setLayout(new GridLayout(1, 0, 0, 0));
JTextArea textArea = new JTextArea();
textArea.setText("text to be displayed");
JScrollPane scroll = new JScrollPane (textArea);
text_panel.add(scroll);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
Border border = BorderFactory.createLineBorder(Color.GRAY);
textArea.setBorder(border);
textArea.setFont(new Font("Arial",Font.PLAIN,12));
textArea.setCaretPosition(0);
textArea.requestFocus();
Panel checebox_panel = new Panel();
checebox_panel.setBackground(Color.WHITE);
add(checebox_panel);
checebox_panel.setLayout(new GridLayout(1, 0, 0, 0));
androidCheckBox = new JCheckBox("Open start page");
androidCheckBox.setBackground(Color.WHITE);
androidCheckBox.addItemListener(itemListener);
androidCheckBox.setSelected(true);
checebox_panel.add(androidCheckBox);
eclipseCheckBox = new JCheckBox("register for updates");
eclipseCheckBox.setBackground(Color.WHITE);
eclipseCheckBox.addItemListener(itemListener);
eclipseCheckBox.setSelected(true);
checebox_panel.add(eclipseCheckBox);
这在GridLayout中是不可能的。GridLayout将始终使用均匀间距。查看另一个布局管理器 这里有一个很好的参考:
这在GridLayout中是不可能的。GridLayout将始终使用均匀间距。查看另一个布局管理器 这里有一个很好的参考:
使用GridLayout,您不能有两个大小不同的行。看看盒子的布局。大概是这样的:
JPanel content = new JPanel();
frame.getContentPane().add(content);
LayoutManager layout = new BoxLayout(content, BoxLayout.Y_AXIS);
Box boxes[] = new Box[2];
boxes[0] = Box.createHorizontalBox();
boxes[1] = Box.createHorizontalBox();
boxes[0].createGlue();
boxes[1].createGlue();
content.add(boxes[0]);
content.add(boxes[1]);
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
panel.setPreferredSize(new Dimension(500,300));
panel2.setPreferredSize(new Dimension(500,200));
boxes[0].add(panel);
boxes[1].add(panel2);
使用
setPreferredSize
从来都不是最佳选择,但它确实有效。这只是一个你如何做到这一点的例子,我相信还有更好的方法 使用GridLayout,您不能有两个大小不同的行。看看盒子的布局。大概是这样的:
JPanel content = new JPanel();
frame.getContentPane().add(content);
LayoutManager layout = new BoxLayout(content, BoxLayout.Y_AXIS);
Box boxes[] = new Box[2];
boxes[0] = Box.createHorizontalBox();
boxes[1] = Box.createHorizontalBox();
boxes[0].createGlue();
boxes[1].createGlue();
content.add(boxes[0]);
content.add(boxes[1]);
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
panel.setPreferredSize(new Dimension(500,300));
panel2.setPreferredSize(new Dimension(500,200));
boxes[0].add(panel);
boxes[1].add(panel2);
使用setPreferredSize
从来都不是最佳选择,但它确实有效。这只是一个你如何做到这一点的例子,我相信还有更好的方法 “在Gridlayout中增加行大小”我遇到了这个要求,通过尝试设置一个解决方案,我得到了一个,尝试了border而不是Gridlayout。这可能会有帮助:)
代码如下:
import ComponentMeta.RequiredComp;
import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PromptPopup extends JDialog {
private final JScrollPane scrollPane = new JScrollPane();
private int rows;
private int cols;
private int vGap;
private int hGap;
private Map<String, Component> componentRepo;
public PromptPopup(JFrame parent) {
super(parent);
componentRepo = new HashMap<>();
this.setModalityType(ModalityType.APPLICATION_MODAL);
}
public void setComponentsDisplayStyle(int rows, int cols, int vGap, int hGap) {
this.rows = rows;
this.cols = cols;
this.vGap = vGap;
this.hGap = hGap;
}
public void setComponentReop() {
JTextField dynamicParamTextField = new JTextField();
this.componentRepo.put("COMPANY_CODE", dynamicParamTextField);
JTextField dynamicParamTextField2 = new JTextField();
this.componentRepo.put("DIST_CODE", dynamicParamTextField2);
JTextField dynamicParamTextField3 = new JTextField();
this.componentRepo.put("LOCA_CODE", dynamicParamTextField3);
JTextField dynamicParamTextField4 = new JTextField();
this.componentRepo.put("TOKEN_EXEC", dynamicParamTextField4);
}
public void initPopupUI() {
//Setting content panes layout
getContentPane().setLayout(new BorderLayout(0, 0));
//Creating a root panel(root container) to hold the child components
JPanel rootContainer = new JPanel();
rootContainer.setLayout(new BorderLayout());
//Creating header panel(header container) to hold the header components
JPanel header = new JPanel();
header.setLayout(new FlowLayout());
JLabel headerText = new JLabel("Source query parameters required ");
headerText.setForeground(Color.WHITE);
header.add(headerText);
header.setBackground(Color.BLUE);
//Creating footer panel(footer container ) to hold the footer components
JPanel footer = new JPanel();
footer.setLayout(new FlowLayout());
JButton executeWithParamsButton = new JButton("Execute with params");
executeWithParamsButton.setBackground(Color.BLACK);
executeWithParamsButton.setForeground(Color.WHITE);
JButton cancelButton = new JButton("Cancel");
cancelButton.setBackground(Color.RED);
cancelButton.setForeground(Color.WHITE);
footer.add(executeWithParamsButton);
footer.add(cancelButton);
footer.setBackground(Color.BLUE);
//Creating content panel(content container) to hold the all dynamically generated components
JPanel contentContainer = new JPanel();
GridLayout gridLayout = new GridLayout(this.rows, this.cols, this.hGap, this.vGap);
contentContainer.setLayout(gridLayout);
for (Map.Entry entry : componentRepo.entrySet()) {
JLabel dynamicParamLabel = new JLabel(entry.getKey().toString());
contentContainer.add(dynamicParamLabel);
contentContainer.add((Component) entry.getValue());
}
// Adding all the created containers to the root container one by one
rootContainer.add(header, BorderLayout.NORTH);
rootContainer.add(contentContainer, BorderLayout.CENTER);
rootContainer.add(footer, BorderLayout.SOUTH);
//Adding the root container to the scroll pane in order the view to be scrollable nno matter how many components are there.
scrollPane.setViewportView(rootContainer);
getContentPane().add(scrollPane);
}
}
导入组件meta.RequiredComp;
导入javax.swing.*;
导入java.awt.*;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
公共类PromptPopup扩展JDialog{
私有最终JScrollPane scrollPane=新JScrollPane();
私有int行;
私人公司;
私营部门;
私人int hGap;
私有地图组件repo;
公共PromptPopup(JFrame父级){
超级(家长);
componentRepo=新HashMap();
this.setModalityType(ModalityType.APPLICATION_MODAL);
}
公共void集合组件显示样式(int行、int列、int vGap、int hGap){
this.rows=行;
this.cols=cols;
this.vGap=vGap;
this.hGap=hGap;
}
public void setComponentRep(){
JTextField dynamicParamTextField=新的JTextField();
this.componentRepo.put(“公司代码”,dynamicParamTextField);
JTextField dynamicParamTextField2=新的JTextField();
这个.componentRepo.put(“DIST_代码”,dynamicParamTextField2);
JTextField dynamicParamTextField3=新的JTextField();
此.componentRepo.put(“LOCA_代码”,dynamicParamTextField3);
JTextField dynamicParamTextField4=新的JTextField();
this.componentRepo.put(“TOKEN_EXEC”,dynamicParamTextField4);
}
public void initpopupuui(){
//设置内容窗格布局
getContentPane().setLayout(新的BorderLayout(0,0));
//创建根面板(根容器)以容纳子组件
JPanel rootContainer=新的JPanel();
setLayout(新的BorderLayout());
//创建收割台面板(收割台容器)以容纳收割台组件
JPanel头=新的JPanel();
header.setLayout(新的FlowLayout());
JLabel headerText=新的JLabel(“需要源查询参数”);
headerText.setForeground(颜色:白色);
header.add(headerText);
标题.立根背景(颜色.蓝色);
//创建页脚面板(页脚容器)以容纳页脚组件
JPanel footer=新的JPanel();
setLayout(新的FlowLayout());
JButton executeWithParamsButton=新JButton(“使用参数执行”);
executeWithParamsButton.setBackground(颜色:黑色);
executeWithParamsButton.setForeground(颜色.白色);
JButton cancelButton=新JButton(“取消”);
取消按钮。设置背景(颜色。红色);
取消按钮。设置前景(颜色。白色);
footer.add(executeWithParamsButton);
页脚.添加(取消按钮);
页脚.收进背景(颜色.蓝色);
//创建内容面板(内容容器)以容纳所有动态生成的组件
JPanel contentContainer=新的JPanel();
GridLayout GridLayout=新的GridLayout(this.rows、this.cols、this.hGap、this.vGap);
setLayout(gridLayout);
对于(Map.Entry:componentRepo.entrySet()){
JLabel dynamicParamLabel=newjlabel(entry.getKey().toString());
contentContainer.add(dynamicParamLabel);
添加((组件)条目.getValue());
}
//将所有创建的容器逐个添加到根容器
添加(标题,BorderLayout.NORTH);
添加(contentContainer,BorderLayout.CENTER);
添加(页脚,BorderLayout.SOUTH);
//将根容器添加到滚动窗格以使视图可滚动,无论有多少组件。
scrollPane.setViewportView(rootContainer);
getContentPane().add(滚动窗格);
}
}
这里是我想要的输出,即页眉位于顶部,仅基于其组件消耗空间,中间面板和包含按钮控件的页脚也是如此
“在Gridlayout中增加行大小”我遇到了这个要求,通过尝试设置解决方案,我得到了一个,尝试了border而不是grid layout。这可能会有帮助:)
代码如下:
import ComponentMeta.RequiredComp;
import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PromptPopup extends JDialog {
private final JScrollPane scrollPane = new JScrollPane();
private int rows;
private int cols;
private int vGap;
private int hGap;
private Map<String, Component> componentRepo;
public PromptPopup(JFrame parent) {
super(parent);
componentRepo = new HashMap<>();
this.setModalityType(ModalityType.APPLICATION_MODAL);
}
public void setComponentsDisplayStyle(int rows, int cols, int vGap, int hGap) {
this.rows = rows;
this.cols = cols;
this.vGap = vGap;
this.hGap = hGap;
}
public void setComponentReop() {
JTextField dynamicParamTextField = new JTextField();
this.componentRepo.put("COMPANY_CODE", dynamicParamTextField);
JTextField dynamicParamTextField2 = new JTextField();
this.componentRepo.put("DIST_CODE", dynamicParamTextField2);
JTextField dynamicParamTextField3 = new JTextField();
this.componentRepo.put("LOCA_CODE", dynamicParamTextField3);
JTextField dynamicParamTextField4 = new JTextField();
this.componentRepo.put("TOKEN_EXEC", dynamicParamTextField4);
}
public void initPopupUI() {
//Setting content panes layout
getContentPane().setLayout(new BorderLayout(0, 0));
//Creating a root panel(root container) to hold the child components
JPanel rootContainer = new JPanel();
rootContainer.setLayout(new BorderLayout());
//Creating header panel(header container) to hold the header components
JPanel header = new JPanel();
header.setLayout(new FlowLayout());
JLabel headerText = new JLabel("Source query parameters required ");
headerText.setForeground(Color.WHITE);
header.add(headerText);
header.setBackground(Color.BLUE);
//Creating footer panel(footer container ) to hold the footer components
JPanel footer = new JPanel();
footer.setLayout(new FlowLayout());
JButton executeWithParamsButton = new JButton("Execute with params");
executeWithParamsButton.setBackground(Color.BLACK);
executeWithParamsButton.setForeground(Color.WHITE);
JButton cancelButton = new JButton("Cancel");
cancelButton.setBackground(Color.RED);
cancelButton.setForeground(Color.WHITE);
footer.add(executeWithParamsButton);
footer.add(cancelButton);
footer.setBackground(Color.BLUE);
//Creating content panel(content container) to hold the all dynamically generated components
JPanel contentContainer = new JPanel();
GridLayout gridLayout = new GridLayout(this.rows, this.cols, this.hGap, this.vGap);
contentContainer.setLayout(gridLayout);
for (Map.Entry entry : componentRepo.entrySet()) {
JLabel dynamicParamLabel = new JLabel(entry.getKey().toString());
contentContainer.add(dynamicParamLabel);
contentContainer.add((Component) entry.getValue());
}
// Adding all the created containers to the root container one by one
rootContainer.add(header, BorderLayout.NORTH);
rootContainer.add(contentContainer, BorderLayout.CENTER);
rootContainer.add(footer, BorderLayout.SOUTH);
//Adding the root container to the scroll pane in order the view to be scrollable nno matter how many components are there.
scrollPane.setViewportView(rootContainer);
getContentPane().add(scrollPane);
}
}
导入组件meta.RequiredComp;
导入javax.swing.*;
导入java.awt.*;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
公共类PromptPopup扩展JDialog{
私有最终JScrollPane scrollPane=新JScrollPane();
私有int行;
私人公司;
私营部门;
私人int hGap;
私有地图组件repo;
公共PromptPopup(JFrame父级){
超级(家长);
componentRepo=新HashMap();
this.setModalityType(ModalityType.APPLICATION_MODAL);
}