我试图用左边的按钮看一下基本的,然后它们链接到的面板应该会弹出但是我有一些问题让它正常工作
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语句有关,或者我不理解动作事件的关键元素
编辑: 我将If语句更改为代码中现在的内容,但仍然无效。我可以点击主页和常见问题,但我的其他两个jbuttons似乎没有工作。我试图用联系按钮测试它
答案 0 :(得分:1)
Contact Us
, First DewMocracy
和NullPointerException
按钮无效。 contactPanel
和firstPanel
从未初始化,当您尝试将其添加到null
中的center
面板时actionPerformed()
。
另外,请查看A Visual Guide to Layout Managers。 CardLayout
可能会有所帮助,而不是手动替换面板。
答案 1 :(得分:0)
变化
Object src = ae.getSource();
到
Jbutton src = (JButton)ae.getSource();
然后
if(src.equals(home))
或
if(src.equals(contact))
依旧......
答案 2 :(得分:0)
你应该将它们添加到代码顶部的容器中,初始化它们,然后在你的else if语句下使用;
contactPanel.setVisible(true);
等。对于他们所有人