javaGUI设计中,JFrame的连接
这次学期末的课程设计,需要用GUI设计一个取款机的小程序,现在遇到了几个问题,
我做了好几个界面,怎么链接起来?
我用JFrame做的,能不能加上图片背景?
我附一段代码。
程序代码:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Welcome extends JFrame { private JComboBox jcb; private JPanel p1,p2; private JLabel pic; private JButton ok,cancel; private JLabel name,psw,note; private JPasswordField pswf; private String name1; private String password1; private String user; private ImageIcon myImage; public Welcome( ) { Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout() ); pic=new JLabel(new ImageIcon("欢迎界面.jpg")); contentPane.add(pic,BorderLayout.NORTH); p1= new JPanel(); //p1.setSize(50,50); p1.setBorder(BorderFactory.createEtchedBorder() ); //显示一圈边儿 contentPane.add(p1,BorderLayout.CENTER ); name=new JLabel("请输入卡号"); p1.add(name); jcb= new JComboBox(); jcb.addItem( "admin" ); jcb.addItem( "aloie" ); jcb.setSelectedIndex( 0 ); jcb.setEditable(true); p1.add(jcb); psw=new JLabel("请输入密码"); p1.add(psw); pswf=new JPasswordField(12); p1.add(pswf); p2=new JPanel(); contentPane.add(p2,BorderLayout.SOUTH); contentPane.add(p2,BorderLayout.SOUTH ); ok=new JButton("登 陆"); ok.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { okJButtonActionPerformed(event); } } ); cancel=new JButton("取 消"); cancel.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); //退出登陆 } } ); note=new JLabel("如有疑问请联系客服热线12580"); p2.add(ok); p2.add(cancel); p2.add(note); setTitle( "登陆窗口" ); this.pack(); setResizable( false ); //将最大化按钮设置为不可用 show(); } private void okJButtonActionPerformed( ActionEvent event ) { //okJButton响应事件,检查用户名和密码的匹配 name1= jcb.getSelectedItem().toString(); if (name1.equals("admin") ) { if (pswf.getText().equals("admin")) { showNewWindow(); setVisible( false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } else if (name1.equals("aloie")) { if ( pswf.getText().equals("aloie") ) { showNewWindow(); setVisible(false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } } public void showNewWindow() { Goangneng(); } public static void main(String[] args ) { //JFrame.setDefaultLookAndFeelDecorated(true); Welcome mylogin = new Welcome( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }
第二个
程序代码:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Gongneng extends JFrame { private JComboBox nameJComboBox; private JPanel userJPanel,buttonPanel; private JLabel pictureJLabel; private JButton okJButton,cancelJButton,gmJButton,qkJButton; private JLabel nameJLabel,passwordJLabel,note; private JPasswordField passwordJPasswordField; private String name1; private String password1; private String user; private ImageIcon myImageIcon; public Gongneng( ) { Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout() ); pictureJLabel=new JLabel(); pictureJLabel.setIcon(new ImageIcon("pic.jpg")); contentPane.add(pictureJLabel,BorderLayout.NORTH); userJPanel = new JPanel(); userJPanel.setSize(50,50); userJPanel.setBorder(BorderFactory.createEtchedBorder() ); //显示一圈边儿 contentPane.add(userJPanel,BorderLayout.CENTER ); buttonPanel=new JPanel(); buttonPanel.setSize(400,400); contentPane.add(buttonPanel,BorderLayout.SOUTH ); okJButton=new JButton("查询"); cancelJButton=new JButton("取款"); gmJButton=new JButton("改密 "); qkJButton=new JButton("取卡"); note=new JLabel(""); buttonPanel.add(okJButton); buttonPanel.add(cancelJButton); buttonPanel.add(gmJButton); buttonPanel.add(qkJButton); buttonPanel.add(note); setTitle( "功能窗口" ); this.pack(); setSize(350, 350 ); setResizable( true ); //将最大化按钮设置为不可用 } public static void main( String[] args ) { Gongneng mylogin = new Gongneng( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }但是,我连不起来这两个窗口。
谢谢