java入门

Java界面制作的组件添加组合,向容器中添加组件,对象式的编程方式??

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JLJB {
	public static void main (String[] args) {
		JFrame frame = new JFrame("JLabelJButton 使用");
		Container contentPane = frame.getContentPane();
		Icon icon1 = new ImageIcon("E:\\TGP\\Data\\cf_skin\\100.png");
		JPanel panel = new JPanel();
		JLabel label = new JLabel("Hello world!",icon1,JLabel.RIGHT);
		JButton button = new JButton("click",icon1);
		label.setHorizontalTextPosition(JLabel.CENTER);
		label.setBackground(Color.red);
		label.setForeground(Color.green);
		button.setHorizontalTextPosition(JButton.RIGHT);
		button.setBackground(Color.yellow);
		button.setForeground(Color.blue);
		contentPane.add(panel);
		panel.add(label);
		panel.add(button);
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {System.exit(0);}
		});
	}
}

 

posted on 2017-09-09 22:01  暗影蔽日  阅读(80)  评论(0编辑  收藏  举报

导航