一个帧中可以连续有5个JPanels
吗?这是我的代码,您可能会更好地理解这个问题:
setLayout(new BorderLayout());
add(mainOrderInfo, BorderLayout.WEST);
add(meatPanel, BorderLayout.WEST);
add(cheesePanel, BorderLayout.CENTER);
add(ingrediantsPanel, BorderLayout.EAST);
add(addonsPanel, BorderLayout.EAST);
运行代码后,我得到的只是
meatPanel
,cheesePanel
和ingrediantsPanel
。我希望mainOrderInfo
在meatPanel
的左侧,而addonsPanel
在ingredientsPanel
的右侧。我觉得这很简单,只是我不知道的代码或代码顺序问题。 最佳答案
GridLayout
有关更多详细信息,请参见How to Use GridLayout
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridLayout(1, 5));
add(createPanel(Color.WHITE));
add(createPanel(Color.BLACK));
add(createPanel(Color.RED));
add(createPanel(Color.GREEN));
add(createPanel(Color.BLUE));
}
protected JPanel createPanel(Color color) {
JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
panel.setBackground(color);
return panel;
}
}
}
GridBagLayout
有关更多详细信息,请参见How to Use GridBagLayout
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(createPanel(Color.WHITE), gbc);
gbc.gridx++;
add(createPanel(Color.BLACK), gbc);
gbc.gridx++;
add(createPanel(Color.RED), gbc);
gbc.gridx++;
add(createPanel(Color.GREEN), gbc);
gbc.gridx++;
add(createPanel(Color.BLUE), gbc);
}
protected JPanel createPanel(Color color) {
JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
panel.setBackground(color);
return panel;
}
}
}