GUI使用者介面的一個簡單例項
阿新 • • 發佈:2018-11-17
這篇部落格寫得比較趕,就不贅述了。
內含有設定框架,面板,按鈕怎麼嵌在一起,插入圖片,設定字型和顏色,前景色背景色,位置等。重點在實現功能,介面並不美觀。
私以為,這麼多個東西怎麼看呢?就應該把一些行註釋掉就知道怎麼看了。
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);
}
}
結果
照片是我前天拍的,哈哈哈~