Swing代码

2019-05-17 19:53

例 6-1 GetContentPaneDemo.java

import java.awt.*; import javax.swing.*;

public class GetContentPaneDemo { public static void main(String[] args) { JFrame f = new JFrame(\ Container c = f.getContentPane(); // JFrame组件获取容器Content Pane

c.setLayout(new BorderLayout());

JButton btn = new JButton(\c.add(btn, BorderLayout.NORTH);

// 组件添加到容器Content Pane上,而非直接加到JFrame组件上

f.setVisible(true); f.setSize(200,100); f.validate(); } }

例 6-2 NewJFrame.java

import java.awt.*;

import java.awt.event.*; import javax.swing.*;

public class NewJFrame extends JFrame implements ActionListener { JButton btn = new JButton(\ public NewJFrame() { Container c = this.getContentPane(); c.add(btn); this.setTitle(\ this.pack(); this.setVisible(true); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); btn.addActionListener(this); }// end method NewJFrame public void actionPerformed(ActionEvent e) { JFrame newFrame = new JFrame(\ newFrame.setVisible(true); newFrame.setSize(200, 100); } public static void main(String[] args) {

new NewJFrame(); }

}// end class NewJFrame

例 6-2 修改版 NewJFrame2.java

import java.awt.*;

import java.awt.event.*; import javax.swing.*;

public class NewJFrame2 extends JFrame implements ActionListener { JButton btn = new JButton(\ public NewJFrame2() { Container c = this.getContentPane(); c.add(btn); this.setTitle(\ this.pack(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn.addActionListener(this); }// end method NewJFrame2 public void actionPerformed(ActionEvent e) { JFrame newFrame = new JFrame(\ newFrame.setVisible(true); newFrame.setSize(200, 100); newFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public static void main(String[] args) { new NewJFrame2(); }

}// end class NewJFrame2

例6-3 JDialogDemo.java

import java.awt.*;

import java.awt.event.*; import javax.swing.*;

public class JDialogDemo extends JFrame implements ActionListener { JButton bQuery = new JButton(\查询\ JButton bQuit = new JButton(\退出\

public JDialogDemo() { Container c = this.getContentPane(); bQuery.addActionListener(this);

bQuit.addActionListener(this); this.setTitle(\成绩查询系统\

this.setSize(240, 60); c.setLayout(new GridLayout(1, 2, 20, 10)); c.add(bQuery); c.add(bQuit); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.validate(); this.setVisible(true); }

public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals(\查询\

new LoginDialog(this); } else if (cmd.equals(\退出\

System.exit(0); } } public static void main(String[] args) { new JDialogDemo(); } }

//定制的登陆对话框类

class LoginDialog extends JDialog { JLabel bj = new JLabel(\班级\ JLabel xm = new JLabel(\姓名\ JLabel xh = new JLabel(\学号\

JTextField txtBj = new JTextField(30); JTextField txtXm = new JTextField(30); JTextField txtXh = new JTextField(30); JButton confirm = new JButton(\确定\JButton cancel = new JButton(\取消\public LoginDialog(Frame owner) { Container c = this.getContentPane(); c.setLayout(new GridLayout(4, 2, 5, 5)); c.add(bj); c.add(txtBj); c.add(xm); c.add(txtXm); c.add(xh); c.add(txtXh); c.add(confirm); c.add(cancel);

this.setTitle(\查询对话框\

this.setSize(160, 160); this.validate(); this.setVisible(true); } }

例 6-4 JOptionPaneDemo.java

import java.awt.*;

import java.awt.event.*; import javax.swing.*;

public class JOptionPaneDemo extends JFrame implements ActionListener { JButton msgDlg = new JButton(\消息对话框\ JButton inDlg1 = new JButton(\输入对话框1\ JButton inDlg2 = new JButton(\输入对话框2\ JButton cnfDlg = new JButton(\确认对话框\ JButton optDlg = new JButton(\选项对话框\

public JOptionPaneDemo() { Container c = this.getContentPane(); c.setLayout(new GridLayout(5, 1, 0, 10)); c.add(msgDlg); c.add(inDlg1); c.add(inDlg2); c.add(cnfDlg); c.add(optDlg); this.setTitle(\ this.setSize(200, 240); this.setLocation(300, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.validate(); this.setVisible(true); msgDlg.addActionListener(this); inDlg1.addActionListener(this); inDlg2.addActionListener(this); cnfDlg.addActionListener(this); optDlg.addActionListener(this); }

public static void main(String[] args) { new JOptionPaneDemo(); }

// 事件处理

public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(\消息对话框\

JOptionPane.showMessageDialog(this, \消息对话框常用于显示文本信息\ \消息对话框\

JOptionPane.INFORMATION_MESSAGE); } else if (e.getActionCommand().equals(\输入对话框1\ JOptionPane.showInputDialog(this, \请输入你的姓名:\ \输入文本的对话框\ JOptionPane.INFORMATION_MESSAGE); } else if (e.getActionCommand().equals(\输入对话框2\ String[] sex = { \男\女\ JOptionPane.showInputDialog(this, \你的性别?\ \进行选择的输入对话框\ JOptionPane.QUESTION_MESSAGE, null, sex, sex[0]);

} else if (e.getActionCommand().equals(\确认对话框\ JOptionPane.showConfirmDialog(this, \今天心情好吗?\ \确认对话框\ JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, new ImageIcon(\

} else if (e.getActionCommand().equals(\选项对话框\ String[] option = { \好\不好\取消\ JOptionPane.showOptionDialog(this, \今天心情好吗?\ \选项对话框\

JOptionPane.YES_NO_CANCEL_OPTION,

JOptionPane.INFORMATION_MESSAGE, new ImageIcon(\ } } }

例 6-5 JAppletDemo.java

import java.awt.*; import javax.swing.*;

public class JAppletDemo extends JApplet { JLabel msg = new JLabel(\这是JApplet示例...\ JTextField msg2 = new JTextField(\中可以使用Swing组件\

public void init() { Container c = getContentPane(); c.setLayout(new GridLayout(2, 1)); add(msg); add(msg2); }


Swing代码.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:甲级单位编制光伏玻璃项目可行性报告(立项可研+贷款+用地+2013

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: