首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用继承在Java中使用基本窗口和扩展窗口

使用继承在Java中使用基本窗口和扩展窗口
EN

Stack Overflow用户
提问于 2011-07-20 15:42:50
回答 3查看 1.9K关注 0票数 1

我有一个名为WindowTemplate的类,它是其他(更复杂)窗口的基础。它是一个抽象类,然后我尝试使用“扩展”技巧将更多的东西添加到新窗口中,保留原来的“骨架”。

这是我的问题,因为如果我运行WindowTemplate.createWindow();a_Welcome.createWindow(); (它们应该指向相同的东西),我就得到了我的“基本”窗口。但是当我运行a_Welcome window =newa_Welcome();(应该是基本的+新的东西)时,我只得到了我添加的没有原始特性的额外部分。

这是我的代码:

代码语言:javascript
复制
package windows;

import java.awt.*;
import javax.swing.*;

public abstract class WindowTemplate extends JFrame {

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 */
public static void createWindow() {

    JFrame myFrame = new JFrame("My first window");
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setVisible(true);
    myFrame.setSize(550, 450);
    myFrame.setLocationRelativeTo(null);

    // JLabel emptyLabel = new JLabel("");
    // emptyLabel.setPreferredSize(new Dimension(550, 450));

    // myFrame.getContentPane().setLayout(new CardLayout());
    // myFrame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

    // myFrame.pack();

}

} 

具有新窗口和一些额外内容的类(忽略a_):

代码语言:javascript
复制
package windows;

import java.awt.*;

import javax.swing.*;

public class a_Welcome extends WindowTemplate {

public a_Welcome() {

    JPanel area = new JPanel();

    JLabel text = new JLabel("One line another line and another line"); // , JLabel.CENTER);

    // text.setBounds(80, 400, 400, 50);
    add(area);

    // area.setLayout(null);
    area.add(text, new CardLayout());

    // area.add(text); // , BorderLayout.CENTER);

    Font font = new Font("SansSerif", Font.BOLD, 30);
    text.setFont(font);
    text.setForeground(Color.green);
    area.setBackground(Color.darkGray);
    area.setSize(550, 450);

}

}

// timer-after 5 seconds-go to the next window (countdown in the bottom right corner)

主要是:

代码语言:javascript
复制
package windows;

public class Launcher {

public static void main(String[] args) {

    // Schedule a job for the event-dispatching thread:
    // creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {

            // WindowTemplate.createWindow();
            // a_Welcome.createWindow();

             a_Welcome window = new a_Welcome();
             window.setVisible(true);
        }
    });

}

}

谢谢你的帮忙!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-07-20 15:48:03

静态方法createWindow()总是创建一个新的JFrame,它不是WindowTemplate的超类。a_Window的构造函数正在向WindowTemplate中添加组件,因为静态createWindow()创建了一个独立的框架,所以还没有初始化组件。

我建议您将静态createWindow()转换为WindowTemplate构造函数,并再次运行main

代码语言:javascript
复制
package windows;

import java.awt.*;
import javax.swing.*;

public abstract class WindowTemplate extends JFrame {

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 */
public WindowTemplate () {

    JFrame myFrame = new JFrame("My first window");
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setVisible(true);
    myFrame.setSize(550, 450);
    myFrame.setLocationRelativeTo(null);

    // JLabel emptyLabel = new JLabel("");
    // emptyLabel.setPreferredSize(new Dimension(550, 450));

    // myFrame.getContentPane().setLayout(new CardLayout());
    // myFrame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

    // myFrame.pack();

}

}
票数 1
EN

Stack Overflow用户

发布于 2011-07-20 15:53:02

在静态JFrame ()方法中定义了另一个createWindow。这意味着要向此框架中添加仅作用域为createWindow()方法的组件,并在构造函数中添加到a_Welcome实例。

你应该这样做

代码语言:javascript
复制
public class BaseWindow() {

  //Constructor
  public BaseWindow() {
    init();
  }

  public void init() {
    //add basic components
  }
}


public class SubClassWindow() {
  public SubClassWindow() {
    super();
  }

  @Override
  public void init() {
    super.init(); //important so you get the base stuff
    //add other components
  }
}

代码未被测试。

票数 0
EN

Stack Overflow用户

发布于 2011-07-20 16:29:37

您可能考虑的另一种方法是拥有一个JFrame,它只是一个包装器,并通过添加一个面板来组成窗口。假设您希望在所创建的每个窗口的顶部都有一个工具栏。每个窗口在工具栏上都有不同的按钮,底部有一组不同的组件。通过这种方式,您可以进行组合而不是继承,因为继承以后会变得很难看。(有关这一点的讨论,请参阅thisthisthis )

看起来应该是:

代码语言:javascript
复制
public interface AppPanel {
  List<JButton> getToolbarButtons();
  boolean okToClose();
  JPanel getGui();
}

public MyPanel extends JPanel implements AppPanel {
  //standard swing components stuff set up here

  public List<JButton> getToolbarButtons() {
    //set up buttons and their actions
    return buttonList;
  }

  public boolean okToClose() {
    //ask user if they want to save, etc.
    return true;
  }

  public JPanel getGui() {
    return this;
  }
}

public AppFrame extends JFrame {
  private AppPanel panel;

  public static AppFrame createFrame(AppPanel panel) {
    AppFrame frame = new AppFrame(panel);
    return frame;
  }

  public AppFrame(AppPanel panel) {
    super();
    this.panel = panel;
    add(panel.getGui(), someLayoutConstraints);

    panel.getToolbarButtons(); //do stuff with the buttons

    //...
    this.addWindowListener(new WindowAdapter() {
      public void WindowClosing(WindowEvent e) {
        if (panel.isOkToClose()) {
          setVisible(false);
        }
      }
    });
  }


}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6764651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档