使用 ActionListener 访问预父类
我把这个作为家庭练习,但我不知道如何解决它......(只是访问部分有点难以弄清楚)
所以,我有两个类:“Top”和“Main”。 Top 类如下所示:(简短说明:Top 类的变量“pane”将在 Main 类中用作顶部面板,Main 扩展 JFrame 并具有 BorderLayout)
public class Top extends JPanel implements ActionListener {
//public JPanel pane;
public JButton red, green, blue, white, black;
public Top() {
//pane = new JPanel(); //Useless, as Top is already a JPanel
this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.setBackground(new Color(0xc9c9c9)); //A gray background
red = new JButton("Red");
red.setBackground(Color.red);
red.addActionListener(this);
this.add(red);
green = new JButton("Green");
green.setBackground(Color.green);
green.addActionListener(this);
this.add(green);
blue = new JButton("Blue");
blue.setBackground(Color.blue);
blue.addActionListener(this);
this.add(green);
white = new JButton("White");
white.setBackground(Color.white);
white.addActionListener(this);
this.add(white);
black = new JButton("Black");
black.setForeground(Color.white);
black.setBackground(Color.black);
black.addActionListener(this);
this.add(black);
}
public void actionPerformed(ActionEvent e) {
Main main = (Main)e.getSource();
if (e.getSource() == this.red)
main.setCENTER(1);
if(e.getSource() == this.green)
main.setCENTER(2);
if(e.getSource() == this.blue)
main.setCENTER(3);
if(e.getSource() == this.white)
main.setCENTER(4);
if(e.getSource() == this.black)
main.setCENTER(5);
}
}
Top 类在 Main 中使用,并且每次都使用按下按钮后,主类的中心区域的背景会发生变化。因此我编写了一个小函数setCENTER,它可以改变背景。它的代码如下所示(代码位于 Main 类中):
public void setCENTER(int var){
switch(var){
case 1: pane.setBackground(Color.red);
break;
case 2: pane.setBackground(Color.green);
break;
case 3: pane.setBackground(Color.blue);
break;
case 4: pane.setBackground(Color.white);
break;
case 5: pane.setBackground(Color.black);
break;
}
}
到目前为止,内容显示正确(主窗口的顶部面板和主窗口本身),但每次我尝试通过按更改背景顶部面板中的 5 个按钮之一,我收到错误,第一个错误将我指向顶级类中 actionPerformed-method 的这一行:
Main main = (Main)e.getSource();
我尝试将其替换为“Main main = new Main();”。它起作用了,背景发生了变化,但每次我按下它时,它都会打开一个新窗口(这显然不是它应该有的行为)。
编辑:我忘记了 Top 已经是一个 JPanel,所以在类中使用另一个 JPanel 是非常没有意义的。 (编辑来源)
I got this as home exercise but I have no clue how to solve it... (Just the access part is kind of difficult to figure out)
So, I have two classes: "Top" and "Main". The Top class looks like this: (Short explanation: The Top class' variable "pane" will be used in the class Main as the top panel, Main extends JFrame and has a BorderLayout)
public class Top extends JPanel implements ActionListener {
//public JPanel pane;
public JButton red, green, blue, white, black;
public Top() {
//pane = new JPanel(); //Useless, as Top is already a JPanel
this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.setBackground(new Color(0xc9c9c9)); //A gray background
red = new JButton("Red");
red.setBackground(Color.red);
red.addActionListener(this);
this.add(red);
green = new JButton("Green");
green.setBackground(Color.green);
green.addActionListener(this);
this.add(green);
blue = new JButton("Blue");
blue.setBackground(Color.blue);
blue.addActionListener(this);
this.add(green);
white = new JButton("White");
white.setBackground(Color.white);
white.addActionListener(this);
this.add(white);
black = new JButton("Black");
black.setForeground(Color.white);
black.setBackground(Color.black);
black.addActionListener(this);
this.add(black);
}
public void actionPerformed(ActionEvent e) {
Main main = (Main)e.getSource();
if (e.getSource() == this.red)
main.setCENTER(1);
if(e.getSource() == this.green)
main.setCENTER(2);
if(e.getSource() == this.blue)
main.setCENTER(3);
if(e.getSource() == this.white)
main.setCENTER(4);
if(e.getSource() == this.black)
main.setCENTER(5);
}
}
The Top class is being used in Main and everytime each of the buttons is pressed the CENTER-area's background of the Main class changes. Therefore I have programmed a small function setCENTER, which changes the background. the code for it looks like this (The code is in the Main class):
public void setCENTER(int var){
switch(var){
case 1: pane.setBackground(Color.red);
break;
case 2: pane.setBackground(Color.green);
break;
case 3: pane.setBackground(Color.blue);
break;
case 4: pane.setBackground(Color.white);
break;
case 5: pane.setBackground(Color.black);
break;
}
}
So far the content is displayed properly (the top panel in the Main window and the Main window itself), but everytime I try to change the background by pressing on one of the 5 buttons in the top panel, I get errors and the first error points me to this line of the actionPerformed-method in the Top class:
Main main = (Main)e.getSource();
I tried replacing it with "Main main = new Main();". It worked, the background changes but everytime I pressed it it opened a new window (which is clearly not how it should behave).
EDIT: I forgot that Top is already a JPanel, so using another JPanel inside the class is pretty senseless. (Edited Source)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事件的来源是按钮。要查找按钮所属的面板,您可以获取按钮的父面板:
要获取主面板,您可能还需要在父面板上调用 getParent() 。
The source of the event is the button. To find the panel the button belongs to you can get the parent of the button:
To get the main panel you may need to invoke getParent() on the parent panel as well.
该操作的源是
JButton
而不是Main
,因此当您尝试转换它时会出现异常。您可以考虑将
Main
实例传递给Top
类The source of the action is the
JButton
and not theMain
, hence you get an exception when you try to cast it.You can consider passing your
Main
instance to theTop
class instead在一般情况下,您需要沿着层次结构链向上移动,调用 getParent(),直到找到所需的父级。如果您正在寻找特定的类,通过一些泛型魔法,您可以编写一个实用程序来执行此操作:
因此,如果您想找到触发事件的 Button 的祖先 FooPanel,您会说
In the general case, you'll need to follow the chain up the hierarchy, calling getParent(), until you find the parent you want. If you are looking for a specific class, through some generics magic you can write a utility to do this:
So, if you want to find the FooPanel that is an ancester of the Button that fired the event, you'd say