Java使用不从JFrame扩展

时间:2014-04-03 21:17:11

标签: java object jframe

我很难理解如何编写我的程序,而不是从JFrame扩展它。

我尝试删除extends JFrame子句并将其添加到我的两个方法中,将CalculatorWhichUsesAInterface frame = new CalculatorWhichUsesAInterface();部分替换为JFrame frame = new JFrame();以及其他一些内容,但没有任何效果。

如何在我的计划中使用JFrame frame = new JFrame()而不是使用extends JFrame

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

class CalculatorWhichUsesAInterface extends JFrame{

     JFrame frame = new JFrame();

     public CalculatorWhichUsesAInterface(){
         JPanel jPanelOne = new JPanel();
         jPanelOne.setLayout(new GridLayout(1, 1);

         jPanelOne.add(new JButton("x"));

         JPanel jPanelTwo = new JPanel(new BorderLayout());
         JTextField field = new JTextField();
         field.setText("2141987.01235");
         jPanelTwo.add(field, BorderLayout.NORTH);
         jPanelTwo.add(jPanelOne, BorderLayout.CENTER);
         add(jPanelTwo, BorderLayout.CENTER);
    }

    public static void main(String[] args){
        CalculatorWhichUsesAInterface frame = new CalculatorWhichUsesAInterface();
        frame.setTitle("Calculator");
        frame.setSize(500, 200);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

3 个答案:

答案 0 :(得分:4)

这实际上相对简单(抱歉,但确实如此)。

首先从JPanel扩展您的课程,这为您提供了构建界面的基本容器。

删除JFrame frame = new JFrame();因为您并未真正使用它,并在main方法中创建JFrame的新实例,并将组件添加到其中

class CalculatorWhichUsesAInterface extends JPanel {
     public CalculatorWhichUsesAInterface(){
        JPanel jPanelOne = new JPanel();
         jPanelOne.setLayout(new GridLayout(1, 1);


         jPanelOne.add(new JButton("x"));

         JPanel jPanelTwo = new JPanel(new BorderLayout());
         JTextField field = new JTextField();
         field.setText("2141987.01235");
         jPanelTwo.add(field, BorderLayout.NORTH);
         jPanelTwo.add(jPanelOne, BorderLayout.CENTER);
         add(jPanelTwo, BorderLayout.CENTER);
     }

    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                CalculatorWhichUsesAInterface calc = new CalculatorWhichUsesAInterface();
                JFrame frame = new JFrame()
                frame.setTitle("Calculator");
                frame.add(calc);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

此概念为您提供灵活且可重复使用的组件。这意味着您可以决定组件的显示方式和位置。显示在其自己的框架中(如上例所示)或添加到另一个容器(例如另一个JPanel甚至是applet)

您可能还想查看Initial Threads

示例二 - 没有扩展任何内容     class CalculatorWhichUsesAInterface {

    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JPanel jPanelOne = new JPanel();
                jPanelOne.setLayout(new GridLayout(1, 1);


                jPanelOne.add(new JButton("x"));

                JPanel jPanelTwo = new JPanel(new BorderLayout());
                JTextField field = new JTextField();
                field.setText("2141987.01235");
                jPanelTwo.add(field, BorderLayout.NORTH);
                jPanelTwo.add(jPanelOne, BorderLayout.CENTER);

                JFrame frame = new JFrame()
                frame.setTitle("Calculator");
                frame.add(jPanelTwo);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

更新了" builder"示例

这是构建器模式的一个(非常基本的)示例,基本上,您有一个单独的类,它只是构建UI并返回JPanel(在此示例中)

更复杂的构建器将允许您添加其他属性以调整结果。

class CalculatorWhichUsesAInterface {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {

                JFrame frame = new JFrame();
                frame.setTitle("Calculator");
                frame.add(CalculatorBuilder.build());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }

    public static class CalculatorBuilder {

        public static JPanel build() {
            JPanel jPanelOne = new JPanel();
            jPanelOne.setLayout(new GridLayout(1, 1));

            jPanelOne.add(new JButton("x"));

            JPanel jPanelTwo = new JPanel(new BorderLayout());
            JTextField field = new JTextField();
            field.setText("2141987.01235");
            jPanelTwo.add(field, BorderLayout.NORTH);
            jPanelTwo.add(jPanelOne, BorderLayout.CENTER);
            return jPanelTwo;
        }

    }
}

答案 1 :(得分:1)

看起来你在main(setTitle,setSize等)中为JFrame对象所做的一切都是"来的"来自继承(扩展JFrame)。所以实际上你没有做任何事情:

JFrame frame = new JFrame();

如果你不想使用继承,你应该调用JFrame对象的方法,而不是CalculatorWhichUsesAInterface()。所以构造函数应该如下所示(在main leave中只创建对象):

class CalculatorWhichUsesAInterface{
     JFrame frame = new JFrame();
     public CalculatorWhichUsesAInterface(){
         JPanel jPanelOne = new JPanel();
         jPanelOne.setLayout(new GridLayout(1, 1);


         jPanelOne.add(new JButton("x"));

         JPanel jPanelTwo = new JPanel(new BorderLayout());
         JTextField field = new JTextField();
         field.setText("2141987.01235");
         jPanelTwo.add(field, BorderLayout.NORTH);
         jPanelTwo.add(jPanelOne, BorderLayout.CENTER);
         frame.add(jPanelTwo, BorderLayout.CENTER); //DIFFERENCE
         frame.setTitle("Calculator");
         frame.setSize(500, 200);
         frame.setLocationRelativeTo(null);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);

     }

正如您所看到的,我几乎从main复制到构造函数,但它在不同的对象上调用(实际上是JFrame,而不是CalculatorWhichUsesAInterface)。但是通过继承来做JFrame是常见的方法。

答案 2 :(得分:1)

不扩展JFrame你有正确的想法,但是在上面的代码中,你需要先删除扩展的JFrame,但你声明框架的方式很好:

JFrame frame = new JFrame("Title");

然后从那里你只需要引用它的对象,例如:

frame.add(jPanelTwo,BorderLayout.CENTER)