0
我想寫一個程序,在任何時候都有2個面板。左面板將是一個帶按鈕的菜單,而右面板將根據面板中按下的按鈕而改變。2幀內的Jpanels。左面板是帶按鈕的菜單。使用按鈕切換右側面板。
現在我被卡住了。當點擊「recipeRolodexButton」時沒有任何事情發生。我想要發生的是,右側面板要麼被移除並替換爲我的面板方法,要麼被另一個面板覆蓋。
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);
}
}
}
對於間距抱歉。當複製我的代碼時,縮進失去了。
'當複製我的代碼時,縮進丟失了。 - 修復您的代碼。請使用所有空格或全部選項卡,但不要混合使用這兩個選項卡。另外方法名稱不應以大寫字符開頭。不要稱你爲課堂「窗口」。這個名稱有一個AWT類可能導致混淆。選擇一個更具描述性的名字。 – camickr