importjavax.swing.*;importjava.util.concurrent.*;publicclassHelloLabel{publicstaticvoidmain(String[]args)throwsException{JFrame frame =newJFrame("Hello Swing");JLabel label =newJLabel("A Label");frame.add(label);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300,100);frame.setVisible(true);TimeUnit.SECONDS.sleep(1);label.setText("Hey! This is Different!");}}
import javax.swing.*;
import java.util.concurrent.*;
public class SubmitLabelManipulationTask {
public static void main(String[] args) throws Exception{
JFrame frame = new JFrame("Hello Swing");
final JLabel label=new JLabel("A Label");
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,100);
frame.setVisible(true);
TimeUnit.SECONDS.sleep(1);
SwingUtilities.invokeLater(new Runnable(){
public void run(){
label.setText("Hey!This is Different!");
}
});
}
}
import javax.swing.*;
public class SwingConsole {
public static void run(final JFrame f,final int width,final int height){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width, height);
f.setVisible(true);
}
});
}
}
import javax.swing.*;
import java.awt.*;
import static 一个显示框架.SwingConsole.*;
public class Button1 extends JFrame {
private JButton
b1=new JButton("Button 1"),
b2=new JButton("Button 2");
public Button1(){
setLayout(new FlowLayout());
add(b1);
add(b2);
}
public static void main(String[] args){
run(new Button1(),200,100);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static 一个显示框架.SwingConsole.*;
public class Button2 extends JFrame{
private JButton
b1= new JButton("Button 1"),
b2= new JButton("Button 2");
private JTextField txt= new JTextField(10);
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String name=((JButton)e.getSource()).getText(); //强制转换
txt.setText(name);
}
}
private ButtonListener bl=new ButtonListener();
public Button2(){
b1.addActionListener(bl);
b2.addActionListener(bl);
setLayout(new FlowLayout());
add(b1);
add(b2);
add(txt);
}
public static void main(String[] args){
run(new Button2(),200,150);
}
}