我正在尝试创建用户界面,但我无法做有序组件。如何像第二张图片那样订购我的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;
}
}
答案 0 :(得分:0)
private JPanel getTest1(){
MigLayout layout = new MigLayout("debug");
JPanel content = new JPanel(layout);
content.setMaximumSize(new Dimension(getCenterPanel().getWidth(), 40));
JCheckBox a1 = new JCheckBox("d");
content.add(a1, "gapright 10, height 30, width 250, cell 0 0");
JLabel label = new JLabel();
label.setBackground(Color.RED);
label.setOpaque(true);
content.add(label, "gapright 10, height 30, width 30, cell 1 0");
JButton btn = new JButton("try again");
content.add(btn,"height 30, width 80, cell 2 0" );
return content;
}