Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Monday, November 19, 2007

example program on java Generic GUI program (xgui)

The following kcomplete GUI program can be inserted immediately after the header comments and optional package declaration. It is a very simple subclass of JFrame. You will have to add a lot to it, but this gives you a good starting point to prevent "blank screen" anxiety.

  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

///////////////////////////////////////////////////////////////////// ${Identifier}
public class ${Identifier} extends JFrame {
//================================================================ constants
private static final String PROMPT = "Do you really want to quit?";

//=================================================================== fields
//... GUI components referenced by listeners, model refs, ....

//============================================================== constructor
public ${Identifier}() {
//... Build components.
JButton quitButton = new JButton("Quit");

//... Add Listeners
quitButton.addActionListener(new QuitAction());

//... Create content pane, set layout and border.
JPanel content = new JPanel();
content.setLayout(new BorderLayout(5, 5));
content.setBorder(BorderFactory.createEmptyBorder(12,12,12,12));
content.add(quitButton);

//... Set window attributes.
super.setContentPane(content);
super.pack(); // Layout components.
super.setTitle("${Identifier}");
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setLocationRelativeTo(null); // Center window.
}


/////////////////////////////////////////// innner listener class QuitAction
class QuitAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
int confirm = JOptionPane.showConfirmDialog(${Identifier}.this, PROMPT);
if (confirm == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
}

//===================================================================== main
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
${Identifier} window = new ${Identifier}();
window.setVisible(true);
}
});
}
}

No comments:

Post a Comment

Archives