GUI用户界面的一个简单实例

这篇博客写得比较赶,就不赘述了。
内含有设置框架,面板,按钮怎么嵌在一起,插入图片,设置字体和颜色,前景色背景色,位置等。重点在实现功能,界面并不美观。
私以为,这么多个东西怎么看呢?就应该把一些行注释掉就知道怎么看了。
image是在src下面的文件夹

package gui;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class TestSwingCommonFeatures extends JFrame{
	public TestSwingCommonFeatures() {
		ImageIcon myIcon=new ImageIcon("src/image/sazi.jpg");
		setLayout(new GridLayout(1,1,5,5));
		add(new JLabel(myIcon));
		JPanel p1=new JPanel(new FlowLayout(FlowLayout.LEFT,2,2));
		JButton jbtLeft=new JButton("Left");
		JButton jbtCenter=new JButton("Center");
		JButton jbtRight=new JButton("Right");
		jbtLeft.setBackground(Color.WHITE);
		jbtCenter.setForeground(Color.GREEN);
		jbtRight.setToolTipText("This is the right button");  //when your mouse put on right button , it will display text "This is the right button"
		p1.add(jbtLeft);
		p1.add(jbtCenter);
		p1.add(jbtRight);
		p1.setBorder(new TitledBorder("Three Buttons"));
		
		Font largeFont=new Font("TimesRoman",Font.BOLD,20);
		Border lineBorder=new LineBorder(Color.BLACK,2);
		
		JPanel p2=new JPanel (new GridLayout(1,2,5,5));
		JLabel jlblRed=new JLabel("Red");
		JLabel jlblOrange=new JLabel("Orange");
		jlblRed.setForeground(Color.RED);           //foreground is the color of font
		jlblOrange.setForeground(Color.ORANGE);
		jlblRed.setFont(largeFont);
		jlblOrange.setFont(largeFont);
		jlblRed.setBorder(lineBorder);
		jlblOrange.setBorder(lineBorder);
		p2.add(jlblRed);
		p2.add(jlblOrange);
		p2.setBorder(new TitledBorder("Two Labels"));   //text with borderline
		setLayout(new GridLayout(2,1,5,5));
		add(p1);
		add(p2);
		
	}
	

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
	    JFrame frame=new TestSwingCommonFeatures();
		frame.setTitle("TestSwingCommonFeatures");
		frame.setSize(300,550);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

	}

}

结果
在这里插入图片描述
照片是我前天拍的,哈哈哈~

猜你喜欢

转载自blog.csdn.net/alike_meng/article/details/83624150