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!");
}
}
}
}

你可能感兴趣的:(java,多线程,thread,swing,F#)