窗口
昨天看书然后抄了这个,有些能懂、有些搞不明白。不过慢慢来吧
package ck2; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; //import com.sun.tools.javac.launcher.Main; public class 总的 implements ActionListener,ItemListener,ListSelectionListener{ JLabel jl; JButton jbtninput; JButton jbtnexit; JRadioButton jrbtn_red; JRadioButton jrbtn_green; JRadioButton jrbtn_blue; JCheckBox jchk_bold; JCheckBox jchk_italic; JComboBox jcbo; JList jlst; 总的(){ JFrame jf=new JFrame(); jf.setSize(300,200); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jl= new JLabel("我们不喜欢Java",JLabel.CENTER);//加入到最上面 jbtninput=new JButton("修改文字"); jbtnexit=new JButton("退出"); jbtninput.addActionListener(this); jbtnexit.addActionListener(this); jrbtn_red=new JRadioButton("红色"); jrbtn_green=new JRadioButton("绿色"); jrbtn_blue=new JRadioButton("蓝色"); ButtonGroup bg=new ButtonGroup(); bg.add(jrbtn_red); bg.add(jrbtn_green); bg.add(jrbtn_blue); jrbtn_red.addActionListener(this); jrbtn_green.addActionListener(this); jrbtn_blue.addActionListener(this); jchk_bold=new JCheckBox("粗体"); jchk_italic=new JCheckBox("斜体"); jchk_bold.addActionListener(this); jchk_italic.addActionListener(this); jcbo=new JComboBox(); jcbo.addItem("8"); jcbo.addItem("12"); jcbo.addItem("24"); jcbo.addItem("36"); jcbo.addItemListener(this); jlst=new JList(); Vector vv=new Vector(); vv.add("宋体"); vv.add("隶书"); vv.add("黑体"); jlst.setListData(vv); jlst.addListSelectionListener(this); JPanel jp=new JPanel(new BorderLayout()); JPanel jpc=new JPanel(); JPanel jpl=new JPanel(new GridLayout(4,1)); JPanel jpl1=new JPanel(); JPanel jpl2=new JPanel(); JPanel jpl3=new JPanel(); JPanel jp2=new JPanel(); JPanel jp3=new JPanel(); JPanel jps=new JPanel(new FlowLayout(FlowLayout.LEFT,40,10)); FlowLayout fl=new FlowLayout(FlowLayout.LEFT); jpl1.setLayout(fl); jpl2.setLayout(fl); jpl3.setLayout(fl); jp2.setLayout(fl); jpl1.add(new JLabel("请选择前景色")); jpl1.add(jrbtn_red); jpl1.add(jrbtn_green); jpl1.add(jrbtn_blue); jpl2.add(new JLabel("请选择字形:")); jpl2.add(jchk_bold); jpl2.add(jchk_italic); jpl3.add(new JLabel("请选择字号")); jpl3.add(jcbo); jpc.add(jl); jpl.add(jpl1); jpl.add(jpl2); jpl.add(jpl3); jp2.add(new JLabel("请选择字体")); jp2.add(jlst); jp3.add(jbtninput); jp3.add(jbtnexit); jps.add(jpl); jps.add(jp2); jps.add(jp3); //jpl.setBackground(new Color(0,255,255)); jp.add(jpc,BorderLayout.CENTER); jp.add(jps,BorderLayout.SOUTH); jf.add(jp); //jf.add(jpl); jf.setVisible(true); } @Override public void valueChanged(ListSelectionEvent e) { // TODO Auto-generated method stub fontset(); } @Override public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub fontset(); } private void fontset() { // TODO Auto-generated method stub int fv=Font.PLAIN; if(jchk_bold.isSelected()) fv=Font.PLAIN|Font.BOLD; if(jchk_italic.isSelected()) fv=fv|Font.ITALIC; Object folst=jlst.getSelectedValue(); String fs="宋体"; if(!(folst==null)) { fs=folst.toString(); } Object focbo=jcbo.getSelectedItem(); int fn=12; if(!(focbo==null)) { fn=Integer.parseInt(focbo.toString()); } jl.setFont(new Font(fs,fv,fn)); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==jrbtn_red) { jl.setForeground(new Color(255,0,0)); } if(e.getSource()==jrbtn_green) { jl.setForeground(new Color(0,255,0)); } if(e.getSource()==jrbtn_blue) { jl.setForeground(new Color(0,0,255)); } if(e.getSource()==jchk_bold) { fontset(); } if(e.getSource()==jchk_italic) { fontset(); } if(e.getSource()==jchk_italic) { String str=JOptionPane.showInputDialog("请输入内容:"); } if(e.getSource()==jbtnexit) { int jopvalue=JOptionPane.showConfirmDialog(null,"确定退出吗?",null, JOptionPane.YES_NO_OPTION); if(jopvalue==JOptionPane.YES_NO_CANCEL_OPTION); System.exit(0); } } public static void main(String[] args) { new 总的(); } }