将方法中的面板添加到框架中

时间:2015-06-13 17:02:44

标签: java swing methods jframe jpanel

我真的不知道怎么说这句话,但基本上是:

- 我有一些单独的“碎片”,我试图添加到主框架上;为了防止代码变得笨拙,我将每个“片段”都作为自己的类。

- 我很难将窗格添加到主框架上,因为类本身不是面板,而是类的方法创建面板,这会产生我不知道如何解决的问题。

PIECE(当我创建对话而不是面板时,它自己工作):

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


public class PieceThing3 extends JPanel //<switched from JDialog
{
    //set up variables here


private ActionListener pieceAction = new ActionListener()
{
    public void actionPerformed (ActionEvent ae)
    {
        // Action Listener (this also works)
    }
};

private void createPiece()
{
    //setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    //setLocationByPlatform(true);
    // the above are commented out when I switch from dialog to panel

    JPanel contentPane = new JPanel();

    //something that uses pieceAction is here

    //two buttons, b and s, with action listeners are here

    contentPane.add(b);
    contentPane.add(s);
    add(contentPane);
    //pack();
       //again, commented out to switch from dialog
    setVisible(true);
    System.out.println("hi I'm done");
      //just to check and make sure it's done
}

public static void main(String[] args) 
{
    // TODO Auto-generated method stub
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            new PieceThing3().createPiece();
        }
    });
}
}

很抱歉这是非常模糊的,但错综复杂并不像一般的想法那么重要 - 当我创建自己的对话框时,它完美无缺,但现在我试图让它在主代码中制作一个面板,下面:

MASTER:

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

public class CollectGUI extends JFrame{


private void createDialog(){
    this.setSize(2000,1000);
    this.setLocation(0,0);
    this.setTitle("TITLE");


    PieceThing3 pt = new PieceThing3();
    //HERE, if I do pt.main(null); while it is in "dialog mode" (rather than panel) it pops up a dialog box and everything is hunky dory. But I don't know how to get it to add the method as a panel.

   this.add(pt.main(null));
   //this gives an error

   this.setVisible(true);
}


public static void main(String[] args) {
    // TODO Auto-generated method stub
    new CollectGUI().createDialog();
}

}

正如我在评论中所说的那样,当我将pt设置为进行对话时,如果我只是执行pt.main(null),它会这样做,但是如果我尝试将pt.main(null)作为面板添加它会抛出一个错误。任何人都可以给我一些关于如何添加类而不是类的方法的见解吗?我很难过。

谢谢!!

1 个答案:

答案 0 :(得分:0)

你肯定是在正确的轨道上努力保持关注点的分离,并在许多不同的组件中实现你的gui。尝试这样的事情:

Panel1的

import javax.swing.JLabel;
import javax.swing.JPanel;

public class Panel1 extends JPanel {

    public Panel1() {
        this.add(new JLabel("This is panel 1"));
    }

}

是Panel2

import javax.swing.JLabel;
import javax.swing.JPanel;

public class Panel2 extends JPanel {

    public Panel2() {
        this.add(new JLabel("This is panel 2"));
    }

}

的JFrame

import java.awt.BorderLayout;

import javax.swing.JFrame;

import org.yaorma.example.jframe.panel.panel1.Panel1;
import org.yaorma.example.jframe.panel.panel2.Panel2;

public class ExampleJFrame extends JFrame {

    public ExampleJFrame() {
        super("Example JFrame Application");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400,400);
        this.setLayout(new BorderLayout());
        Panel1 pan1 = new Panel1();
        Panel2 pan2 = new Panel2();
        this.add(pan1, BorderLayout.NORTH);
        this.add(pan2, BorderLayout.SOUTH);
        this.setVisible(true);
    }
}

主:

public class ExampleApplication {

    public static void main(String[] args) throws Exception {
        new ExampleJFrame();
    }

}

编辑: 这是一个内容更多的Panel1。

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.yaorma.example.action.sayhello.SayHelloAction;

public class Panel1 extends JPanel {

    //
    // instance variables
    //

    private JButton pressMeButton;

    //
    // constructor
    //

    public Panel1() {
        this.setLayout(new BorderLayout());
        this.add(new JLabel("This is panel 1"), BorderLayout.NORTH);
        this.initPressMeButton();
    }

    //
    // button
    //

    private void initPressMeButton() {
        this.pressMeButton = new JButton("Press Me");
        this.pressMeButton.addActionListener(new PressMeButtonActionListener());
        this.add(pressMeButton, BorderLayout.SOUTH);
    }

    //
    // method to get the parent jframe
    //

    private JFrame getParentJFrame() {
        Container con = this;
        while(con != null) {
            con = con.getParent();
            if(con instanceof JFrame) {
                return (JFrame)con;
            }
        }
        return null;
    }

    //
    // action listener for Press Me button
    //

    private class PressMeButtonActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            JFrame jFrame = getParentJFrame();
            SayHelloAction action = new SayHelloAction(jFrame);
            action.execute();
        }

    }

}

按钮调用的操作:

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class SayHelloAction {

    private JFrame owner;

    public SayHelloAction(JFrame owner) {
        this.owner = owner;
    }

    public void execute() {
        JOptionPane.showMessageDialog(owner, "Hello World");
    }

}