gpt4 book ai didi

Java:GridLayout垂直元素对齐

转载 作者:行者123 更新时间:2023-11-30 02:59:04 25 4
gpt4 key购买 nike

我正在尝试将 JPanel p1 的第一行垂直居中。其中的元素是一个 JLabelJTextField 和另一个带有 GridLayoutJPanel,当窗口拉伸(stretch)时元素停留在它之上,我该怎么做才能实现我想要的?

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

public class grafica extends JFrame{

private JPanel p1,p2,p3;
private JButton aggiungi, affitta, reso, cerca, stampa;
private JTextArea a1;
private JTextField n1;
private JLabel l1;

public grafica(){
//creating panels
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p1.setLayout(new GridLayout(2,1));
p3.setLayout(new GridLayout(5,1));

//creating elements
aggiungi=new JButton("Aggiungi");
affitta=new JButton("Affitta");
reso=new JButton("Reso");
cerca=new JButton("Cerca");
stampa=new JButton("Stampa");
n1=new JTextField(10);
l1=new JLabel("Taglia: ");

//adding elements
p2.add(l1);
p2.add(n1);
p3.add(aggiungi);
p3.add(affitta);
p3.add(reso);
p3.add(cerca);
p3.add(stampa);
p2.add(p3);
p1.add(p2);

a1=new JTextArea(20,20);
a1.setEditable(false);
p1.add(a1);
p1.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.red));
p2.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.blue));

p3.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.green));

this.setVisible(true);
this.getContentPane().add(p1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
}

这是我想要的结果: intended result

最佳答案

使用另一个面板容纳p2,并将新面板的布局设置为BoxLayout

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

public class grafica extends JFrame {

private JPanel p1, p2, p3, p4;
private JButton aggiungi, affitta, reso, cerca, stampa;
private JTextArea a1;
private JTextField n1;
private JLabel l1;

public grafica() {
// creating panels
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p1.setLayout(new GridLayout(2, 1));
p3.setLayout(new GridLayout(5, 1));

// creating elements
aggiungi = new JButton("Aggiungi");
affitta = new JButton("Affitta");
reso = new JButton("Reso");
cerca = new JButton("Cerca");
stampa = new JButton("Stampa");
n1 = new JTextField(10);
l1 = new JLabel("Taglia: ");

// adding elements
p2.add(l1);
p2.add(n1);
p3.add(aggiungi);
p3.add(affitta);
p3.add(reso);
p3.add(cerca);
p3.add(stampa);
p2.add(p3);

// added part
JPanel p2Holder = new JPanel();
p2Holder.setLayout(new BoxLayout(p2Holder, BoxLayout.Y_AXIS));
p2Holder.add(Box.createVerticalGlue());
p2Holder.add(p2);
p2Holder.add(Box.createVerticalGlue());

p1.add(p2Holder);

a1 = new JTextArea(20, 20);
a1.setEditable(false);
p1.add(a1);
p1.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.red));
p2.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.blue));

p3.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.green));

this.setVisible(true);
this.getContentPane().add(p1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}

public static void main(String[] args) {
new grafica();
}
}

关于Java:GridLayout垂直元素对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36420389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com