我有一个 jframe 和 4 个带 gridbaglayout 的 jpanel。我想通过单击按钮从 jpanel 切换到另一个。但我没有得到第一个面板,而是得到了最后一个面板。 这就是我所做的:
public static void main(String args[]){
JFrame myframe = new JFrame();
try {
contentPane = new BackgroundPanel();
contentPane.setLayout(new GridBagLayout());
contentPane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
//principalpane.setBackground(Color.cyan);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
principalpane = new BackgroundPanel();
principalpane.setLayout(new GridBagLayout());
principalpane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
//principalpane.setBackground(Color.cyan);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
optionspane = new BackgroundPanel();
optionspane.setLayout(new GridBagLayout());
optionspane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
//principalpane.setBackground(Color.cyan);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
datapane = new BackgroundPanel();
datapane.setLayout(new GridBagLayout());
datapane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
//principalpane.setBackground(Color.cyan);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
genpane = new BackgroundPanel();
genpane.setLayout(new GridBagLayout());
genpane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
//principalpane.setBackground(Color.cyan);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/***************************************************************************/
/*****************************First*****************************************/
GridBagConstraints gbc=new GridBagConstraints();
GridBagConstraints gbcl=new GridBagConstraints();
JLabel welcome=new JLabel("Optimizing & Visualizing Process Progression");
welcome.setFont(new Font("Tahoma", Font.BOLD, 14));
gbcl.gridx=0;
gbcl.gridy=0;
gbcl.gridwidth = 2;
gbcl.gridheight=1;
gbcl.insets=new Insets(10,200,5,100);
gbcl.fill=GridBagConstraints.BOTH;
principalpane.add(welcome, gbcl);
gbc.gridx=0;
gbc.gridy=1;
gbc.weightx = 1;
gbc.weighty = 1;
// gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.fill=GridBagConstraints.BOTH;
gbc.insets=new Insets(10,10,10,10);
try {
principalpane.add(Mypanel.secondpane(), gbc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gbc.gridx=1;
gbc.gridy=1;
// gbc.anchor=GridBagConstraints.NORTHEAST;
gbc.fill=GridBagConstraints.BOTH;
gbc.insets=new Insets(10,10,10,10);
try {
principalpane.add(Mypanel.firstpanel(), gbc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gbc.gridx=0;
gbc.gridy=2;
gbc.gridwidth = 2;
gbc.gridheight=2;
// gbc.anchor=GridBagConstraints.SOUTH;
gbc.fill=GridBagConstraints.BOTH;
gbc.insets=new Insets(0,10,5,10);
try {
principalpane.add(Mypanel.thirdpanel(), gbc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/******************Second ***************************/
GridBagConstraints ogbc=new GridBagConstraints();
GridBagConstraints ogbcl=new GridBagConstraints();
JLabel optwelcome=new JLabel("Abnormal Process Progression");
optwelcome.setFont(new Font("Ubuntu", Font.BOLD, 16));
ogbcl.gridx=0;
ogbcl.gridy=0;
ogbcl.gridwidth = 2;
ogbcl.gridheight=1;
ogbcl.insets=new Insets(10,280,5,10);
ogbcl.fill=GridBagConstraints.BOTH;
optionspane.add(optwelcome, ogbcl);
gbc.gridx=0;
ogbc.gridy=1;
ogbc.weightx = 1;
ogbc.weighty = 1;
// gbc.anchor=GridBagConstraints.NORTHWEST;
ogbc.fill=GridBagConstraints.BOTH;
ogbc.insets=new Insets(10,10,10,10);
try {
optionspane.add(Mypanel.forthpanel(), ogbc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ogbc.gridx=1;
ogbc.gridy=1;
ogbc.weightx = 1;
ogbc.weighty = 1;
ogbcl.gridwidth = 1;
ogbc.gridheight=2;
// gbc.anchor=GridBagConstraints.NORTHWEST;
ogbc.fill=GridBagConstraints.BOTH;
ogbc.insets=new Insets(10,10,10,10);
try {
optionspane.add(Mypanel.sixthpanel(), ogbc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ogbc.gridx=0;
ogbc.gridy=2;
ogbc.weightx = 1;
ogbc.weighty = 1;
ogbc.gridheight=1;
// gbc.anchor=GridBagConstraints.NORTHWEST;
ogbc.fill=GridBagConstraints.BOTH;
ogbc.insets=new Insets(10,10,10,10);
try {
optionspane.add(Mypanel.fifthpanel(), ogbc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ogbc.gridx=0;
ogbc.gridy=3;
ogbc.weightx = 1;
ogbc.weighty = 1;
ogbc.gridwidth=2;
// gbc.anchor=GridBagConstraints.NORTHWEST;
ogbc.fill=GridBagConstraints.BOTH;
ogbc.insets=new Insets(10,10,10,10);
try {
optionspane.add(Mypanel.seventhpanel(), ogbc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*************************Third*************************************/
GridBagConstraints dgbc=new GridBagConstraints();
GridBagConstraints dgbcl=new GridBagConstraints();
JLabel datawelcome=new JLabel("Please Enter the QoS' values");
datawelcome.setFont(new Font("Ubuntu", Font.BOLD, 16));
dgbcl.gridx=0;
dgbcl.gridy=0;
dgbcl.gridwidth = 1;
dgbcl.gridheight=1;
dgbcl.anchor=GridBagConstraints.NORTH;
dgbcl.insets=new Insets(0,250,35,10);
dgbcl.fill=GridBagConstraints.BOTH;
datapane.add(datawelcome, dgbcl);
dgbc.gridx=0;
dgbc.gridy=0;
dgbc.weightx = 1;
dgbc.weighty = 1;
dgbcl.anchor=GridBagConstraints.CENTER;
dgbc.fill=GridBagConstraints.BOTH;
dgbc.insets=new Insets(5,10,10,5);
try {
datapane.add(Mypanel.eighthpanel(), dgbc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dgbc.gridx=0;
dgbc.gridy=1;
gbc.anchor=GridBagConstraints.SOUTH;
dgbc.fill=GridBagConstraints.BOTH;
dgbc.insets=new Insets(5,0,5,5);
try {
datapane.add(Mypanel.ninethpanel(), dgbc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/****************Fourth*************************************/
GridBagConstraints ggbc=new GridBagConstraints();
ggbc.gridx=0;
ggbc.gridy=0;
ggbc.weightx = 1;
ggbc.weighty = 1;
ggbc.anchor=GridBagConstraints.CENTER;
ggbc.fill=GridBagConstraints.BOTH;
ggbc.insets=new Insets(5,10,10,5);
try {
genpane.add(Mypanel.tenthpanel(), ggbc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ggbc.gridx=0;
ggbc.gridy=1;
ggbc.anchor=GridBagConstraints.SOUTH;
ggbc.fill=GridBagConstraints.BOTH;
ggbc.insets=new Insets(5,0,5,5);
try {
genpane.add(Mypanel.eleventhpanel(), ggbc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GridBagConstraints cgbc=new GridBagConstraints();
cgbc.gridx=0;
cgbc.gridy=0;
cgbc.weightx = 1;
cgbc.weighty = 1;
cgbc.anchor=GridBagConstraints.CENTER;
cgbc.fill=GridBagConstraints.BOTH;
principalpane.setVisible(true);
contentPane.add(principalpane, cgbc);
cgbc.gridx=0;
cgbc.gridy=0;
cgbc.weightx = 1;
cgbc.weighty = 1;
cgbc.anchor=GridBagConstraints.CENTER;
cgbc.fill=GridBagConstraints.BOTH;
optionspane.setVisible(false);
contentPane.add(optionspane, cgbc);
cgbc.gridx=0;
cgbc.gridy=0;
cgbc.weightx = 1;
cgbc.weighty = 1;
cgbc.anchor=GridBagConstraints.CENTER;
cgbc.fill=GridBagConstraints.BOTH;
datapane.setVisible(false);
contentPane.add(datapane, cgbc);
cgbc.gridx=0;
cgbc.gridy=0;
cgbc.weightx = 1;
cgbc.weighty = 1;
cgbc.anchor=GridBagConstraints.CENTER;
cgbc.fill=GridBagConstraints.BOTH;
genpane.setVisible(false);
contentPane.add(genpane, cgbc);
contentPane.setVisible(true);
myframe.getContentPane().add(contentPane);
myframe.pack();
myframe.setLocationRelativeTo(null);
myframe.setSize(new Dimension(800, 600));
myframe.setVisible(true);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
最佳答案
代码太多,无法理解您在做什么,但 GridBagLayout 用于组件不会动态更改的静态布局。
I want to switch from a jpanel to another with a click on a button.
对我来说,你应该使用 Card Layout一次只显示一个面板,并允许您交换面板。
教程示例展示了如何在生成 ActionEvent 时交换面板。
关于java - 如何在 jframe 上切换 gridbaglayout 面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31322572/