想问个问题,怎么用java写界面的程序?
想问个问题,怎么用java写界面的程序?
用java.awt的API或者javax.swing的API,我最近也刚刚开始JAVA的GUI编程,通常swing下的API写的比awt的好看得多,所以就配合点swing的感觉比较好 这边给你个简单的awt的界面: import java.awt.*; import java.awt.event.*; public class Frame1 extends Frame implements ActionListener { Button a,b; Panel p; public void actionPerformed(ActionEvent ea) { if((ea.getActionCommand()).equals("click")) { setVisible(false); try { Thread.sleep(1000); } catch (Exception ex) { ex.printStackTrace(); } setVisible(true); } if((ea.getActionCommand()).equals("quit")) { setVisible(false); System.exit(0); }
} public Frame1() { a = new Button("click"); b = new Button("quit"); p = new Panel(); add(a); add(b); setBounds(450,300,150,150); setTitle("message"); setLayout(new FlowLayout()); a.addActionListener(this); b.addActionListener(this); setVisible(true); } public static void main(String [] args) { Frame1 frm = new Frame1(); } } -------------------------------- -------------------------------- 再来个配合swing写的: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Frame1 extends JFrame implements ActionListener { JButton a,b; JPanel p; public void actionPerformed(ActionEvent ea) { if((ea.getActionCommand()).equals("click")) { setVisible(false); try { Thread.sleep(1000); } catch (Exception ex) { ex.printStackTrace(); } setVisible(true); } if((ea.getActionCommand()).equals("quit")) { setVisible(false); System.exit(0); }
} public Frame1() { a = new JButton("click"); b = new JButton("quit"); p = new JPanel(); this.getContentPane().add(a); this.getContentPane().add(b); this.setBounds(450,300,150,150); this.setTitle("message"); this.getContentPane().setLayout(new FlowLayout()); a.addActionListener(this); b.addActionListener(this); this.setVisible(true); } public static void main(String [] args) { Frame1 frm = new Frame1(); } }