swing禁用最大化、去掉java图标、设置窗口为中央显示以及多线程

1.禁用最大化窗口
f.setResizable(false);
2.去掉JFrame的java图标
Image icon = Toolkit.getDefaultToolkit().getImage("");
f.setIconImage(icon);
3.设置窗口为中央显示
a.适用所有窗口
public void centerWindow(Component component) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension scmSize = toolkit.getScreenSize();
Dimension size = component.getPreferredSize();
int width = component.WIDTH,
height = component.HEIGHT;

component.setLocation(scmSize.width / 2 - (width / 2),
scmSize.height / 2 - (height / 2));
}

centerWindow(f);

b.不是对所有的都适用,如对装有Image图象的ImagePanelPanel面板的窗口就不行
setLocationRelativeTo(null);

4.swing 多线程
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TestThread extends JFrame {
JPanel jPanel1 = new JPanel();
JButton startButton = new JButton();
JButton stopButton = new JButton();
MyThread thread = null;

public TestThread() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
startButton.setText("start");
startButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
startButton_actionPerformed(e);
}
});
stopButton.setText("stop");
stopButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
stopButton_actionPerformed(e);
}
});
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(startButton, null);
jPanel1.add(stopButton, null);
}

void startButton_actionPerformed(ActionEvent e) {
if (thread != null)
thread.stop();
thread = new MyThread();
thread.start();
}

void stopButton_actionPerformed(ActionEvent e) {
if (thread != null)
thread.stop();
thread = null;
}

public static void main(String[] args) {
TestThread test = new TestThread();
test.setSize(300, 400);
test.show();
}

private class MyThread extends Thread {
public MyThread() {
}

public void run() {
while (true) {
try {
sleep(1000);
} catch (InterruptedException e) {
}
System.out.println("this is a test!");
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值