我对GUI很新,我在使用这个任务时遇到了麻烦。基本上我们总共有八个类,必须以三种不同的方式使用三个面板类,一种方式只使用TopPanel和InitialPanel类,一种只使用BottomPanel和InitialPanel类,另一种只使用InitialPanel类。目标是在按下顶部面板中的按钮时,在底部面板中显示关于足球运动员的信息。 `
public class BottomPanel extends JPanel implements ActionListener
{
public JButton b1;
public BottomPanel(final JPanel topPanel)
{
super();
setBackground(Color.pink);
//setLayout(new GridLayout(3,1));
b1 = new JButton("When the user clicks on the button in the UPPER panel, displays the football player's position here" );
add(b1);
}
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
if(obj == b1)
{
b1.setText(fp1.getPosition());
}
}
}
public class InitialPanel extends JPanel
{
public InitialPanel()
{
super();
setBackground(Color.gray);
setLayout(new BorderLayout());
TopPanel p1 = new TopPanel();
add(p1,"North");
BottomPanel p2 = new BottomPanel(p1);
add(p2,"Center");
}
}`
public class TopPanel extends JPanel
{
public TopPanel()
{
super();
setBackground(Color.yellow);
footballPlayer fp1 = new footballPlayer("Mark","Allen",22, "IST", 5.6f, 180, "Junior","Running Back");
// the whatsUp of this student has to shown in the other panel
JButton jl1 = new JButton(fp1.getInfo());
add(jl1);
}
}`
我认为我有唯一的TopPanel和InitialPanel正在运行,但我仍然坚持要对其他两个做什么。此外,getInfo()是设置底部按钮文本时调用的方法,我们不能创建除TopPanel中使用的另一个足球运动员对象。任何帮助将不胜感激!
答案 0 :(得分:1)
您已将jl1
按钮添加到TopPanel
,现在您应该为该按钮添加一个监听器:您可以在BottomPanel
类添加它。
您在topPanel
构造函数中收到对BottomPanel
的引用,因此您可以将其保留为成员并创建侦听器,如下所示:
public class BottomPanel extends JPanel implements ActionListener
{
public JButton b1;
/** added member to topPanel **/
private JPanel mTopPanel;
public BottomPanel(final JPanel topPanel)
{
super();
setBackground(Color.pink);
this.mTopPanel = topPanel;
b1 = new JButton("When the user clicks on the button in the UPPER
panel, displays the football player's position here" );
add(b1);
/** the topPanel jli button listener **/
this.mTopPanel.jl1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
/** edit your button with the info of the player **/
b1.setText("player info added here");
}
});
}
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
if(obj == b1)
{
b1.setText(fp1.getPosition());
}
}
}
答案 1 :(得分:1)
您应该将底部面板注册为顶部面板中按钮的Listener
。为此,您应该创建一个列表,将所有侦听器存储到TopPanel
类中的按钮。按下按钮后,当前播放器的信息将被广播给所有听众。您需要做的是让ButtonPanel
类实现Listener
,我称之为DisplayPlayerInfoListener
。
class TopPanel extends JPanel {
JButton displayButton = new JButton("show player info");
FootballPlayer currentPlayer = new FootballPlayer();
List<DisplayPlayerInfoListener> listeners = new ArrayList<>();
public TopPanel() {
add(displayButton);
displayButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayButtonPressed();
}
});
}
void displayButtonPressed() {
for (DisplayPlayerInfoListener listener : listeners) {
listener.displayPlayer(currentPlayer);
}
}
public void addDisplayPlayerInfoListener(DisplayPlayerInfoListener listener) {
listeners.add(listener);
}
}
interface DisplayPlayerInfoListener {
void displayPlayer(FootballPlayer player);
}
class BottomPanel extends JPanel implements DisplayPlayerInfoListener {
@Override
public void displayPlayer(FootballPlayer player) {
// show the player's information on your bottom panel's UI
}
}
在代码中的某处,您需要将底部面板添加为顶部面板按钮的监听器。
topPanel.addDisplayPlayerInfoListener(bottomPanel);
通过执行此操作,您可以根据需要注册任意数量的侦听器,因为此信息可能也需要广播到其他UI组件。此外,它将两个面板分开,您不需要通过访问方法中的其他面板字段来获取信息。信息将在准备好发送时处理。 TopPanel只将BottomPanel视为DisplayPlayerInfoListener实例,因此耦合较少。