**
**
1.JFrame类
(1)JFrame() 创建一个无标题的窗口
(2) JFrame(String title);创建一个标题为title的窗口
(3) 方法
import java.awt.*;
import javax.swing.*;
public class MyGUI {
public static void main(String[] args) {
JFrame f=new JFrame();
f.getContentPane().setBackground(Color.BLUE);
f.setSize(300,500);
f.setVisible(true);
}
}
2.标签
(1)JLabel() 创建空标签。
(2)JLabel(String Text):创建内容为text的标签
(3)JLabel(String text,int align):创建内容为text的标签,对齐方式为align的标签,JLabel.LEFT,JLabel.RIGHT、JLabel.CENTER。
(4)添加 add(标签对象名) #标签等组件不会自动显示,必须把它放到容器中 JFrame添加标签的方法是:先由getContentPane()方法获得主窗口的内容面板,再调用add(组件对象名)将组件加入
(5) 设置标签字体文字
(6)设置标签文字颜色(Color 类)
import java.awt.*;
import javax.swing.*;
public class MyGUI {
public static void main(String[] args) {
JFrame f=new JFrame();
JLabel l1=new JLabel("用户名",JLabel.LEFT);
JLabel l2=new JLabel("密码",JLabel.LEFT);
f.getContentPane().setBackground(Color.BLUE);
f.getContentPane().add(l1);
f.getContentPane().add(l2);
Font f1=new Font("黑体",Font.ITALIC,40);
l1.setFont(f1);
Color c=new Color(200,100,52);
l1.setForeground(c);
l2.setForeground(Color.yellow);
f.setSize(300,500);
f.setVisible(true);
}
}
3.按钮
import java.awt.*;
import javax.swing.*;
public class MyGUI {
public static void main(String[] args) {
JFrame f=new JFrame();
JButton b1=new JButton("确定");
f.getContentPane().add(b1);
f.setSize(300,500);
f.setVisible(true);
}
}
4.JPanel面板
import java.awt.*;
import javax.swing.*;
public class GCR {
public static void main(String[] args) {
JFrame f=new JFrame("练习");
JLabel l1=new JLabel("用户名");
JLabel l2=new JLabel("密码");
JButton b1=new JButton("确定");
JPanel p1=new JPanel();
p1.add(l1);
p1.add(l2);
p1.add(b1);
f.add(p1);
f.setSize(300,500);
f.setVisible(true);
}
}
5.布局管理器
布局类型
FlowLayout(int alignment,int horz,int vert)
import java.awt.*;
import javax.swing.*;
public class FlowLayoutWindow extends JFrame{
public FlowLayoutWindow() {
setLayout(new FlowLayout());
add(new JLabel("Buttons:"));
add(new JButton("Buttons 1"));
add(new JButton("2"));
add(new JButton("Buttons 3"));
add(new JButton("wang"));
add(new JButton("Buttons 5"));
}
public static void main(String[] args) {
FlowLayoutWindow w=new FlowLayoutWindow();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setTitle("wang");
w.pack();
w.setVisible(true);
}
}
GridLayout()
import java.awt.*;
import javax.swing.*;
public class Test1 extends JFrame {
static JButton button;
JTextArea tf,td;
JPasswordField tc;
public Test1() {
setLayout(new GridLayout(3,2));
add(new JLabel("请输入用户名 "));
add(td=new JTextArea());
add(new JLabel("请输入密码 "));
add(tc=new JPasswordField());
add(button=new JButton("确定"));
add(tf=new JTextArea());
}
public static void main(String args[]) {
Test1 dj=new Test1();
JPasswordField tf=new JPasswordField();
dj.pack();
dj.setSize(500,500);
dj.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Test1 extends JFrame implements ActionListener {
static JButton button;
JTextArea tf,td;
JPasswordField tc;
public Test1() {
setLayout(new GridLayout(3,2));
add(new JLabel("请输入用户名 "));
add(td=new JTextArea());
add(new JLabel("请输入密码 "));
add(tc=new JPasswordField());
add(button=new JButton("确定"));
add(tf=new JTextArea());
}
public static void main(String args[]) {
Test1 dj=new Test1();
JPasswordField tf=new JPasswordField();
dj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dj.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
button.addActionListener(dj);
dj.pack();
dj.setSize(500,500);
dj.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s=tc.getText().strip();
String v=td.getText();
String g="欢迎,000";
String h="用户名或密码错误";
if(s.equals("111")&&v.equals("000"))
tf.append(g);
else tf.append(h);
}
}