【JAVA语言程序设计基础篇】--图形用户界面基础--Swing GUI组件的公共特性

来源:互联网 发布:区块链的共识机制 算法 编辑:程序博客网 时间:2024/05/21 10:54

package chapter12;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Font;import java.awt.GridLayout;import javax.swing.JPanel;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.border.LineBorder;//线框import javax.swing.border.TitledBorder;//标题框@SuppressWarnings("serial")public class TestSwingCommonFeatures extends JFrame{public TestSwingCommonFeatures(){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");p1.add(jbtleft);p1.add(jbtcenter);p1.add(jbtright);p1.setBorder(new TitledBorder("three buttons"));//-------------------------------------//Font largeFont =new Font ("TimesRoman",Font.BOLD,20);//字体JPanel p2 = new JPanel (new FlowLayout(FlowLayout.LEFT,5,5));JButton jbtred = new JButton ("red");JButton jbtorange = new JButton("orange");jbtred.setFont(largeFont);jbtred.setBackground(Color.red);jbtorange.setForeground(Color.orange);jbtorange.setToolTipText("this is a orange button");p2.add(jbtred);p2.add(jbtorange);p2.setBorder(new LineBorder(Color.green,4));//被下一行覆盖p2.setBorder(new TitledBorder("a"));p2.setLayout(new GridLayout(1,2,1,10));setLayout(new FlowLayout(FlowLayout.LEFT,30,30));setLayout(new GridLayout(1,2,5,10));//下一行有一个新的setlayout 这一样的setlayout被覆盖,就不显示了add(p1);add(p2);}public static void main(String[] args) {JFrame frame = new TestSwingCommonFeatures();frame.setTitle("test swing common features");frame.setSize(500, 150);frame.setLocationRelativeTo(null);//放中间frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}




展示

Font 字体

Color 颜色

Border 框




0 0