77范文网 - 专业文章范例文档资料分享平台

Swing代码(3)

来源:网络收集 时间:2019-05-18 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

c.add(p4); p.add(msg); p1.add(lName); p1.add(tName); p1.add(lSex); p1.add(p2);

bgSex.add(male); bgSex.add(female); p2.add(male); p2.add(female); p1.add(lWeight); p1.add(tWeight); p1.add(lInterest); p1.add(p3); p3.add(read); p3.add(tour); p3.add(sport); p3.add(game); p1.add(lCity); p1.add(goCity);

p4.setLayout(new BoxLayout(p4, BoxLayout.X_AXIS)); p4.add(Box.createHorizontalStrut(20)); p4.add(bSubmit);

p4.add(Box.createHorizontalStrut(30)); p4.add(bReset);

bSubmit.setMaximumSize(new Dimension(120, 32)); bReset.setMaximumSize(new Dimension(120, 32)); bSubmit.setActionCommand(\bReset.setActionCommand(\// 为按钮和文本框注册监听器

bSubmit.addActionListener(blsn); bReset.addActionListener(blsn); tName.addCaretListener(tlsn); tWeight.addCaretListener(tlsn); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle(\ this.setSize(320, 300); this.setLocation(200, 200); this.setVisible(true); } public static void main(String[] args) { new CommonComponent(); }

}// end class CommonComponent

// BtnListener监听器类,用于处理点击按钮的事件

class BtnListener implements ActionListener { CommonComponent ob; public BtnListener(CommonComponent ob) { this.ob = ob; } public void actionPerformed(ActionEvent e) { String src = e.getActionCommand(); if (src.equals(\ JFrame outFrame = new JFrame(\ Container c = outFrame.getContentPane(); String output = \ if (ob.female.isSelected()) { ob.sex = \ } if (ob.male.isSelected()) { ob.sex = \ } int i = 0; if (ob.read.isSelected()) { ob.interest[i++] = \ } if (ob.tour.isSelected()) { ob.interest[i++] = \ } if (ob.sport.isSelected()) { ob.interest[i++] = \ } if (ob.game.isSelected()) { ob.interest[i++] = \ } ob.city = (String) (ob.goCity.getSelectedItem()); output += \ output += \ output += \ output += \ for (int j = 0; j < i; j++) { output += ob.interest[j]; } output += \ JTextArea ta = new JTextArea(output, 30, 100); ta.setEditable(false); c.add(ta);

outFrame.setSize(300, 200); outFrame.setVisible(true); outFrame.setLocation(300, 300); } else if (src.equals(\ ob.tName.setText(\ ob.tWeight.setText(\ ob.male.setSelected(true); ob.read.setSelected(false); ob.tour.setSelected(false); ob.sport.setSelected(false); ob.game.setSelected(false); ob.goCity.setSelectedIndex(0); } }

}// end class BtnListener

// TxtListener监听器类,用于处理文本框和密文框上的事件 class TxtListener implements CaretListener { CommonComponent ob; public TxtListener(CommonComponent ob) { this.ob = ob; } public void caretUpdate(CaretEvent e) { Object src = e.getSource(); if (src == ob.tName) { ob.name = ob.tName.getText(); } else if (src == ob.tWeight) { ob.weight = new String(ob.tWeight.getPassword()); } }

}// end class TxtListener

例6-12 JTextAreaDemo.java

import java.awt.*;

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

public class JTextAreaDemo extends JFrame implements ActionListener { JTextArea ta = new JTextArea(5, 10); JButton bCut = new JButton(\ JButton bCopy = new JButton(\ JButton bPaste = new JButton(\ public JTextAreaDemo() { Container c = this.getContentPane();

c.setLayout(new BorderLayout()); JScrollPane sp = new JScrollPane(ta); JPanel p = new JPanel(); p.setLayout(new GridLayout(1, 3, 20, 0)); p.add(bCut); p.add(bCopy); p.add(bPaste); bCut.addActionListener(this); bCopy.addActionListener(this); bPaste.addActionListener(this); c.add(sp, \ c.add(p, \ this.setTitle(\ this.pack(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String args[]) { new JTextAreaDemo(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == bCut) { ta.cut(); } if (e.getSource() == bCopy) { ta.copy(); } if (e.getSource() == bPaste) { ta.paste(); } } }

例6-13 JTextPaneDemo.java import javax.swing.*; import javax.swing.text.*; import java.awt.event.*; import java.awt.*;

public class JTextPaneDemo { JTextPane tp = new JTextPane();

public JTextPaneDemo() { tp.setBackground(Color.white); tp.setEditable(false); }

public void setBlue_Bold(String str) { SimpleAttributeSet sas = new SimpleAttributeSet(); StyleConstants.setForeground(sas, Color.blue); StyleConstants.setBold(sas, true); StyleConstants.setFontSize(sas, 18); insert(str, sas); }

public void setRed_Italic(String str) { SimpleAttributeSet sas = new SimpleAttributeSet(); StyleConstants.setForeground(sas, Color.red); StyleConstants.setItalic(sas, true); StyleConstants.setFontSize(sas, 18); insert(str, sas); }

public void setCyan_UnderLine(String str) { SimpleAttributeSet sas = new SimpleAttributeSet(); StyleConstants.setForeground(sas, Color.cyan); StyleConstants.setUnderline(sas, true); StyleConstants.setFontSize(sas, 18); insert(str, sas); }

public void insert(String str, AttributeSet as) { Document docs = tp.getDocument(); str += \ try { docs.insertString(docs.getLength(), str, as); } catch (BadLocationException e) { e.printStackTrace(); } }

public Component getComponent() { return tp; }

public static void main(String[] args) { JTextPaneDemo tPane = new JTextPaneDemo(); tPane.setBlue_Bold(\ tPane.setRed_Italic(\ tPane.setCyan_UnderLine(\ JFrame f = new JFrame(\ f.getContentPane().add(tPane.getComponent());

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Swing代码(3)在线全文阅读。

Swing代码(3).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/629845.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: