扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条

/**//*
* AwtSwing.java
* @author Fancy
*/
import java.awt.BorderLayout;
import java.awt.Button;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

public final class AwtSwing ...{

public static void main(String[] args) ...{
AwtSwing as = new AwtSwing();
as.show();
}
JFrame frame = new JFrame("Test AWT and SWING");
JDesktopPane jdp = new JDesktopPane();
JInternalFrame jif1 = new JInternalFrame("controls");
JInternalFrame jif2 = new JInternalFrame("cover");

public AwtSwing() ...{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(jdp);
jif1.setContentPane(new JPanel());
jif2.setContentPane(new JPanel());
jif1.getContentPane().setLayout(new BorderLayout());
jif1.getContentPane().add(new Button("AWT Button"), BorderLayout.WEST);
jif1.getContentPane().add(new JButton("Swing Button"),
BorderLayout.EAST);
jif1.setSize(200, 100);
jif2.setSize(200, 100);
jdp.add(jif1);
jdp.add(jif2);
frame.setSize(240, 140);
}

public void show() ...{
frame.setVisible(true);
jif1.setVisible(true);
jif2.setVisible(true);
}
}

/** *//**
* @(#) TestFrame.java
* @author James
*/
import javax.swing.*;
import java.awt.event.*;

public class TestFrame extends JFrame ...{
private int counter = 0;

public TestFrame() ...{
/**//* 使用匿名类添加一个窗口监听器 */
addWindowListener(new WindowAdapter() ...{
public void windowClosing(WindowEvent e) ...{
System.out.println(
"Exit when Closed event");
//退出应用程序
System.exit(0);
}

public void windowActivated(WindowEvent e) ...{
// 改变窗口标题
setTitle("Test Frame " + counter++);
}
});
// 设置窗口为固定大小
setResizable(false);
setSize(200, 150);
}

public static void main(String[] args) ...{
TestFrame tf = new TestFrame();
tf.show();
}
}
JFrame frame = new JFrame("Frame's Title");
frame.setSize(400, 300);
frame.setVisible(true);|
windowActivated(WindowEvent e) |
窗口得到焦点时触发 |
|
windowClosed(WindowEvent e) |
窗口关闭之后触发 |
|
windowClosing(WindowEvent e) |
窗口关闭时触发 |
|
windowDeactivated(WindowEvent e) |
窗口失去焦点时触发 |
|
windowDeiconified(WindowEvent e) |
|
|
windowIconified(WindowEvent e) |
|
|
windowOpened(WindowEvent e) |
窗口打开之后触发 |


/**//*
* TestButtons.java
* @author Fancy
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;

public final class TestButtons ...{

public static void main(String[] args) ...{
TestButtons tb = new TestButtons();
tb.show();
}
JFrame frame = new JFrame("Test Buttons");
JButton jButton = new JButton("JButton"); // 按钮
JToggleButton toggle = new JToggleButton("Toggle Button"); // 切换按钮
JCheckBox checkBox = new JCheckBox("Check Box"); // 复选按钮
JRadioButton radio1 = new JRadioButton("Radio Button 1"); // 单选按钮
JRadioButton radio2 = new JRadioButton("Radio Button 2");
JRadioButton radio3 = new JRadioButton("Radio Button 3");
JLabel label = new JLabel("Here is Status, look here."); // 不是按钮,是静态文本

public TestButtons() ...{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new java.awt.FlowLayout());
// 为一般按钮添加动作监听器
jButton.addActionListener(new ActionListener() ...{
public void actionPerformed(ActionEvent ae) ...{
label.setText("You clicked jButton");
}
});
// 为切换按钮添加动作监听器
toggle.addActionListener(new ActionListener() ...{
public void actionPerformed(ActionEvent ae) ...{
JToggleButton toggle = (JToggleButton) ae.getSource();
if (toggle.isSelected()) ...{
label.setText("You selected Toggle Button");
} else ...{
label.setText("You deselected Toggle Button");
}
}
});
// 为复选按钮添加条目监听器
checkBox.addItemListener(new ItemListener() ...{
public void itemStateChanged(ItemEvent e) ...{
JCheckBox cb = (JCheckBox) e.getSource();
label.setText("Selected Check Box is " + cb.isSelected());
}
});
// 用一个按钮组对象包容一组单选按钮
ButtonGroup group = new ButtonGroup();
// 生成一个新的动作监听器对象,备用
ActionListener al = new ActionListener() ...{
public void actionPerformed(ActionEvent ae) ...{
JRadioButton radio = (JRadioButton) ae.getSource();
if (radio == radio1) ...{
label.setText("You selected Radio Button 1");
} else if (radio == radio2) ...{
label.setText("You selected Radio Button 2");
} else ...{
label.setText("You selected Radio Button 3");
}
}
};
// 为各单选按钮添加动作监听器
radio1.addActionListener(al);
radio2.addActionListener(al);
radio3.addActionListener(al);
// 将单选按钮添加到按钮组中
group.add(radio1);
group.add(radio2);
group.add(radio3);
frame.getContentPane().add(jButton);
frame.getContentPane().add(toggle);
frame.getContentPane().add(checkBox);
frame.getContentPane().add(radio1);
frame.getContentPane().add(radio2);
frame.getContentPane().add(radio3);
frame.getContentPane().add(label);
frame.setSize(200, 250);
}

public void show() ...{
frame.setVisible(true);
}
}
/** *//**
* @(#) Test.java
* @author James
*/
import javax.swing.*;
import java.awt.event.*;

public class Test ...{
JButton b;
JRadioButton rb;

public Test() ...{
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(
new java.awt.FlowLayout());
b = new JButton("JButton");
rb = new JRadioButton("RadioButton");
ActionListener a = new ActionListener() ...{
public void actionPerformed(ActionEvent ae) ...{
if (ae.getSource() == b) ...{
System.out.println(
"You clicked the JButton");
} else ...{
System.out.println(
"You clicked the RadioButton");
}
}
};
b.addActionListener(a);
rb.addActionListener(a);
f.getContentPane().add(b);
f.getContentPane().add(rb);
f.pack();
f.show();
}

public static void main(String[] args) ...{
new Test();
}
}

/**//*
* TestTexts.java
* @author Fancy
*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public final class TestTexts extends JFrame ...{

public static void main(String[] args) ...{
TestTexts tt = new TestTexts();
tt.setVisible(true);
}
private JLabel label = new JLabel("Status");
private JTextField textField;
private JPasswordField pwdField;
private JTextArea textArea;

public TestTexts() ...{
super("Test Texts");
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.FlowLayout());
textField = new JTextField(15);
/**//* 监听文本光标移动事件 */
textField.addCaretListener(new CaretListener() ...{
public void caretUpdate(CaretEvent e) ...{
// 如果改变了内容,就可以即时更新 label 显示的内容
label.setText(textField.getText());
}
});
pwdField = new JPasswordField(15);
pwdField.setEchoChar('#');
textArea = new JTextArea(5, 15);
textArea.setLineWrap(true);
getContentPane().add(textField);
getContentPane().add(pwdField);
getContentPane().add(textArea);
getContentPane().add(label);
setSize(200, 200);
}
}|
FlowLayout |
将组件按从左到右从上到下的顺序依次排列,一行不能放完则折到下一行继续放置 |
|
BorderLayout |
将组件按东(右)、南(下)、西(左)、北(上)、中五个区域放置,每个方向最多只能放置一个组件(或容器)。 |
|
GridLayout |
形似一个无框线的表格,每个单元格中放一个组件 |
|
BoxLayout |
就像整齐放置的一行或者一列盒子,每个盒子中一个组件 |
|
BorderLayout.EAST |
东(右) |
|
BorderLayout.SOUTH |
南(下) |
|
BorderLayout.WEST |
西(左) |
|
BorderLayout.NORTH |
北(上) |
|
BorderLayout.CENTER |
中 |

/**//*
* TestPanels.java
* @author Fancy
*/
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public final class TestPanels extends JFrame ...{

public static void main(String[] args) ...{
TestPanels tp = new TestPanels();
tp.setVisible(true);
}

public TestPanels() ...{
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
for (int i = 0; i < 2; i++) ...{
panel.add(new JButton("Button 00" + i));
}
JTextArea textArea = new JTextArea(5, 15);
textArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(textArea);
getContentPane().add(panel, BorderLayout.NORTH);
getContentPane().add(scrollPane, BorderLayout.CENTER);
pack();
}
}
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。