JAVA进阶(4)--Swing

JAVA进阶(4)--Swing,第1张

JAVA进阶(4)--Swing Jframe窗口
package Swing;

import javax.swing.Jframe;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;


public class TestJframe {

	
	public void init() {
		
		
		Jframe jframe = new Jframe("窗口的title");
		jframe.setVisible(true);
		jframe.setBounds(100, 100, 200, 200);
		
		
		
		JLabel label = new JLabel("text");
		jframe.add(label);
		
		
		label.setHorizontalAlignment(SwingConstants.CENTER);
		
		
		jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
	}
	
	public static void main(String[] args) {
		
		
		new TestJframe().init();
		
	}
	
}
JDialogd窗
package Swing;

import java.awt.Container;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;



@SuppressWarnings("serial")
public class TestDialog extends Jframe{

	
	public TestDialog() {
		
		this.setVisible(true);
		this.setSize(700, 500);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		
		Container container = this.getContentPane();
		
		container.setLayout(null);
		
		
		JButton button = new JButton("点击d出一个对话框");
		button.setBounds(30, 30, 200, 200);
		
		
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
				new MyDialog();
			}
			
		});
		
		container.add(button);
	}
	
	public static void main(String[] args) {
		
		new TestDialog();
		
	}
	
}


@SuppressWarnings("serial")
class MyDialog extends JDialog{
	
	public MyDialog() {
		this.setVisible(true);
		this.setBounds(100, 100, 500, 500);
		//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		Container container = this.getContentPane();
		container.setLayout(null);
		container.add(new Label("text"));
				
	}
	
}
标签

label

new JLabel("text");

Icon图标

package Swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.Jframe;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;


@SuppressWarnings("serial")
public class TestIcon extends Jframe implements Icon{

	private int width;
	private int height;
	
	
	public TestIcon() {}
	
	
	public TestIcon(int width, int height) {
		
		this.width = width;
		this.height = height;
		
	}
	
	public void init() {
		
		TestIcon testIcon = new TestIcon(35, 35);
		
		JLabel label = new JLabel("test", testIcon, SwingConstants.CENTER);
		
		Container container = getContentPane();
		container.add(label);
		container.setBackground(Color.PINK);
		
		this.setBounds(100, 100, 500, 500);
		this.setVisible(true);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
	}
	
	public static void main(String[] args) {
	
		new TestIcon().init();
	
	}

	@Override
	public void paintIcon(Component c, Graphics g, int x, int y) {
		// TODO Auto-generated method stub
		g.setColor(Color.GRAY);
		g.fillOval(x, y, width, height);
		
	}

	@Override
	public int getIconWidth() {
		// TODO Auto-generated method stub
		return this.width;
	}

	@Override
	public int getIconHeight() {
		// TODO Auto-generated method stub
		return this.height;
	}
	
}
面板
package Swing;

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.Jframe;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

@SuppressWarnings("serial")
public class TestJPanel extends Jframe{

	public TestJPanel() {
		
		Container container = this.getContentPane();
		
		container.setLayout(new GridLayout(2, 1, 10, 10));
				
		JPanel panel1 = new JPanel(new GridLayout(1, 3));
		
		panel1.add(new JButton("1"));
		panel1.add(new JButton("2"));
		panel1.add(new JButton("3"));
		
		container.add(panel1);
		
		this.setVisible(true);
		this.setSize(500, 500);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	
	}
	
	public static void main(String[] args) {
		new TestJPanel();
	}
	
}

JScrollPanel滚动条

package Swing;

import java.awt.Container;

import javax.swing.Jframe;
import javax.swing.JScrollPane;
import javax.swing.Jtextarea;
import javax.swing.WindowConstants;


public class TestJScroll extends Jframe{

	public TestJScroll() {
		
		Container container = this.getContentPane();
		
		
		Jtextarea textarea = new Jtextarea(20, 50);
		textarea.setText("文本域");
		
		
		JScrollPane scrollPane = new JScrollPane(textarea);
		container.add(scrollPane);
		
		this.setVisible(true);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setBounds(100, 100, 200, 200);
		
	}
	
	public static void main(String[] args) {
		new TestJScroll();
	}
	
}

注:以上代码均可直接运行,放在Swing包内

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5707585.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存