我怎样才能在Java中实现这种可扩展的布局?灵活的BoxLayout等 [英] How would I be able to achieve this expandable layout in Java? Flexible BoxLayout etc
问题描述
我希望能够有三个JPanels p1 p2和p3,并让它们像这样布局:
我一直在玩FlowLayout,BoxLayout等,但我不确定我是否朝着正确的方向前进。我对Java很新,所以如果我很诚实,我不知道会发生什么。
我喜欢BoxLayout如何工作,调整面板大小,但我会喜欢能给它一些宽度属性。
我没有使用视觉设计师,这是我目前的窗口代码:
private void initialize(){
Dimension dimensions = Toolkit.getDefaultToolkit()。getScreenSize();
frame = new JFrame();
frame.setLayout(new GridLayout(1,3));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(dimensions.width / 2 - WINDOW_WIDTH / 2,
dimensions.height / 2 - WINDOW_HEIGHT / 2,
WINDOW_WIDTH,WINDOW_HEIGHT);
JPanel p1 = new JPanel(new BorderLayout());
p1.setBackground(Color.red);
JPanel p2 = new JPanel(new BorderLayout());
p2.setBackground(Color.black);
JPanel p3 = new JPanel(new BorderLayout());
p3.setBackground(Color.blue);
frame.add(p2);
frame.add(p1);
frame.add(p3);
}
任何指针赞赏!
编辑:感谢mKorbel,我已经设法让它按照我想要的方式工作。正确的列没有完全按照我的要求进行布局,但实际上我改变了主意并决定保留其他布局。
代码:
import java.awt。*;
import javax.swing。*;
公共类PanelWindow扩展JFrame {
private static final long serialVersionUID = 1L;
private static final int WINDOW_HEIGHT = 600;
private static final int WINDOW_WIDTH = 800;
私人JPanel p1,p2,p3;
public static void main(String [] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
PanelWindow panelWindow = new PanelWindow();
}
});
}
public PanelWindow(){
initialize();
}
private void initialize(){
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
p1 = new JPanel();
p1.setBackground(Color.red);
p2 = new JPanel();
p2.setBackground(Color.green);
p3 = new JPanel();
p3.setBackground(Color.blue);
gbc.gridx = gbc.gridy = 0;
gbc.gridwidth = gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = gbc.weighty = 97;
gbc.insets = new Insets(2,2,2,2);
add(p1,gbc);
gbc.gridy = 1;
gbc.weightx = gbc.weighty = 3;
gbc.insets = new Insets(2,2,2,2);
add(p2,gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.weightx = 20;
gbc.insets = new Insets(2,2,2,2);
add(p3,gbc);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
维度维度= Toolkit.getDefaultToolkit()。getScreenSize();
setBounds(dimensions.width / 2 - WINDOW_WIDTH / 2,
dimensions.height / 2 - WINDOW_HEIGHT / 2,
WINDOW_WIDTH,WINDOW_HEIGHT);
setTitle(面板窗口);
}
}
一个字: MigLayout 。如您所见,标准布局管理器可以实现。但我可以向你保证,使用MigLayout将布局代码减少到4行...(虽然它需要一些时间才能进入MigLayout,因为它的工作方式与通常的布局管理器不同)。
I would like to be able to have three JPanels p1 p2 and p3, and have them lay out like so:
I have been playing around with FlowLayout, BoxLayout etc but I'm not really sure if I am heading in the right direction. I am quite new with Java so I don't know what does what if I'm quite honest.
I like how BoxLayout works, resizing the panels, but I would like to be able to give it some sort of width attribute.
I am not using a visual designer for this, this is my window code at the moment:
private void initialize() {
Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame();
frame.setLayout(new GridLayout(1, 3));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(dimensions.width / 2 - WINDOW_WIDTH / 2,
dimensions.height / 2 - WINDOW_HEIGHT / 2,
WINDOW_WIDTH, WINDOW_HEIGHT);
JPanel p1 = new JPanel(new BorderLayout());
p1.setBackground(Color.red);
JPanel p2 = new JPanel(new BorderLayout());
p2.setBackground(Color.black);
JPanel p3 = new JPanel(new BorderLayout());
p3.setBackground(Color.blue);
frame.add(p2);
frame.add(p1);
frame.add(p3);
}
Any pointers appreciated!
EDIT: I have managed to get it to work how I wanted, thanks to mKorbel. The right column isn't laid out exactly as I was going to do it but I actually changed my mind and decided to keep the other layout.
The code:
import java.awt.*;
import javax.swing.*;
public class PanelWindow extends JFrame {
private static final long serialVersionUID = 1L;
private static final int WINDOW_HEIGHT = 600;
private static final int WINDOW_WIDTH = 800;
private JPanel p1, p2, p3;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
PanelWindow panelWindow = new PanelWindow();
}
});
}
public PanelWindow() {
initialize();
}
private void initialize() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
p1 = new JPanel();
p1.setBackground(Color.red);
p2 = new JPanel();
p2.setBackground(Color.green);
p3 = new JPanel();
p3.setBackground(Color.blue);
gbc.gridx = gbc.gridy = 0;
gbc.gridwidth = gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = gbc.weighty = 97;
gbc.insets = new Insets(2, 2, 2, 2);
add(p1, gbc);
gbc.gridy = 1;
gbc.weightx = gbc.weighty = 3;
gbc.insets = new Insets(2, 2, 2, 2);
add(p2, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.weightx = 20;
gbc.insets = new Insets(2, 2, 2, 2);
add(p3, gbc);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(dimensions.width / 2 - WINDOW_WIDTH / 2,
dimensions.height / 2 - WINDOW_HEIGHT / 2,
WINDOW_WIDTH, WINDOW_HEIGHT);
setTitle("Panel Window");
}
}
One word: MigLayout. As you have seen, it is possible with the standard layout managers. But I can guarantee you, the layout code would be reduced to 4 lines with MigLayout... (Although it would take you some time to get into MigLayout as it works a bit different than the usual layout managers).
这篇关于我怎样才能在Java中实现这种可扩展的布局?灵活的BoxLayout等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!