Swing代码(3)

2019-05-17 19:53

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());


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

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

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

马上注册会员

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