从一帧移到另一帧

时间:2018-08-07 11:47:46

标签: java swing jframe

我是JAVA swing的新手,如果单击按钮框架,则我将开发两个不同的JFrame;如果将按钮框架移到另一个框架,并且通过打开下一个框架,则关闭上一个框架。

我单击按钮打开下一帧,但按钮上未显示该帧内的数据,并且未关闭上一帧。请帮助找到代码中的问题。

框架1:---------------------------------

package com.demo.test;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.demo.gui.TestjigWindow;

public class TestjigWindowCheck extends JFrame{

private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;

public TestjigWindowCheck() {

    initUI();
}

private void initUI() {

    mainFrame = new JFrame("Fuse Test jig");
    mainFrame.setSize(400,400);
    mainFrame.setLayout(new GridLayout(3, 1));

    headerLabel = new JLabel("", JLabel.CENTER);        
    statusLabel = new JLabel("",JLabel.CENTER);    
    statusLabel.setSize(500,500);

    controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());

    mainFrame.add(headerLabel);
    mainFrame.add(controlPanel);
    mainFrame.add(statusLabel);
    mainFrame.setVisible(true);  
}

public void showEventDemo(){
    //TestjigWindow frame1 = new TestjigWindow();
    headerLabel.setText("Fuse Test Jig"); 
    headerLabel.setFont(new Font( "Arial", Font.BOLD, 25));
    headerLabel.setBackground(Color.green);

    JButton startButton = new JButton("Start");
    startButton.setActionCommand("Start");

    JButton closeButton = new JButton("Close");
    closeButton.setActionCommand("close");

    startButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            try
            {
             if(e.getSource() == startButton)
             {
              TestjigWindow2 frame2 = new TestjigWindow2();
              frame2.setVisible(true);
              dispose();
             } 
             else if(e.getSource() == closeButton)
             {
                 System.exit(0);
             }

            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    });

   closeButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            try
            {
              System.exit(0);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    });

    controlPanel.add(startButton);
    controlPanel.add(closeButton);

    mainFrame.setVisible(true);  
 }

public static void main(String[] args) {

    TestjigWindowCheck test = new TestjigWindowCheck();
    test.showEventDemo();
    //test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
 }  

框架2 -----------------------------------。

package com.demo.test;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class TestjigWindow2 extends JFrame{
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JPanel controlPanel1;

public TestjigWindow2()
{
    prepareGUI();
}

public static void main(String args[])
{
    TestjigWindow2 test = new TestjigWindow2();
    test.showRadioButton();
}

private void prepareGUI()
{

    mainFrame = new JFrame("Fuse Test2 jig");
    mainFrame.setSize(400,400);
    mainFrame.setLayout(new GridLayout(3, 1));

    headerLabel = new JLabel("", JLabel.CENTER);        
    statusLabel = new JLabel("",JLabel.CENTER);    
    statusLabel.setSize(500,500);

    controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());

    mainFrame.add(headerLabel);
    mainFrame.add(controlPanel);
    mainFrame.add(statusLabel);
    mainFrame.setVisible(true);

}

public void showRadioButton()
{
    headerLabel.setText("Fuse Mode"); 
    final JRadioButton setting =new JRadioButton("Setting");    
    final JRadioButton testing =new JRadioButton("Testing");

    setting.setBounds(75,50,100,30);    
    testing.setBounds(75,100,100,30);

    setting.setMnemonic(KeyEvent.VK_S);
    testing.setMnemonic(KeyEvent.VK_T);

    ButtonGroup group = new ButtonGroup();

    group.add(setting);
    group.add(testing);

    controlPanel.add(setting);
    controlPanel.add(testing);

    JButton button = new JButton("Next");
    button.setActionCommand("Next");

    controlPanel.add(button);
    mainFrame.setVisible(true);  
 }
}

2 个答案:

答案 0 :(得分:0)

对于这个问题,我认为这是一个相当简单的解决方案,正如Andrew所言,您不需要继续创建JFrame,可以在第一个程序中创建JFrame,然后将其通过构造函数传递给第二个类。

为什么我认为您的程序正在关闭是因为您在创建新框架后调用 dispose(),这可能会破坏新框架中的组件。

您可以采用这种方法,该方法仅使用一个在开始课程中创建的帧,并延续到第二个课程中

例如(使用代码段):

框架1

  //This is where you are moving to the second frame.
  if(e.getSource() == startButton)
  {
      mainFrame.getContentPane().removeAll(); //removeAll() method wipes all components attached to the contentpane of the frame
      //Frame can be reused when passed to second class
      TestjigWindow2 frame2 = new TestjigWindow2(this.mainFrame);

   } 

框架2

//In your constructor you could have something like this

private JFrame mainFrame; 
/*
 * Other variables and constants go here
 *
 */

public TestjigWindow2(JFrame mainFrame)
{
   this.mainFrame = mainFrame;
   prepareGUI();
}

然后在prepareGUI()中,将组件添加到框架中,而无需创建新框架。这样,您无需创建多个JFrame即可关闭您的第一页,并打开第二个框架。

答案 1 :(得分:0)

您应该只在第一帧的actionPerformed方法中创建一个TestjigWindow2的新实例。不必在开始按钮和停止按钮上分别添加actionPerformed,而是在Frame1类中实现ActionListener接口,并只保留一个方法,因为无论如何都在方法内部检查源代码。

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        try
        {
         if(e.getSource() == startButton)
         {
          TestjigWindow2 frame2 = new TestjigWindow2();
          //frame2.setVisible(true); do this inside the frame2 preparegui method
          dispose();
         } 
         else if(e.getSource() == closeButton)
         {
             System.exit(0);
         }

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
});

如果您在Frame1内部具有main方法并在其中实例化Frame1,则代码将具有更为通用的流程。

而且您不需要在Frame1的actionPerformed内部使用setVisible。