您好,欢迎光临本网站![请登录][注册会员]  
文件名称: SWING高级设计,RCP技术讲解,SWT/JFACE RCP入门资料
  所属分类: Java
  开发工具:
  文件大小: 4mb
  下载次数: 0
  上传时间: 2009-08-26
  提 供 者: tb***
 详细说明: Swing组件简介 1. 模型-视图-控制器设计模式 Swing组件都有三个要素:  内容,例如,按钮的状态(是否按下)或者文本框中的文本。  外观显示(颜色,尺寸)。  行为(对事件的反应)。 也就是MVC模式:实现此模式的三个独立的类:  模型-------存储内容  视图-------显示内容  控制器----处理用户输入 这个模式明确规定了三个对象如何进行交互。模型存储内容,它没有任何用户界面。对于一个按钮来说,内容非常简单—它只是一组标志,用来说明按钮是否按下,是否启用等等。对于一个文本框来说,内 容稍稍复杂,它是容纳当前文本的一个字符串对象。这个内容和视图的内容并不一致------如果内容的长度大于文本框的显示长度,那么用户看到的就只有文本框显示的那一部分。模型必须实现改变和发现内容的方法。例如:一个文本模型中的方法有:在当前文本中添加或者删除字符以及把当前文本作为一个字符串返回等。再次强调,模型是完全不可见的,显示存储在模型中的数据是视图的工作。 2. 选择组件  复选框(JCheckBox) 例如:JCheckBoxTest.java import java.awt.*; import java.awt.event.*; import javax.swing.*; class JCheckBoxTest { public static void main(String[] args) { JCheckBoxFrame f=new JCheckBoxFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.show(); } } class JCheckBoxFrame extends JFrame { private JLabel label; private JCheckBox bold; private JCheckBox italic; public static final int FONTSIZE=30; public JCheckBoxFrame() { setTitle("JCheckBoxFrame"); setSize(400,300); Container contentPane=getContentPane(); label=new JLabel("The dog jumps over the pig"); label.setFont(new Font("Serif",Font.PLAIN,FONTSIZE)); contentPane.add(label,BorderLayout.CENTER); ActionListener listener=new ActionListener() { public void actionPerformed(ActionEvent e) { int mode=0; if(bold.isSelected()) mode +=Font.BOLD; if(italic.isSelected()) mode +=Font.ITALIC; label.setFont(new Font("Serif",mode,FONTSIZE)); } }; JPanel buttonPanel=new JPanel(); bold=new JCheckBox("Bold"); bold.addActionListener(listener); buttonPanel.add(bold); italic =new JCheckBox("Italic"); italic.addActionListener(listener); buttonPanel.add(italic); contentPane.add(buttonPanel,BorderLayout.SOUTH); } };  单选按钮 对于复选框来说,用户可以选折0个或多个。在许多情况下,我们需要用户只选择几个框中的一个。当用户选择另外一个时,前一个选择就自动取消。这样的一组框通常称作一个单选按钮组(radio button group),这是因为这些按钮的工作方式像收音机上的电台选择按钮,当按下一个按钮时,前一个按下的按钮则自动释放。 例如:JRadioButtonTest.java import java.awt.*; import java.awt.event.*; import javax.swing.*; class JRadioButtonTest { public static void main(String[] args) { JRadioButtonFrame f=new JRadioButtonFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.show(); } } class JRadioButtonFrame extends JFrame { private JPanel buttonPanel; private ButtonGroup group; private JLabel label; public static final int DEFAULT_SIZE=30; public JRadioButtonFrame() { setTitle("JRadioButtonFrame"); setSize(400,300); Container contentPane=getContentPane(); label=new JLabel("The lazy dog doesn't jump over the clever pig"); label.setFont(new Font("Serif",Font.PLAIN,DEFAULT_SIZE)); contentPane.add(label,BorderLayout.CENTER); buttonPanel=new JPanel(); group=new ButtonGroup(); addRadioButton("Small",12); addRadioButton("Medium",18); addRadioButton("Large",25); addRadioButton("Extra large",36); contentPane.add(buttonPanel,BorderLayout.SOUTH); } public void addRadioButton(String name,final int size) { boolean selected=size==DEFAULT_SIZE; JRadioButton button=new JRadioButton(name,selected); group.add(button); buttonPanel.add(button); ActionListener listener=new ActionListener() { public void actionPerformed(ActionEvent e) { label.setFont(new Font("Serif",Font.PLAIN,size)); } }; button.addActionListener(listener); } }  边界 如果在一个窗口中有多组单选按钮,那么你需要从视觉上说明哪些按钮属于同一组。Swing提供了一组有用的边界来解决该问题。你可以为任何扩展了JComponent的组件提供一种边界。最常见的用法是在一个面板周围设置一种边界,然后用其他用户界面元素(如单选按钮)来填充该面板。 有几种不同边界可供选择,但是使用它们的步骤完全一样:  低斜面  凸斜面  蚀刻  直线  不光滑  空(只是在组件周围创建一些空白地方) 例如:BorderTest.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class BorderTest { public static void main(String[] args) { BorderFrame f=new BorderFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.show(); } }; class BorderFrame extends JFrame { private JPanel demoPanel; private JPanel buttonPanel; private ButtonGroup group; public BorderFrame() { setTitle("BorderTest"); setSize(400,300); demoPanel=new JPanel(); buttonPanel=new JPanel(); group=new ButtonGroup(); addRadioButton("Lowered bevel",BorderFactory.createLoweredBevelBorder()); addRadioButton("Raised bevel",BorderFactory.createRaisedBevelBorder()); addRadioButton("Etched",BorderFactory.createEtchedBorder()); addRadioButton("Line",BorderFactory.createLineBorder(Color.blue)); addRadioButton("Matte",BorderFactory.createMatteBorder(10,10,10,10,Color.blue)); addRadioButton("Empty",BorderFactory.createEmptyBorder()); Border etched=BorderFactory.createEtchedBorder(); Border titled=BorderFactory.createTitledBorder(etched,"Border types"); buttonPanel.setBorder(titled); Container contentPane=getContentPane(); contentPane.setLayout(new GridLayout(2,1)); contentPane.add(buttonPanel); contentPane.add(demoPanel); } public void addRadioButton(String buttonName,final Border b) { JRadioButton button=new JRadioButton(buttonName); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { demoPanel.setBorder(b); validate(); } }); group.add(button); buttonPanel.add(button); } };  组合框 如果你有很多的选择项,那么使用单选按钮就不合适了,这是因为它们会占用很大的屏幕空间。取而代之的是,你可以使用一个组合框。当用户点击该组件时,一列选择项就会下拉弹出,然后用户可以选择其中的一个。如果该下拉列表框被设置成可编辑的,那么你可以编辑当前选择项,这时候该组件才称作组合框----它把编辑框的灵活性与一组预定义的选择项组合起来。JComboBox类提供了组合框组件。 例如:JComboBoxTest.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JComboBoxTest { public static void main(String[] args) { JComboBoxFrame f=new JComboBoxFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.show(); } }; class JComboBoxFrame extends JFrame { private JComboBox faceCombo; private JLabel label; public static final int DEFAULT_SIZE=30; public JComboBoxFrame() { setTitle("JComboBoxFrame"); setSize(400,300); Container contentPane=getContentPane(); label=new JLabel("The dog jumps over the monkey"); label.setFont(new Font("Serif",Font.PLAIN,DEFAULT_SIZE)); contentPane.add(label,BorderLayout.CENTER); faceCombo=new JComboBox(); faceCombo.setEditable(true); faceCombo.addItem("Serif"); faceCombo.addItem("SansSerif"); faceCombo.addItem("Monospaced"); faceCombo.addItem("Dialog"); faceCombo.addItem("DialogInput"); faceCombo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { label.setFont(new Font((String)faceCombo.getSelectedItem(),Font.PLAIN,DEFAULT_SIZE)); } }); JPanel comboPanel =new JPanel(); comboPanel.add(faceCombo); contentPane.add(comboPanel,BorderLayout.SOUTH); } };  滑块 组合框允许用户从一组离散值中进行选择。而滑块允许进行连续值的选择,例如,选择从1到100的任意值。构造一个滑块的方法如下: JSlider slider=new JSlider(min,max,initialValue); 如果你忽略最小值,最大值和初始值,那么这三项分别默认为0,100,50。或者,如果你需要一个垂直的滑块,那么可以使用如下构造器: JSlider slider=new JSlider(SwingConstants.VERTICAL,min,max,initialValue); 下面是使用滑块的综合例子。 例如:JSliderTest.java import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; public class JSliderTest { public static void main(String[] args) { JSliderTestFrame f=new JSliderTestFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.show(); } }; class JSliderTestFrame extends JFrame { private JPanel sliderPanel; private JTextField textField; private ChangeListener listener; public JSliderTestFrame() { setTitle("JSliderTest"); setSize(400,500); sliderPanel=new JPanel(); sliderPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); listener=new ChangeListener() { public void stateChanged(ChangeEvent e) { JSlider source=(JSlider)e.getSource(); textField.setText(""+source.getValue()); } }; // add a plain slider JSlider slider=new JSlider(); addSlider(slider,"Plain"); //add a slider with major and minor ticks slider=new JSlider(); slider.setPaintTicks(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); addSlider(slider,"Ticks"); //add a slider that snaps to ticks slider=new JSlider(); slider.setPaintTicks(true); slider.setSnapToTicks(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); addSlider(slider,"Snap to ticks"); //add a filled slider slider =new JSlider(); slider.setPaintTicks(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); slider.putClientProperty("JSlider.isFilled",Boolean.TRUE); addSlider(slider,"Filled"); // add a filled and inverted slider slider=new JSlider(); slider.setPaintTicks(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); slider.putClientProperty("JSlider.isFilled",Boolean.TRUE); slider.setInverted(true); addSlider(slider,"Inverted"); //add a slider with numeric labels slider=new JSlider(); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); addSlider(slider,"Labels"); //add a slider with alphabetic labels slider=new JSlider(); slider.setPaintLabels(true); slider.setPaintTicks(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); Hashtable labelTable=new Hashtable(); labelTable.put(new Integer(0),new JLabel("A")); labelTable.put(new Integer(20),new JLabel("B")); labelTable.put(new Integer(40),new JLabel("C")); labelTable.put(new Integer(60),new JLabel("D")); labelTable.put(new Integer(80),new JLabel("E")); labelTable.put(new Integer(100),new JLabel("F")); slider.setLabelTable(labelTable); addSlider(slider,"Custom labels"); //add a slider with icon labels slider=new JSlider(); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.setSnapToTicks(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(20); labelTable = new Hashtable(); //add card images labelTable.put(new Integer(0),new JLabel(new ImageIcon("01.gif"))); labelTable.put(new Integer(20),new JLabel(new ImageIcon("02.gif"))); labelTable.put(new Integer(40),new JLabel(new ImageIcon("03.gif"))); labelTable.put(new Integer(60),new JLabel(new ImageIcon("04.gif"))); labelTable.put(new Integer(80),new JLabel(new ImageIcon("05.gif"))); labelTable.put(new Integer(100),new JLabel(new ImageIcon("06.gif"))); slider.setLabelTable(labelTable); addSlider(slider,"Icon labels"); //add the text field that displays the slider value textField=new JTextField(); Container contentPane=getContentPane(); contentPane.add(sliderPanel,BorderLayout.CENTER); contentPane.add(textField,BorderLayout.SOUTH); } public void addSlider(JSlider s,String description) { s.addChangeListener(listener); JPanel panel=new JPanel(); panel.add(s); panel.add(new JLabel(description)); sliderPanel.add(panel); } };  微调控制器(JSpinner) JSpinner(微调控制器)也是文本框,它在一边带有两个小按钮,可以让你增加或者减少存储在文本框内的值。 例如:JSpinnerTest.java import java.awt.*; import java.awt.event.*; import java.text.*; import java.util.*; import javax.swing.*; public class JSpinnerTest { public static void main(String[] args) { JSpinnerFrame f=new JSpinnerFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.show(); } }; class JSpinnerFrame extends JFrame { private JPanel mainPanel; private JButton okButton; public JSpinnerFrame() { setTitle("SpinnerTest"); setSize(400,300); Container contentPane=getContentPane(); JPanel buttonPanel=new JPanel(); okButton=new JButton("Ok"); buttonPanel.add(okButton); contentPane.add(buttonPanel,BorderLayout.SOUTH); mainPanel=new JPanel(); mainPanel.setLayout(new GridLayout(0,3)); contentPane.add(mainPanel,BorderLayout.CENTER); JSpinner defaultSpinner=new JSpinner(); addRow("Default",defaultSpinner); JSpinner boundedSpinner=new JSpinner(new SpinnerNumberModel(5,0,10,0.5)); addRow("Bounded",boundedSpinner); String[] fonts=GraphicsEnvironment.getLocalGraphicsEnvironment() .getAvailableFontFamilyNames(); JSpinner listSpinner=new JSpinner(new SpinnerListModel(fonts)); addRow("List",listSpinner); JSpinner reverseListSpinner=new JSpinner(new SpinnerListModel(fonts) { public Object getNextValue() { return super.getPreviousValue(); } public Object getPreviousValue() { return super.getNextValue(); } }); addRow("Reverse List",reverseListSpinner); JSpinner dateSpinner=new JSpinner(new SpinnerDateModel()); addRow("Date",dateSpinner); JSpinner betterDateSpinner =new JSpinner(new SpinnerDateModel()); String pattern=((SimpleDateFormat)DateFormat.getDateInstance()).toPattern(); betterDateSpinner.setEditor(new JSpinner.DateEditor(betterDateSpinner,pattern)); addRow("Better Date",betterDateSpinner); JSpinner timeSpinner =new JSpinner(new SpinnerDateModel( new GregorianCalendar(2000,Calendar.JANUARY,1,12,0,0).getTime(),null,null,Calendar.HOUR)); addRow("Time",timeSpinner); JSpinner permSpinner=new JSpinner(new PermutationSpinnerModel("meat")); addRow("Word permutations",permSpinner); } public void addRow(String labelText,final JSpinner spinner) { mainPanel.add(new JLabel(labelText)); mainPanel.add(spinner); final JLabel valueLabel=new JLabel(); mainPanel.add(valueLabel); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object value=spinner.getValue(); valueLabel.setText(value.toString()); } }); } }; class PermutationSpinnerModel extends AbstractSpinnerModel { private String word; public PermutationSpinnerModel(String w) { word=w; } public Object getValue() { return word; } public void setValue(Object value) { if(!(value instanceof String)) throw new IllegalArgumentException(); word=(String)value; fireStateChanged(); } public Object getNextValue() { StringBuffer buffer=new StringBuffer(word); for(int i=buffer.length()-1;i>0;i--) { if(buffer.charAt(i-1)buffer.charAt(j)) j--; swap(buffer,i-1,j); reverse(buffer,i,buffer.length()-1); return buffer.toString(); } } reverse(buffer,0,buffer.length()-1); return buffer.toString(); } public Object getPreviousValue() { StringBuffer buffer=new StringBuffer(word); for(int i=buffer.length()-1;i>0;i--) { if(buffer.charAt(i-1)>buffer.charAt(i)) { int j=buffer.length()-1; while(buffer.charAt(i-1)getWidth()) icon=new ImageIcon(icon.getImage().getScaledInstance(getWidth(),-1,Image.SCALE_DEFAULT)); setIcon(icon); } } } }); } };  颜色选择器 上个例子你已经看到,高质量的文件选择器是一个很复杂的用户界面组件,你可能不愿意自己实现。除了文件选择器之外,Swing只提供了另外一种选择器-----JColorChooser。 你可以使用它来挑选一种颜色。同JFileChooser类一样,颜色选择器是一个组件而非一个对话框。但是它包含了用于创建包含一个颜色选择器组件的对话框的方法。 例如:JColorChooserTest.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class JColorChooserTest { public static void main(String[] args) { JColorChooserFrame f=new JColorChooserFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.show(); } }; class JColorChooserFrame extends JFrame { public JColorChooserFrame() { setTitle("JColorChooserTest"); setSize(400,300); ColorChooserPanel panel=new ColorChooserPanel(); Container contentPane=getContentPane(); contentPane.add(panel); } }; class ColorChooserPanel extends JPanel { public ColorChooserPanel() { JButton modalButton=new JButton("Modal"); modalButton.addActionListener(new ModalListener()); add(modalButton); JButton modelessButton=new JButton("Modeless"); modelessButton.addActionListener(new ModelessListener()); add(modelessButton); JButton immediateButton=new JButton("Immediate"); immediateButton.addActionListener(new ImmediateListener()); add(immediateButton); } private class ModalListener implements ActionListener { public void actionPerformed(ActionEvent e) { Color defaultColor=getBackground(); Color selected=JColorChooser.showDialog(ColorChooserPanel.this,"Set background",defaultColor); setBackground(selected); } }; private class ModelessListener implements ActionListener { private JDialog dialog; private JColorChooser chooser; public ModelessListener() { chooser=new JColorChooser(); dialog=JColorChooser.createDialog(ColorChooserPanel.this,"Background Color",false,chooser,new ActionListener() { public void actionPerformed(ActionEvent e) { setBackground(chooser.getColor()); } },null); } public void actionPerformed(ActionEvent e) { chooser.setColor(getBackground()); dialog.show(); } } private class ImmediateListener implements ActionListener { private JDialog dialog; private JColorChooser chooser; public ImmediateListener() { chooser=new JColorChooser(); chooser.getSelectionModel().addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { setBackground(chooser.getColor()); } }); dialog=new JDialog((Frame)null,false/*not moal */); dialog.getContentPane().add(chooser); dialog.pack(); } public void actionPerformed(ActionEvent e) { chooser.setColor(getBackground()); dialog.show(); } }; }; ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 输入关键字,在本站1000多万海量源码库中尽情搜索: