import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class LoginFrame extends JFrame {
JButton button = new JButton("点击我");
JButton button1 = new JButton("ok");
public LoginFrame(){
super();
this.setResizable(false);
this.setSize(new Dimension(300, 205));
this.setTitle("第一个窗体");
this.setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocation(300, 200);
this.setVisible(true);
this.getContentPane().add(button, null);
button.setBounds(new Rectangle(111, 70, 78, 27));
button.addActionListener(new LoginOKAction());//给按钮加事件监听器
}
class LoginOKAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
// JFrame jf = new JFrameTest();
// jf.setSize(300,300);
JFrame jf = new SwingThreadTest3();
jf.pack();
jf.setVisible(true);
}
}
public static void main(String[] args) {
new LoginFrame();
}
}
class JFrameTest extends JFrame{
JButton jb1 = new JButton("java let's go ");
public JFrameTest(){
this.setSize(300, 300);
this.setTitle("弹出框");
this.add(jb1);
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class SwingThreadTest3 extends JFrame {
private static final long serialVersionUID = 1L;
private static final String STR = "Completed : ";
private JProgressBar progressBar = new JProgressBar();
private JTextField text = new JTextField(10);
private JButton start = new JButton("Start");
private JButton end = new JButton("End");
private boolean flag = false;
private int count = 0;
private GoThread t = null;
private Runnable run = null;//更新组件的线程
public SwingThreadTest3() {
this.setLayout(new FlowLayout());
add(progressBar);
text.setEditable(false);
add(text);
add(start);
add(end);
start.addActionListener(new Start());
end.addActionListener(new End());
run = new Runnable(){//实例化更新组件的线程
public void run() {
progressBar.setValue(count);
text.setText(STR + String.valueOf(count) + "%");
}
};
}
private void go() {
while (count < 100) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (flag) {
count++;
SwingUtilities.invokeLater(run);//将对象排到事件派发线程的队列中
}
}
}
private class Start implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = true;
if(t == null){
t = new GoThread();
t.start();
}
}
}
class GoThread extends Thread{
public void run() {
//do something...
go();
}
}
private class End implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = false;
}
}
public static void main(String[] args) {
SwingThreadTest3 fg = new SwingThreadTest3();
fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fg.setSize(300, 100);
fg.setVisible(true);
}
}