我的框架有问题。我认为我的问题是逻辑问题。我打开菜单框,然后当我点击添加乘客时,框架不会打开。我仍然可以看到前一帧。我使用在这个论坛上发布的想法将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 );
}
}
答案 0 :(得分:3)
对于这种情况,最好应用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 );
}
}
,以便它们可以回复到观众。在这里,我对代码进行了一些更改。
{{1}}