我的镜架有问题。我认为我的问题是逻辑上的。我打开菜单框架,然后单击添加乘客时框架不会打开。我仍然可以看到前一帧。我使用在该论坛上发布的想法将setVisisble更改为(false),我以为我可以正确地看到其他框架,但效果不佳。你能给我一些建议吗?谢谢

public class DriverProgram
{
     public static void main (String [ ] argument)
      {
          Flight sw101 = new Flight();
          sw101.setVisible(true);
      }
 }

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

public class Flight extends JFrame
{
SeatingChart sc = new SeatingChart();
WaitingList wl = new WaitingList();
private String buttons[] = new String[5];
private final Font MENU_FONT = new Font("Serif", Font.BOLD, 20);
JButton b;
ActionListener listener1 = new MenuClickListener();
private JTextField input =null;
private JTextField firstName;
private JTextField middleName;
private JTextField lastName;
private final Font NEW_FONT = new Font("Serif", Font.BOLD + Font.ITALIC, 20);

public Flight()
{
    menu();
}

void menu( )
{
    uiFrameMenu();
    addPanelMenu();
}

void uiFrameMenu()
{
    setTitle( "MENU" );
    setSize( 400, 400 );
    setLocationRelativeTo( null );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

void addPanelMenu()
{
    setLayout( new BorderLayout() );
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout (5,1));

    buttons[0] = "Add Passenger";
    buttons[1] = "Remove Passenger";
    buttons[2] = "Seating Chart";
    buttons[3] = "Waiting List";
    buttons[4] = "Exit";


    for( int i = 0; i < buttons.length ; i++ )
    {
        b = new JButton( buttons[i]);
        b.setText( buttons[i] );
        b.setForeground(Color.BLACK);
        b.setFont(MENU_FONT);
        b.addActionListener(listener1);
        panel.add(b);
    }

    getContentPane();
    panel.setBackground(Color.MAGENTA);
    add(panel,"Center");
}

class MenuClickListener implements ActionListener
{
    MenuClickListener() { }

    @Override
    public void actionPerformed( ActionEvent event )
    {
        if ( event.getActionCommand().equals(buttons[0]))
        {
            addPassenger();
        }
        else if ( event.getActionCommand().equals(buttons[1]))
        {

        }
        setVisible(true);
             }
}



void addPassenger( )
{
    this.setEnabled(false);
    uiFrameData();
    setLayout( new BorderLayout() );
    JPanel panel1 = new JPanel();
    panel1.setLayout( new GridLayout(3, 2) );
    JPanel panel2 = new JPanel();
    panel2.setLayout( new GridLayout(3, 1) );
    JLabel l1 = new JLabel("First Name");
    JLabel l2 = new JLabel("Middle Name");
    JLabel l3 = new JLabel("Last Name");
    firstName = new JTextField();
    middleName = new JTextField();
    lastName = new JTextField();
    panel1.add(l1);
    panel1.add(firstName);
    panel1.add(l2);
    panel1.add(middleName);
    panel1.add(l3);
    panel1.add(lastName);
    add(panel1);
}

void uiFrameData()
{
    setTitle( "FIRST, MIDDLE AND LAST NAME" );
    setSize( 400, 400 );
    //setLocationRelativeTo( null );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}

最佳答案

正如已经告诉您的那样,对于这种情况,最好应用CardLayout。在这里,我修改了您的代码,您每次都在Layout上设置JFrame,因为它与您设置的Layout相同,所以为什么不做一次。为什么要无故每次都添加额外的代码:-),在对GUI进行动态更改后在revalidate()上执行repaint()JFrame,以便它们可以与查看者相对应。在这里,我对代码进行了一些更改。

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

public class DriverProgram
{
     public static void main (String [ ] argument)
      {
          SwingUtilities.invokeLater(new Runnable()
          {
                public void run()
                {
                    Flight sw101 = new Flight();
                    sw101.setVisible(true);
                }
          });
      }
 }

class Flight extends JFrame
{
    //SeatingChart sc = new SeatingChart();
    //WaitingList wl = new WaitingList();
    private String buttons[] = new String[5];
    private final Font MENU_FONT = new Font("Serif", Font.BOLD, 20);
    JButton b;
    ActionListener listener1 = new MenuClickListener();
    private JPanel panel; // changed this to instance field so it can be accessed, at button click.
    private JTextField input =null;
    private JTextField firstName;
    private JTextField middleName;
    private JTextField lastName;
    private final Font NEW_FONT = new Font("Serif", Font.BOLD + Font.ITALIC, 20);

    public Flight()
    {
        menu();
    }

    void menu( )
    {
        uiFrameMenu();
        addPanelMenu();
    }

    void uiFrameMenu()
    {
        setTitle( "MENU" );
        setSize( 400, 400 );
        setLocationRelativeTo( null );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }

    void addPanelMenu()
    {
        setLayout( new BorderLayout() );
        panel = new JPanel();
        panel.setLayout(new GridLayout (5,1));

        buttons[0] = "Add Passenger";
        buttons[1] = "Remove Passenger";
        buttons[2] = "Seating Chart";
        buttons[3] = "Waiting List";
        buttons[4] = "Exit";


        for( int i = 0; i < buttons.length ; i++ )
        {
            b = new JButton( buttons[i]);
            b.setText( buttons[i] );
            b.setForeground(Color.BLACK);
            b.setFont(MENU_FONT);
            b.addActionListener(listener1);
            panel.add(b);
        }

        getContentPane();
        panel.setBackground(Color.MAGENTA);
        add(panel,"Center");
    }

    class MenuClickListener implements ActionListener
    {
        MenuClickListener() { }

        @Override
        public void actionPerformed( ActionEvent event )
        {
            if ( event.getActionCommand().equals(buttons[0]))
            {
                addPassenger();
            }
            else if ( event.getActionCommand().equals(buttons[1]))
            {

            }
        }
    }



    void addPassenger( )
    {
        //this.setEnabled(false);
        remove(panel);
        uiFrameData();
        JPanel panel1 = new JPanel();
        panel1.setLayout( new GridLayout(3, 2) );
        JPanel panel2 = new JPanel();
        panel2.setLayout( new GridLayout(3, 1) );
        JLabel l1 = new JLabel("First Name");
        JLabel l2 = new JLabel("Middle Name");
        JLabel l3 = new JLabel("Last Name");
        firstName = new JTextField();
        middleName = new JTextField();
        lastName = new JTextField();
        panel1.add(l1);
        panel1.add(firstName);
        panel1.add(l2);
        panel1.add(middleName);
        panel1.add(l3);
        panel1.add(lastName);
        add(panel1);

        revalidate(); // If your JDK is 1.7 else use getContentPane().revalidate(); for JDK 1.6 or below
        repaint();
    }

    void uiFrameData()
    {
        setTitle( "FIRST, MIDDLE AND LAST NAME" );
        setSize( 400, 400 );
    }
}

关于java - 无法关闭上一帧以查看新帧Java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9797635/

10-09 07:20