gpt4 book ai didi

java - 无法关闭前一帧以查看新帧 java

转载 作者:行者123 更新时间:2023-11-29 06:01:55 24 4
gpt4 key购买 nike

我的框架有问题。我认为我的问题是逻辑一。我打开菜单框架,然后当我点击添加乘客时,框架没有打开。我仍然可以看到上一帧。我使用了这个论坛上发布的想法将 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 ,对于这种情况,正如已经告诉你的那样。我在这里修改了你的代码,你每次都在你的 JFrame 上设置 Layout,因为它与你设置的 Layout 相同,为什么不这样做那只有一次。为什么每次都无缘无故地添加额外的代码:-),在对 JFrame 进行更改后执行 revalidate()repaint() GUI 在运行中,以便它们可以返回给查看者。在这里,我对代码做了一些更改。

//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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com