首页 > 代码库 > JAVA简单Swing图形界面应用示例

JAVA简单Swing图形界面应用示例

JAVA简单Swing图形界面应用示例


package org.rui.hello;

import javax.swing.JFrame;
/**
 * 简单的swing窗口
 * @author lenovo
 *
 */
public class HelloSwing {
	public static void main(String[] args) {
		JFrame frame=new JFrame("hello Swing");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(300,200);
		frame.setVisible(true);
	}

}


package org.rui.hello;

import java.util.concurrent.TimeUnit;

import javax.swing.JFrame;
import javax.swing.JLabel;
/**
 * JFream 中添加 一个JLabel来使事情变得更有趣
 * @author lenovo
 *
 */
public class HelloLabel {
	
	public static void main(String[] args) throws InterruptedException {
		JFrame frame=new JFrame("hello swing");
		JLabel label=new JLabel("A label");
		frame.add(label);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(300,100);
		frame.setVisible(true);
		TimeUnit.SECONDS.sleep(1);
		label.setText("Hey! this is Different!");
		
	}

}




package org.rui.hello;

import java.util.concurrent.TimeUnit;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 * swing 有它自已的专用线程来接收UI事件并更新屏幕
 * 
 * @author lenovo
 * 
 */
public class SubmitLabelManipulationTask {

	public static void main(String[] args) throws InterruptedException {
		JFrame frame = new JFrame("Hello Swing");
		final JLabel label = new JLabel("A Label");
		frame.add(label);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(300, 100);
		frame.setVisible(true);
		TimeUnit.SECONDS.sleep(1);
		//专用线程
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				label.setText("hey! this is different");

			}
		});
	}
}




package org.rui.hello;

import java.util.concurrent.TimeUnit;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class SubmitSwingProgram extends JFrame {
	JLabel label;

	public SubmitSwingProgram() {
		super("hello swing");
		label = new JLabel("A Label");
		add(label);
		setSize(300, 100);
		setVisible(true);
	}

	//
	static SubmitSwingProgram ssp;
	public static void main(String[] args) throws InterruptedException {
		//实例对象
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				ssp = new SubmitSwingProgram();
			}
		});
		
		TimeUnit.SECONDS.sleep(1);
		//设置改变文字
		SwingUtilities.invokeLater(new Runnable(){
			@Override
			public void run(){
				ssp.label.setText("hey! this is different!");
			}
		});
		
	}

}



package org.rui.hello;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
 * 这可能是一个你想要自已使用的工具,
 * 要想使用它,你的应用就必须位于一个JFrame中
 * 静态的run方法可以将视窗的标题设置为类的简单名
 * @author lenovo
 *
 */
public class SwingConsole {

	public static void run(final JFrame f, final int width, final int height) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				f.setTitle(f.getClass().getSimpleName());
				f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				f.setSize(width,height);
				f.setVisible(true);

			}
		});
	}

}