为什么不能显示组件
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Atmsystem implements ActionListener
{
JFrame frame;
Container con;
JPanel panel1,panel2;
JButton inbutton,okbutton,cancelbutton;
JLabel label1,label2,cardlabel,pwlabel;
Icon bandicon;
Font font,fontpassword;
JTextField card;
JPasswordField password;
String cardnum,paswd;
Atmsystem()
{
frame=new JFrame("ATM System");
frame.setBounds(10,10,500,500);
frame.setVisible(true);
frame.validate();
con=frame.getContentPane();
con.setBackground(Color.BLUE);
con.setVisible(true);
con.validate();
card=new JTextField(15);
password=new JPasswordField(15);
font=new Font("IJ",Font.BOLD,22);
fontpassword=new Font("",Font.BOLD,24);
card.setBounds(200,80,150,30);
password.setBounds(200,160,150,30);
password.setEchoChar('*');
password.setFont(fontpassword);
bandicon=new ImageIcon("logo.gif");
label1=new JLabel("Welcome use the Agriculturl Bank of China",JLabel.CENTER);
label1.setFont(font);
label1.setForeground(Color.YELLOW);
label2=new JLabel(bandicon,JLabel.CENTER);
cardlabel=new JLabel("CardNumber :");
pwlabel=new JLabel("Password :");
cardlabel.setBounds(100,80,90,30);
pwlabel.setBounds(100,160,90,30);
inbutton=new JButton("Plase Insert Your Card");
okbutton=new JButton("OK");
cancelbutton=new JButton("CANCEL");
inbutton.setBounds(150,400,200,30);
okbutton.setBounds(100,400,100,30);
cancelbutton.setBounds(300,400,100,30);
okbutton.addActionListener(this);
cancelbutton.addActionListener(this);
inbutton.addActionListener(this);
panel1=new JPanel();
panel1.setVisible(true);
panel1.setBackground(Color.BLUE);
panel1.setBounds(20,1,450,400);
panel1.add(label1);
panel1.add(label2);
panel1.add(inbutton);
panel1.validate();
panel2=new JPanel();
panel2.setVisible(true);
panel2.setBackground(Color.BLUE);
panel2.setVisible(false);
panel2.setBounds(10,10,450,450);
panel2.add(cardlabel);
panel2.add(pwlabel);
panel2.add(okbutton);
panel2.add(card);
panel2.add(password);
panel2.add(cancelbutton);
con.add(panel1);
con.add(panel2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==inbutton)
{
panel1.setVisible(false);
panel2.setVisible(true);
}
}
public static void main(String args[])
{
new Atmsystem();
}
}
程序运行后不能正常显示组件帮忙找找原因?
搜索更多相关的解决方案:
组件
----------------解决方案--------------------------------------------------------
你把 con.setVisible(true);con.validate();放在con.add(panel1); con.add(panel2)代码的后面试试.
----------------解决方案--------------------------------------------------------
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.validate();
----------------解决方案--------------------------------------------------------