此代码使用卡布局,因为我想更改窗口显示而不是具有多个窗口(或帧(。因此,我想要多个面板,这似乎有效。
但是,在面板中,我想将Gridbag布局用于定位组件。但这不起作用!它充当流程布局。谁能帮我克服这个障碍?请。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class another
{
private JPanel contentPane;
private MyPanel panel1;
private MyPanel2 panel2;
private void displayGUI()
{
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new CardLayout());
panel1 = new MyPanel(contentPane);
panel2 = new MyPanel2();
contentPane.add(panel1, "Panel 1");
contentPane.add(panel2, "Panel 2");
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new another().displayGUI();
}
});
}
}
class MyPanel extends JPanel {
private JTextField How;
private JLabel jcomp2;
private JLabel jcomp3;
private JButton jcomp4;
private JPanel contentPane;
private JPanel myPanel1;
public MyPanel(JPanel panel)
{
contentPane = panel;
//construct components
How = new JTextField (1);
jcomp2 = new JLabel ("Label2");
jcomp3 = new JLabel ("Label3");
jcomp4 = new JButton ("openNewWindow");
myPanel1 = new JPanel();
//adjust size and set layout
setPreferredSize (new Dimension (600, 600));
setLayout (new GridBagLayout());
//set component bounds (only needed by Absolute Positioning)
/*
How.setBounds (245, 50, 60, 25);
jcomp2.setBounds (35, 30, 185, 50);
jcomp3.setBounds (250, 30, 60, 20);
jcomp4.setLocation(0, 0);
jcomp4.setSize(315, 25);
*/
insert(jcomp2, 0, 0, 1, 1);
insert(jcomp3, 0, 1, 1, 1);
insert(jcomp4, 1, 0, 1, 1);
jcomp4.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});
//add components
//add (How);
add (jcomp2);
add (jcomp3);
add (jcomp4);
}
public void insert(Component c, int gridX, int gridY, int gridW, int gridH)
{
GridBagConstraints constraint = new GridBagConstraints();
constraint.gridx = gridX;
constraint.gridy = gridY;
constraint.gridwidth = gridW;
constraint.gridheight = gridH;
constraint.anchor = GridBagConstraints.LINE_START;
myPanel1.add(c, constraint);
}
}
class MyPanel2 extends JPanel {
private JButton jcomp1;
private JButton jcomp2;
private JButton jcomp3;
private JTextField jcomp4;
public MyPanel2() {
//construct components
jcomp1 = new JButton ("test1");
jcomp2 = new JButton ("test2");
jcomp3 = new JButton ("test3");
jcomp4 = new JTextField (5);
//adjust size and set layout
setPreferredSize (new Dimension (395, 156));
setLayout (null);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (20, 45, 100, 25);
jcomp2.setBounds (135, 60, 100, 25);
jcomp3.setBounds (260, 35, 100, 25);
jcomp4.setBounds (105, 115, 100, 25);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
}
}
您看起来不必要地过于复杂。您可以创建一个名为ContentPane的Jpanel,并给它一个贴装,一切都很好,但是由于某种原因,您将contentpane jpanel传递到mypanel构造函数
JPanel contentPane = new JPanel();
contentPane.setLayout(new CardLayout());
panel1 = new MyPanel(contentPane);
panel2 = new MyPanel2();
然后在构造函数中您将组件分配给相同的ContentPane Jpanel - 但是为什么呢?您将gridbaglayout分配给mypanel this
实例,然后以gridbag的方式将组件添加到其内部的mypanel1变量中,而此jpanel具有jpanel jpanel默认default flowlayout:
class MyPanel extends JPanel {
// ....
private JPanel contentPane;
private JPanel myPanel1;
public MyPanel(JPanel panel) {
contentPane = panel;
// ....
myPanel1 = new JPanel();
setLayout (new GridBagLayout()); // you set *** this *** to GridBagLayout
insert(jcomp2, 0, 0, 1, 1);
insert(jcomp3, 0, 1, 1, 1);
insert(jcomp4, 1, 0, 1, 1);
add (jcomp2); // and then you add components to this without GridBagConstraints ?
add (jcomp3);
add (jcomp4);
}
public void insert(Component c, int gridX, int gridY, int gridW, int gridH) {
// ....
// but then add components in a GridBagLayout way into the myPanel1 JPanel???
myPanel1.add(c, constraint);
}
}
最好将mypanel设置为使用GridBaglayout:
// adjust size and set layout
setPreferredSize(new Dimension(600, 600));
// setLayout(new GridBagLayout());
myPanel1.setLayout(new GridBagLayout());
而不是this
再次,您似乎过于复杂的是简单的努力。
例如:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class SimpleCardExample extends JPanel {
private static final int EB_GAP = 8;
private CardLayout cardLayout = new CardLayout();
public SimpleCardExample() {
setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
setLayout(cardLayout);
add(new GridBagPanel(this), GridBagPanel.class.getSimpleName());
add(new NextPanel(this), NextPanel.class.getSimpleName());
}
public void nextCard() {
cardLayout.next(this);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("SimpleCardExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SimpleCardExample());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class GridBagPanel extends JPanel {
private static final int GAP = 6;
private static final Insets GBC_INSETS = new Insets(GAP, GAP, GAP, GAP);
public GridBagPanel(SimpleCardExample scExample) {
setLayout(new GridBagLayout());
add(new JLabel("Title Goes Here", SwingConstants.CENTER), createGbc(0, 0, 2, 1));
add(new JLabel("Name:"), createGbc(0, 1, 1, 1));
add(new JTextField(15), createGbc(1, 1, 1, 1));
add(new JLabel("Phone:"), createGbc(0, 2, 1, 1));
add(new JTextField(15), createGbc(1, 2, 1, 1));
add(new JLabel("Address:"), createGbc(0, 3, 1, 1));
add(new JTextField(15), createGbc(1, 3, 1, 1));
add(new JButton(new NextAction("Next", scExample)), createGbc(0, 4, 2, 1));
}
private GridBagConstraints createGbc(int x, int y, int w, int h) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
gbc.insets = GBC_INSETS;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = x == 0 ? GridBagConstraints.WEST : GridBagConstraints.EAST;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
return gbc;
}
}
class NextPanel extends JPanel {
public NextPanel(SimpleCardExample scExample) {
add(new JButton(new NextAction("Next", scExample)));
}
}
class NextAction extends AbstractAction {
private SimpleCardExample scExample;
public NextAction(String name, SimpleCardExample scExample) {
super(name);
this.scExample = scExample;
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
scExample.nextCard();
}
}