不想花太多时间在UI上,因为GUI一般都大同小异,需要使用时再翻阅文档应该也够了。java里的图形界面由javax.swing.*支持。现在给出一个粗糙的例子:
- public class SwingTest {
- static void testJFrame() {
- JFrame frame = new JFrame("Window Title");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(400, 300);
- frame.setLayout(new FlowLayout());
-
- final JLabel label = new JLabel("label text");
-
- final JTextArea textArea = new JTextArea();
- textArea.addKeyListener(new KeyAdapter() {
- @Override
- public void keyTyped(KeyEvent e) {
- label.setText(label.getText() + e.getKeyChar());
- }
- });
-
- final JButton button = new JButton("button text");
- button.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- textArea.append(((JButton)e.getSource()).getText());
- }
- });
- button.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- JPopupMenu menu = new JPopupMenu();
- menu.add(new JMenuItem("Item"));
- menu.show(button, 23, 35);
- }
- });
-
- frame.add(label);
- frame.add(button);
- frame.add(textArea);
- frame.setVisible(true);
- }
- }
阅读(913) | 评论(0) | 转发(0) |