JFrame
举例:
public class JFrameDemo2 {
public static void main(String[] args) {
new MyJFrame().init();
}
}
class MyJFrame extends JFrame{
public void init(){
this.setBounds(10,10,200,200);
this.setVisible(true);
this.setBackground(Color.BLACK);
JLabel label = new JLabel("欢迎来到Java学习");
this.add(label);
//文本居中
label.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container container=this.getContentPane();
container.setBackground(Color.BLUE);
}
}
运行结果:
JDialog弹窗
JDialog自带关闭事件
举例:
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(500,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭
Container container=this.getContentPane();
container.setLayout(null);//绝对布局
//按钮
JButton button = new JButton("请点击这个按钮");
button.setBounds(20,20,300,100);
//点击事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
new MyDialogFrame();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口,点击事件
class MyDialogFrame extends JDialog{
public MyDialogFrame() {
this.setVisible(true);
this.setBounds(100,100,200,200);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);弹窗默认有关闭事件
Container container = this.getContentPane();
container.setLayout(null);
//container.add(new JLabel("hello"));
JLabel label = new JLabel("欢迎来到Java学习");
container.add(label);
}
}
运行结果:
Icon图标
图标举例:
//图标,实现类,
public class Icon extends JFrame implements javax.swing.Icon {
private int width;
private int height;
public Icon(){
}
public Icon(int width, int height) {
this.width = width;
this.height = height;
}
public void init(){
Icon icon = new Icon(20, 20);
//图标可以放在标签上,也可以放在按钮上
JLabel label = new JLabel("IconTest",icon,SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Icon().init();
}
@Override
public void paintIcon(Component c, Graphics g, int i, int i1) {
g.fillOval(i,i1,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
图片:
public class ImageIconDemo extends JFrame{
public ImageIconDemo() {
JLabel label = new JLabel("ImageIcon");
//获取图片地址
URL url = ImageIconDemo.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setBounds(100,100,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
运行结果:
JScroll
JPanel:
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//10,10表示间距
JPanel panel1= new JPanel(new GridLayout(1,3));
JPanel panel2= new JPanel(new GridLayout(1,2));
JPanel panel3= new JPanel(new GridLayout(2,1));
JPanel panel4= new JPanel(new GridLayout(3,2));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setBackground(Color.BLACK);
this.setBounds(100,100,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
运行结果:
JScroll滚动条
public class JScollDemo extends JFrame {
public JScollDemo() {
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText("Shmily");
//面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,200,300);
this.setBackground(Color.BLACK);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScollDemo();
}
}
运行结果:
图片添加到按钮上
public class JButtonDemo extends JFrame {
public JButtonDemo()
{
Container container = this.getContentPane();
//将图片转化为图标
URL url = JButtonDemo.class.getResource("tx.jpg");
Icon icon = new ImageIcon(url);
//把图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片");
container.add(button);
this.setVisible(true);
this.setBackground(Color.BLACK);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo();
}
}
运行结果为:
单选框
public class JButtonDemo2 extends JFrame{
public JButtonDemo2()
{
Container container = this.getContentPane();
//将图片转化为图标
URL url = JButtonDemo.class.getResource("tx.jpg");
Icon icon = new ImageIcon(url);
//单选框
JRadioButton radioButton1 = new JRadioButton("1");
JRadioButton radioButton2 = new JRadioButton("2");
JRadioButton radioButton3 = new JRadioButton("3");
//由于单选框只能选择一个,分组,一次只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1,BorderLayout.CENTER);
container.add(radioButton2,BorderLayout.NORTH);
container.add(radioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setBackground(Color.BLACK);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo2();
}
}
运行结果:
复选框
public class JButonDemo3 extends JFrame {
public JButonDemo3()
{
Container container = this.getContentPane();
//将图片转化为图标
URL url = JButtonDemo.class.getResource("tx.jpg");
Icon icon = new ImageIcon(url);
//多选框
JCheckBox checkBox1 = new JCheckBox("checkBox1");
JCheckBox checkBox2= new JCheckBox("checkBox2");
JCheckBox checkBox3= new JCheckBox("checkBox3");
container.add(checkBox1,BorderLayout.NORTH);
container.add(checkBox2,BorderLayout.SOUTH);
container.add(checkBox3,BorderLayout.CENTER);
this.setVisible(true);
this.setBackground(Color.BLACK);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButonDemo3();
}
}
Conbobox下拉框
public class ConboboxDemo extends JFrame {
public ConboboxDemo() {
Container container = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("即将上线");
status.addItem("已下架");
container.add(status);
this.setVisible(true);
this.setBounds(100,100,300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ConboboxDemo();
}
}
列表框
public class ConboboxDemo2 extends JFrame {
public ConboboxDemo2() {
Container container = this.getContentPane();
JComboBox status = new JComboBox();
//生成列表的内容
String []contents={"1","2","3"};//静态
Vector contents = new Vector();//动态
//列表中需要放入数据
JList list = new JList(contents);
contents.add("1");//动态
contents.add("2");//动态
contents.add("3");//动态
contents.add("4");//动态
container.add(status);
this.setVisible(true);
this.setBounds(100,100,300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ConboboxDemo2();
}
}
//一般是动态扩容的
运行结果:
文本框
public class testTextDemo1 extends JFrame {
public testTextDemo1(){
Container container = getContentPane();
JTextField textField = new JTextField("hello");
JTextField textField2 = new JTextField("world",20);
container.add(textField,BorderLayout.CENTER);
container.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setBackground(Color.BLACK);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(200,300);
}
public static void main(String[] args) {
new testTextDemo1();
}
}
密码框
public class testTextDemo2 extends JFrame {
public testTextDemo2(){
Container container = getContentPane();
JPasswordField passwordField = new JPasswordField();//密码
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setBackground(Color.BLACK);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(200,300);
}
public static void main(String[] args) {
new testTextDemo2();
}
}
源代码参考GUI-Study——lesson5,6