import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
protected JLabel pLabel;
protected JTextField tf;
protected JTextArea ta;
protected JButton btn1,btn2;
public MyFrame(String s){
super(s);
Container contentPane=getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel ptf=new JPanel(new GridLayout(1,2));
pLabel=new JLabel("请输入",JLabel.RIGHT);
ptf.add(pLabel);
tf=new JTextField(12);
ptf.add(tf);
contentPane.add(ptf,BorderLayout.NORTH);
ta=new JTextArea("这是一个文本区");
JScrollPane scrollpane=new JScrollPane(ta);
contentPane.add(scrollpane,BorderLayout.CENTER);
JPanel pbtn=new JPanel();
btn1=new JButton("按钮1");
pbtn.add(btn1);
btn2=new JButton("按钮2");
pbtn.add(btn2);
contentPane.add(pbtn,BorderLayout.SOUTH);
setSize(400,300);
setVisible(true);
}
public static void main(String []args){
MyFrame w =new MyFrame("应用程序");
}
}
第二个
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JColorChooser;
public class MyFrame2 extends MyFrame implements ActionListener{
public MyFrame2(String s){
super(s);
tf.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==tf){
ta.append("\n"+tf.getText());
tf.setText("");
}
else if(e.getSource()==btn1){
Color tempColor=JColorChooser.showDialog(this,"调色板",Color.red);
ta.setBackground(tempColor);
}
else if(e.getSource()==btn2){
Color tempColor=JColorChooser.showDialog(this,"调色板",Color.red);
ta.setForeground(tempColor);
}
}
public static void main(String []args){
MyFrame2 w=new MyFrame2("窗口应用程序");
}
}
注释越多越好 最好每行一个 麻烦啦 ...最好5分都拿出来了