我試圖讓一個基本的看起來與左邊的按鈕,然後他們鏈接到的面板應該彈出,但即時通訊有一些問題,讓它正常工作JPanels沒有通過其他顯示如果語句
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MtnDew extends JApplet implements ActionListener
{
JButton home, first, faq, contact;
Image img;
JLabel title, logo, copyright, ntbkImg, firstMsg, ntbkDesc, infoSheets, welcomeMsg;
JLabel basket;
Color blackColor = new Color(10, 10, 10);
Color greenColor = new Color(10,200,50);
JPanel leftside, top, center, welcome, firstPanel,contactPanel, separator;
Jpanel faqPanel;
public void init()
{
setLayout(new BorderLayout());
doTitle();
doLeftSide();
doBottom();
doCenter();
setupFAQPanel();
}
public void setupButton(JButton b)
{
b.setContentAreaFilled(false);
b.setBorderPainted (false);
b.setFocusable(false);
b.setForeground (blackColor);
b.addActionListener (this);
leftside.add(b);
}
public void doTitle()
{
title = new JLabel("<HTML>The DewMocracy",JLabel.CENTER);
img = getImage(getCodeBase(), "MtnDew1.jpeg");
logo = new JLabel (new ImageIcon(img));
img = getImage(getCodeBase(), "MtnDew2.jpeg");
title = new JLabel (new ImageIcon(img));
top = new JPanel (new FlowLayout());
top.add(logo);
top.add(title);
top.setBackground (blackColor);
add(top, BorderLayout.NORTH);
}
public void doLeftSide()
{
//Links on left bar
leftside = new JPanel (new GridLayout(5, 1));
leftside.setBackground(greenColor);
home = new JButton ("Home");
first = new JButton ("First DewMocracy");
faq = new JButton ("FAQ");
contact = new JButton ("Contact Us");
setupButton (home);
setupButton (first);
setupButton (faq);
setupButton (contact);
basket = new JLabel(new ImageIcon(getImage( getCodeBase(),"MtnDew3.jpeg")));
leftside.add(basket);
add(leftside, BorderLayout.WEST);
}
public void doBottom()
{
copyright = new JLabel("<HTML>Text from dobottom",JLabel.CENTER);
copyright.setForeground(blackColor);
copyright.setOpaque(true);
copyright.setBackground(greenColor);
add(copyright, BorderLayout.SOUTH);
}
public void doCenter()
{
center = new JPanel (new BorderLayout());
center.setBackground(blackColor);
separator = new JPanel();
separator.setBackground (greenColor);
separator.setPreferredSize(new Dimension (10, 20));
center.add(separator, BorderLayout.NORTH);
welcome = new JPanel(new FlowLayout());
welcome.setOpaque(false);
welcomeMsg = new JLabel("<html><center>"
+ "<H2>this is a welcome message</H2>");
welcomeMsg.setForeground(greenColor);
welcome.add(welcomeMsg);
center.add(welcome,BorderLayout.CENTER);
add(center, BorderLayout.CENTER);
}
public void setupcontactPanel()
{
JLabel contactText = new JLabel ("<HTML><H1>FAQ</H1>"
+ "contact panel test");
contactPanel = new JPanel (new FlowLayout());
contactPanel.setOpaque(false);
contactPanel.add(contactText);
}
public void setupFAQPanel()
{
JLabel faqText = new JLabel ("<HTML><H1>FAQ</H1>"
+ "lots bla text can go here");
faqPanel = new JPanel (new FlowLayout());
faqPanel.setOpaque(false);
faqPanel.add(faqText);
}
public void actionPerformed (ActionEvent ae)
{
JButton src = (JButton)ae.getSource();
center.removeAll();
separator = new JPanel();
separator.setBackground (greenColor);
separator.setPreferredSize(new Dimension (10, 20));
center.add(separator, BorderLayout.NORTH);
if (src.equals(home))
{
center.removeAll();
center.add(welcome,BorderLayout.CENTER);
}
else if (src.equals(contact))
{
center.removeAll();
center.add(contactPanel, BorderLayout.CENTER);
}
else if (src.equals(faq))
{
center.removeAll();
center.add(faqPanel, BorderLayout.CENTER);
}
else if (src.equals(first))
{
center.removeAll();
center.add(firstPanel, BorderLayout.CENTER);
}
center.repaint();
validate();
repaint();
}
}
我一直試圖讓它工作,我甚至開始做一個簡單的版本,但它也不工作。這似乎是什麼與我的如果陳述,或即時通訊不瞭解的行動事件的關鍵要素
編輯: 我改變了現在在代碼中的If語句,它仍然無法正常工作。我可以點擊家庭和常見問題,但我的其他兩個jbuttons似乎不工作。即時通訊試圖使用接觸按鈕
我會大膽地猜測,並說這是因爲你不使用」 .equals'來評價您的謂詞。 '=='只檢查內存位置。 – Max