【发布时间】:2016-03-24 09:50:57
【问题描述】:
我正在尝试创建用户界面,但我无法创建有序组件。如何像第二张图片一样订购我的 JPanel 组件,有什么想法吗?
这是我的java代码
import javax.swing.*;
import java.awt.*;
public class ApplicationWindow2 {
private JFrame mainJFrame;
private JPanel topPanel;
private JPanel centerPanel;
private JPanel bottomPanel;
public ApplicationWindow2(){
mainJFrame = new JFrame("setupbox");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
mainJFrame.setMaximumSize(new Dimension(600,600));
mainJFrame.setMinimumSize(new Dimension((int) mainJFrame.getMaximumSize().getWidth()-1,(int) mainJFrame.getMaximumSize().getHeight()-1));
mainJFrame.setResizable(false);
mainJFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
int windowHeight = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
int windowWidth = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
mainJFrame.setLocation((windowWidth-mainJFrame.getWidth())/2, (windowHeight-mainJFrame.getHeight())/2);//
// components
mainJFrame.getContentPane().add(getTopPanel(), BorderLayout.PAGE_START);
mainJFrame.getContentPane().add(getCenterPanel(), BorderLayout.CENTER);
mainJFrame.getContentPane().add(getBottomPanel(), BorderLayout.PAGE_END);
mainJFrame.setVisible(true);
mainJFrame.pack();
}
private JPanel getTopPanel(){
if (topPanel == null){
topPanel = new JPanel();
topPanel.add(new JLabel("top panel"));
}
return topPanel;
}
private JPanel getBottomPanel(){
if(bottomPanel == null){
bottomPanel = new JPanel();
bottomPanel.add(new Label("bottom panel"));
}
return bottomPanel;
}
private JPanel getCenterPanel(){
if(centerPanel == null){
centerPanel = new JPanel();
centerPanel.setSize(new Dimension((int) mainJFrame.getMaximumSize().getWidth()-100,(int) mainJFrame.getMaximumSize().getHeight()-100));
centerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
BoxLayout boxLayout = new BoxLayout(centerPanel, BoxLayout.Y_AXIS);
centerPanel.setLayout(boxLayout);
centerPanel.add(getTest());
centerPanel.add(getTest1());
centerPanel.add(Box.createVerticalStrut(30));
centerPanel.add(getTest());
}
return centerPanel;
}
private JPanel getTest(){
JPanel testPanel = new JPanel(new GridBagLayout());
testPanel.setMaximumSize(new Dimension(getCenterPanel().getWidth(), 40));
testPanel.setBorder(BorderFactory.createLineBorder(Color.black));
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
c.gridy = 0;
c.insets = new Insets(5, 5, 5, 5);
JCheckBox box1 = new JCheckBox("asdasdasdasdasdasd");
c.gridx = 0;
c.anchor = GridBagConstraints.WEST;
testPanel.add(box1, c);
JLabel l3 = new JLabel("lbl3asdssssssssssssssssssssssssss");
l3.setPreferredSize(new Dimension(20,30));
l3.setBorder(BorderFactory.createLineBorder(Color.black));
c.gridx = 1;
c.anchor = GridBagConstraints.LINE_END;
testPanel.add(l3, c);
return testPanel;
}
private JPanel getTest1(){
JPanel testPanel = new JPanel(new GridBagLayout());
testPanel.setMaximumSize(new Dimension(getCenterPanel().getWidth(), 40));
testPanel.setBorder(BorderFactory.createLineBorder(Color.black));
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NONE;
c.gridy = 0;
c.insets = new Insets(5, 5, 5, 5);
JCheckBox box1 = new JCheckBox("aass1133");
c.gridx = 0;
c.anchor = GridBagConstraints.LINE_START;
testPanel.add(box1, c);
JLabel l3 = new JLabel("lbl3as");
l3.setPreferredSize(new Dimension(20,30));
l3.setBorder(BorderFactory.createLineBorder(Color.black));
c.gridx = 1;
c.anchor = GridBagConstraints.LINE_END;
testPanel.add(l3, c);
return testPanel;
}
}
【问题讨论】:
-
JTable?MigLayout? -
我们可以在 migLayout 中设置单元格的不同大小吗?以及我们如何导入intellij?看起来不错
-
我个人没有用过
MigLayout,但有人建议它可以解决这类问题。基本上,您需要下载 Jar 文件并将其包含在您的项目中。也不熟悉intellij:P -
感谢您的建议,我会试试看:D
-
有一个 MigLayout Demo 。其中,有几个示例如何设置单元格大小。例如,您可以使用某个组件的百分比、使用固定的单元格大小等。
标签: java swing layout-manager grid-layout gridbaglayout