Java 一个框架内有2个JPanel。左面板是带有按钮的菜单。使用按钮切换右面板。
我试图写一个程序,其中有两个面板在任何时候。左侧面板将是一个带有按钮的菜单,而右侧面板将根据在任一面板中按下的按钮而变化Java 一个框架内有2个JPanel。左面板是带有按钮的菜单。使用按钮切换右面板。,java,swing,jframe,jpanel,containers,Java,Swing,Jframe,Jpanel,Containers,我试图写一个程序,其中有两个面板在任何时候。左侧面板将是一个带有按钮的菜单,而右侧面板将根据在任一面板中按下的按钮而变化 public class Window extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L; //Buttons JButton recipeRolodexButton = new JButton("Recipe Rolode
public class Window extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
//Buttons
JButton recipeRolodexButton = new JButton("Recipe Rolodex");
JButton groceryFinanceButton = new JButton("Grocery Finance");
JButton testButton1 = new JButton("Test Button");
JPanel homeLeftPanel = new JPanel();
JPanel homeRightPanel = new JPanel();
RolodexOptionsPane rop = new RolodexOptionsPane();
public static void main(String[] args){
new Window();
}
public Window(){
//declarations
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = new Dimension();
dim = tk.getScreenSize();
GridBagConstraints gbcl = new GridBagConstraints();
//setting up the window
this.setVisible(true);
this.setSize(550,300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Food Management");
int xPosition = ( (dim.width / 2) - this.getWidth() / 2);
int yPosition = ( (dim.height / 2) - (this.getHeight() / 2));
this.setLocation(xPosition, yPosition);
this.setLayout(new GridBagLayout());
this.setResizable(false);
//Add Action Listeners
recipeRolodexButton.addActionListener(this);;
groceryFinanceButton.addActionListener(this);
testButton1.addActionListener(this);
//Setting up Main Panels
homeLeftPanel.setBackground(Color.BLUE);
homeLeftPanel.setLayout(new GridBagLayout());
homeRightPanel.setBackground(Color.BLACK);
homeRightPanel.setLayout(new GridBagLayout());
// setting the Button constraints in the main Left Panel buttons
gbcl.anchor = GridBagConstraints.CENTER;
gbcl.fill = GridBagConstraints.HORIZONTAL;
gbcl.gridy = 0;
gbcl.gridx = 0;
gbcl.ipady = 4;
gbcl.insets = new Insets(15,0,0,0);
homeLeftPanel.add(recipeRolodexButton,gbcl);
gbcl.gridy = 1;
homeLeftPanel.add(groceryFinanceButton, gbcl);
gbcl.gridy = 2;
homeLeftPanel.add(testButton1, gbcl);
// setting up the buttons in the main right panel
// EXECUTION!!!!!!!!!!!!
PanelSet(homeLeftPanel,homeRightPanel);
}
public void PanelSet(JPanel left , JPanel right){
GridBagConstraints gbcLeft = new GridBagConstraints();
GridBagConstraints gbcRight = new GridBagConstraints();
//positioning the panels
gbcLeft.fill = GridBagConstraints.BOTH;
gbcLeft.gridx = 0;
gbcLeft.gridy = 0;
gbcLeft.weightx = .15;
gbcLeft.weighty = .5;
gbcRight.fill = GridBagConstraints.BOTH;
gbcRight.gridx = 1;
gbcRight.gridy = 0;
gbcRight.weightx = .85;
this.add(left,gbcLeft);
this.add(right,gbcRight);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == recipeRolodexButton){
PanelSet(recipeRolladexButton,rop);
}
}
}
现在我被卡住了。单击“ReciperLodexButton”时,不会发生任何事情。我想做的是,右面板或者被我的面板方法移除并替换,或者被另一个面板覆盖
public class Window extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
//Buttons
JButton recipeRolodexButton = new JButton("Recipe Rolodex");
JButton groceryFinanceButton = new JButton("Grocery Finance");
JButton testButton1 = new JButton("Test Button");
JPanel homeLeftPanel = new JPanel();
JPanel homeRightPanel = new JPanel();
RolodexOptionsPane rop = new RolodexOptionsPane();
public static void main(String[] args){
new Window();
}
public Window(){
//declarations
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = new Dimension();
dim = tk.getScreenSize();
GridBagConstraints gbcl = new GridBagConstraints();
//setting up the window
this.setVisible(true);
this.setSize(550,300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Food Management");
int xPosition = ( (dim.width / 2) - this.getWidth() / 2);
int yPosition = ( (dim.height / 2) - (this.getHeight() / 2));
this.setLocation(xPosition, yPosition);
this.setLayout(new GridBagLayout());
this.setResizable(false);
//Add Action Listeners
recipeRolodexButton.addActionListener(this);;
groceryFinanceButton.addActionListener(this);
testButton1.addActionListener(this);
//Setting up Main Panels
homeLeftPanel.setBackground(Color.BLUE);
homeLeftPanel.setLayout(new GridBagLayout());
homeRightPanel.setBackground(Color.BLACK);
homeRightPanel.setLayout(new GridBagLayout());
// setting the Button constraints in the main Left Panel buttons
gbcl.anchor = GridBagConstraints.CENTER;
gbcl.fill = GridBagConstraints.HORIZONTAL;
gbcl.gridy = 0;
gbcl.gridx = 0;
gbcl.ipady = 4;
gbcl.insets = new Insets(15,0,0,0);
homeLeftPanel.add(recipeRolodexButton,gbcl);
gbcl.gridy = 1;
homeLeftPanel.add(groceryFinanceButton, gbcl);
gbcl.gridy = 2;
homeLeftPanel.add(testButton1, gbcl);
// setting up the buttons in the main right panel
// EXECUTION!!!!!!!!!!!!
PanelSet(homeLeftPanel,homeRightPanel);
}
public void PanelSet(JPanel left , JPanel right){
GridBagConstraints gbcLeft = new GridBagConstraints();
GridBagConstraints gbcRight = new GridBagConstraints();
//positioning the panels
gbcLeft.fill = GridBagConstraints.BOTH;
gbcLeft.gridx = 0;
gbcLeft.gridy = 0;
gbcLeft.weightx = .15;
gbcLeft.weighty = .5;
gbcRight.fill = GridBagConstraints.BOTH;
gbcRight.gridx = 1;
gbcRight.gridy = 0;
gbcRight.weightx = .85;
this.add(left,gbcLeft);
this.add(right,gbcRight);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == recipeRolodexButton){
PanelSet(recipeRolladexButton,rop);
}
}
}
对不起,间隔太长了。复制代码时,缩进丢失了
而右侧面板将根据在任一面板中按下的按钮而变化
public class Window extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
//Buttons
JButton recipeRolodexButton = new JButton("Recipe Rolodex");
JButton groceryFinanceButton = new JButton("Grocery Finance");
JButton testButton1 = new JButton("Test Button");
JPanel homeLeftPanel = new JPanel();
JPanel homeRightPanel = new JPanel();
RolodexOptionsPane rop = new RolodexOptionsPane();
public static void main(String[] args){
new Window();
}
public Window(){
//declarations
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = new Dimension();
dim = tk.getScreenSize();
GridBagConstraints gbcl = new GridBagConstraints();
//setting up the window
this.setVisible(true);
this.setSize(550,300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Food Management");
int xPosition = ( (dim.width / 2) - this.getWidth() / 2);
int yPosition = ( (dim.height / 2) - (this.getHeight() / 2));
this.setLocation(xPosition, yPosition);
this.setLayout(new GridBagLayout());
this.setResizable(false);
//Add Action Listeners
recipeRolodexButton.addActionListener(this);;
groceryFinanceButton.addActionListener(this);
testButton1.addActionListener(this);
//Setting up Main Panels
homeLeftPanel.setBackground(Color.BLUE);
homeLeftPanel.setLayout(new GridBagLayout());
homeRightPanel.setBackground(Color.BLACK);
homeRightPanel.setLayout(new GridBagLayout());
// setting the Button constraints in the main Left Panel buttons
gbcl.anchor = GridBagConstraints.CENTER;
gbcl.fill = GridBagConstraints.HORIZONTAL;
gbcl.gridy = 0;
gbcl.gridx = 0;
gbcl.ipady = 4;
gbcl.insets = new Insets(15,0,0,0);
homeLeftPanel.add(recipeRolodexButton,gbcl);
gbcl.gridy = 1;
homeLeftPanel.add(groceryFinanceButton, gbcl);
gbcl.gridy = 2;
homeLeftPanel.add(testButton1, gbcl);
// setting up the buttons in the main right panel
// EXECUTION!!!!!!!!!!!!
PanelSet(homeLeftPanel,homeRightPanel);
}
public void PanelSet(JPanel left , JPanel right){
GridBagConstraints gbcLeft = new GridBagConstraints();
GridBagConstraints gbcRight = new GridBagConstraints();
//positioning the panels
gbcLeft.fill = GridBagConstraints.BOTH;
gbcLeft.gridx = 0;
gbcLeft.gridy = 0;
gbcLeft.weightx = .15;
gbcLeft.weighty = .5;
gbcRight.fill = GridBagConstraints.BOTH;
gbcRight.gridx = 1;
gbcRight.gridy = 0;
gbcRight.weightx = .85;
this.add(left,gbcLeft);
this.add(right,gbcRight);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == recipeRolodexButton){
PanelSet(recipeRolladexButton,rop);
}
}
}
您应该在右侧面板上使用。然后,当您单击一个按钮时,您只需指定要显示的面板的名称,CardLayout
将完成其余操作
本教程有一个工作示例,演示如何在从组合框中选择项目时交换面板。
复制我的代码时,缩进丢失。
-修复代码。使用所有空格或所有选项卡,但不要将两者混合使用。此外,方法名称不应以大写字符开头。不要把你的班级叫做“窗口”。有一个使用该名称的AWT类可能会引起混淆。选择一个更具描述性的名称。