简体   繁体   English

将Jpanel的位置从Jframe更改为JDialog

[英]Changing Jpanel location from Jframe to JDialog

Let's say that I have 1 frame, 1 JDialog and 1 panel. 假设我有1个框架,1个JDialog和1个面板。 The panel is on the frame. 面板在框架上。 What I want to do is if a button is clicked i wanna switch the location of the panel to JDialog. 我想做的是如果单击一个按钮,我想将面板的位置切换到JDialog。 I need two windows so i use Jdialog for that. 我需要两个窗口,因此我使用Jdialog。 Maybe there is a better way of creating that window rather then using JDialog. 也许有一个更好的方法来创建该窗口,而不是使用JDialog。

Part of my code: 我的部分代码:

    public class Bestellterminal { 

    private static JPanel panel;

    public static void addComponentsToPane(final Container pane)  {}

    public static void addComponentsToPane1(final Container pane) {}

    public static void addComponentsToPane2(final Container pane) {

    final JPanel kpanel1 = new JPanel();

    kpanel1.setBounds(0 + insets.left, 0 + insets.top, size.width + 900, 
    size.height + 700);
    kpanel1.setVisible(true);


    final JDialog meinJDialog = new JDialog();

    meinJDialog.setTitle("Küchenterminal");
    meinJDialog.setSize(1200,900);
    meinJDialog.setVisible(true);
    meinJDialog.setLayout(null);
    meinJDialog.add(kpanel1);


     Classic.addActionListener(new ActionListener() {

     public void actionPerformed(ActionEvent e) {

             if (brclassic == 1)        {

                 if (kunde == 1) 
     {Bestellpanel.add(buttonx);buttonx.setVisible(true);brclassic++;
                 kpanel1.add(Bestellpanel);
     }
     }
     }


    private static void createAndShowGUI() 

    {

    JFrame frame = new JFrame("test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     

    addComponentsToPane(frame.getContentPane());
    addComponentsToPane1(frame.getContentPane());
    addComponentsToPane2(frame.getContentPane());

    Insets insets = frame.getInsets();
    frame.setSize(1200 + insets.left + insets.right,
                  900 + insets.top + insets.bottom);
    frame.setVisible(true);

     }

public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
    }    
    }

I made a simple example using two JFrames, it could be done with any type of container/layout really. 我使用两个JFrames作了一个简单的示例,实际上可以使用任何类型的容器/布局来完成。

package helloworld;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;

/**
 * Created on 4/30/17.
 */
public class SwapPanel {

    public static void main(String[] args){

        JPanel panel = new JPanel();
        panel.add(new JLabel("mover"));

        JFrame a = new JFrame("frame a");
        JButton aButton = new JButton("swap");
        JFrame b = new JFrame("frame b");
        JButton bButton = new JButton("swap");
        bButton.setEnabled(false);

        a.getContentPane().add(aButton, BorderLayout.SOUTH);
        a.getContentPane().add(panel, BorderLayout.CENTER);

        b.getContentPane().add(bButton, BorderLayout.SOUTH);

        aButton.addActionListener(evt->{
            if(aButton.isEnabled()){
                aButton.setEnabled(false);
                a.getContentPane().remove(panel);
                b.getContentPane().add(panel, BorderLayout.CENTER);
                bButton.setEnabled(true);
                a.pack();
                b.pack();
                a.repaint();
                b.repaint();
            }
        });

        bButton.addActionListener(evt->{
            if(bButton.isEnabled()){
                bButton.setEnabled(false);
                b.getContentPane().remove(panel);
                a.getContentPane().add(panel, BorderLayout.CENTER);
                aButton.setEnabled(true);
                a.pack();
                b.pack();
                a.repaint();
                b.repaint();
            }
        });


        a.pack();
        a.setVisible(true);
        b.pack();
        b.setVisible(true);

        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

The main thing that needs to be done, first remove the panel from one layout/container, then add the component panel to another container, and finally validate/repaint. 需要完成的主要工作是,首先从一个布局/容器中移除面板,然后将组件面板添加到另一个容器中,最后进行验证/重新绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM