使用Font类
下边是例子
---------------------------------------------------------------------------------------------
import javaawtFont;
import javaxswingJFrame;
import javaxswingJLabel;
public class FontApp extends JFrame {
public FontApp() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setResizable(false);
getContentPane()setLayout(null);
JLabel lblNewLabel = new JLabel("a String size 18");
lblNewLabelsetFont(new Font(null, FontITALIC, 18));
lblNewLabelsetBounds(12, 30, 232, 29);
getContentPane()add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("b String size 14");
lblNewLabel_1setFont(new Font(null, FontBOLD, 14));
lblNewLabel_1setBounds(12, 97, 232, 29);
getContentPane()add(lblNewLabel_1);
setVisible(true);
}
public static void main(String[] args) {
new FontApp();
}
}
import javaawt;
import javaxswing;
public class file extends JFrame{
JButton jb; JFrame jf; JLabel jl1,jl2; JPanel jp1,jp2; JTextField jtf;
public file(){ //初始化 jb = new JButton("第一个按钮"); jl1 = new JLabel("第一个标签"); jl2 = new JLabel("你好,这是XXX的第一个java Swing程序"); jl2setFont(new Font("宋体",FontPLAIN,16));//设置字体样式 jp1 = new JPanel(); jp2 = new JPanel(); jtf = new JTextField(10); //添加组件 jp1add(jl1); jp1add(jtf); jp1add(jb); jp2add(jl2); //位置 thisadd(jp1,BorderLayoutNORTH); thisadd(jp2,BorderLayoutCENTER); //显示窗口 thissetTitle("XXX的第一个java Swing程序"); thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE); thissetSize(600,500); thissetVisible(true); } public static void main(String[] args) {
new file(); }}
很简单啊 给你现写的 运行一下看看和不合格
//正好以前注释过一个
import javaawt;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventKeyAdapter;
import javaawteventKeyEvent;
import javaawtimageMemoryImageSource;
import javautilRandom;
import javaxswingJDialog;
import javaxswingJPanel;
import javaxswingTimer;
public class CharacterRain extends JDialog implements ActionListener {
private static final long serialVersionUID = 1L;
private Random random = new Random();
private Dimension screenSize;
private JPanel graphicsPanel;
private final static int gap = 20;
private int[] posArr;
private int lines;
private int columns;
public static void main(String[] args) {
new CharacterRain();
}
public CharacterRain() {
initComponents();
}
private void initComponents() {
setLayout(new BorderLayout());
graphicsPanel = new GraphicsPanel();
add(graphicsPanel, BorderLayoutCENTER);
Toolkit defaultToolkit = ToolkitgetDefaultToolkit();
Image image = defaultToolkitcreateImage(new MemoryImageSource(0, 0,
null, 0, 0));
Cursor invisibleCursor = defaultToolkitcreateCustomCursor(image,
new Point(0, 0), "cursor");
setCursor(invisibleCursor);
KeyPressListener keyPressListener = new KeyPressListener();
thisaddKeyListener(keyPressListener);
thissetAlwaysOnTop(true);
thissetUndecorated(true);
thisgetGraphicsConfiguration()getDevice()setFullScreenWindow(this);
thissetDefaultCloseOperation(JDialogDISPOSE_ON_CLOSE);
setVisible(true);
screenSize = ToolkitgetDefaultToolkit()getScreenSize();
lines = screenSizeheight / gap; //用gap来分行
columns = screenSizewidth / gap; //用gap来分列
posArr = new int[columns + 1];
random = new Random();
for (int i = 0; i < posArrlength; i++) {
posArr[i] = randomnextInt(lines); //随机行数
}
new Timer(100, this)start();
}
private char getChr() {
return (char) (randomnextInt(94) + 33); //返回随机字符
}
public void actionPerformed(ActionEvent e) {
graphicsPanel repaint(); // Timer事件,graphicsPanel重绘
}
@SuppressWarnings("serial")
private class GraphicsPanel extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d setFont (getFont () deriveFont (Font BOLD));
g2d setColor(Color BLACK);
g2d fillRect (0, 0, screenSize width, screenSize height); // 设置背景色为黑色
int currentColumn = 0;
for (int x = 0; x < screenSizewidth; x += gap) {
int endPos = posArr[currentColumn]; //获得开始行位置
g2d setColor(Color GREEN);
g2ddrawString(StringvalueOf(getChr()), x, endPos gap); //画出随机开始行的字符
int cg = 0; //初始黑色
int length = 25; //字符列的长度
for (int j = endPos -length; j < endPos; j++) { //随机行逐列向上length行,为循环开始行
cg += 255/(length + 1); //渐变色递增,0,255,0为green
if (cg > 255) {
cg = 255;
}
g2dsetColor(new Color(0, cg, 0));
g2ddrawString(StringvalueOf(getChr()), x, j gap); //画出随机行后的15行字符,颜色从黑色渐变成绿色,逐行增加
}
posArr[currentColumn] += randomnextInt(5); //下落距离最快为4, 会产生加速下落的感觉
if ((posArr[currentColumn] -5) gap > getHeight()) { //如果行数位置大于屏幕高度,从新获取合适的行
posArr[currentColumn] = randomnextInt(lines);
}
currentColumn++; //下一列获取随机行
}
}
}
private class KeyPressListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if (egetKeyCode() ==KeyEventVK_ESCAPE) { //监听键盘事件,按ESC按键退出
Systemexit(0);
}
}
}
}
1首先定义三个输入框
JTextField firstNumber;
JTextField secontdNumber;
JTextField maxNumber;
2然后怎样生成并把它们放到界面上我就不多说了。
3接下来生成最大数按钮maxNumberButton和关闭按钮closeButton,并放在界面上。
然后让maxnNumberButton监听鼠标单击事件
maxNumberButtonaddMouseListener(new MouseAdapter() {
if (SwingUtilitiesisLeftMouseButton(e)) {//判断是否鼠标左键按下
//在这里获得第一个和第二个数并比较获得最大的数,当然,你还可以首先判断是否已经输入了两个数,判断输入的两个值是否是数值等等的。你也可以在外边实现一个方法,直接返回最大值。然后是把最大值显示出来。
//从输入框直接获得的是string,你要自己转换成数值,具体怎样做我就不说了。默认你已经获得了两个值,并得到最大值max,顺便说一下float和double是不能直接大于小于这样比较的。
maxNumbersetText("");//首先把之前显示的清除
maxNumbersetText(max);
}
});
关闭按钮处理类似
closeButtonaddMouseListener(new MouseAdapter() {
if (SwingUtilitiesisLeftMouseButton(e)) {
Systemexit(0);
}
});
以上就是关于java 用GUI写一个程序全部的内容,包括:java 用GUI写一个程序、java的GUI应用程序、怎样用JAVA的GUI(图形用户界面)来设计一个程序!求程序和详解。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)