【发布时间】:2018-09-06 12:10:25
【问题描述】:
我决定将 BorderLayout 用于名为 Loginwards 的摇摆设计。我为 BorderLayout 的 5 面写了 5 个类,分别为 PageStart、LineStart、Center、LineEnd、PageEnd。
我没有装饰 Loginwards 并设计了 PageStart 用于最小化、调整大小、退出。(对于我自己的图像、规则...)按钮退出很简单,因为它就像这样工作
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
但例如最小化我无法解决。我应该从另一个类对象更改我的 Loginwards 属性
总结
public class Loginwards extends Jframe
{ ...
JFrame frame = new JFrame("BorderLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(PageStart.Panel(), BorderLayout.PAGE_START);
...
}
public class PageStart{
public static JPanel Panel(){
JPanel panel = new JPanel();
FlowLayout pagestart = new FlowLayout(FlowLayout.RIGHT);
panel.setLayout(pagestart);
panel.add(MinimizeButton());
panel.add(ResizeButton());
panel.add(QuitButton());
}
public static JButton MinimizeButton(){
JButton button = new JButton();
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// I Should be able to reach Loginwards here.
}
});
return button;
}
【问题讨论】:
-
为什么?你甚至没有那个类中的 Loginwards 实例
-
我想在点击 PageStart 类时最小化我的 Loginwards
-
您的设计/代码不清楚。为什么
Loginwards extends Jframe?为什么同时有JFrame frame = new JFrame("BorderLayoutDemo");?这就像说Loginwards是一个盒子,它有自己的盒子frame。你用的是哪个盒子(如果两者都用,怎么用)? -
您不应该使用静态方法和变量。这表明您的类存在设计问题。没有足够的信息来说明您正在尝试做什么来提供替代方案。但基本的答案是,如果你想从另一个类调用一个类中的方法,那么第一个类需要对第二个类的引用。
标签: java swing class user-interface