SWING is GUI tool kit for JAVA.
You can using it. If you installing plug-in.
This post is JButton.
JButton is button of SWING.
If you executing swing, can see this picture.
There are 2 type button. (JButton and Button)
JButton is for SWING and Button is for AWT.
I use JButton.
This is my sample source.
You can making Button likes this picture.
And look, left side!!
I checked 'Variable' and 'text'.
Variable is JButton's name.
You possible to change name.
And text is display text.
You can check right side.
Okay, go to java source.
-
-
-
private JButton btnServerOpen,btnClear,btnSend;
-
-
-
-
-
public static JButton btnServerClose;
public static boolean disconnected=true;
-
public static boolean echoEnable=false;
-
/**
* Launch the application.
*/
public static void main
(String[] args
) {
-
public void run() {
try {
serverSWING window = new serverSWING();
window.frame.setVisible(true);
-
e.printStackTrace();
}
}
});
}
Line 1 : I implement 'ActionListener'. This similar Android's 'onClickListener'.
Line 5 : I declared JButton.
Line 19 : Starting SWING.
/**
* Create the application.
*/
public serverSWING() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
-
frame.setBounds(100, 100, 640, 480);
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
-
portNum.setText("5000");
portNum.setBounds(549, 10, 59, 21);
frame.getContentPane().add(portNum);
portNum.setColumns(10);
-
lblPort.setBounds(503, 13, 44, 15);
frame.getContentPane().add(lblPort);
btnServerOpen =
new JButton("SERVER OPEN");
btnServerOpen.addActionListener(this);
btnServerOpen.setBounds(493, 41, 119, 23);
frame.getContentPane().add(btnServerOpen);
-
commText.setText("Plz, type port and press connect button.");
commText.setBounds(12, 10, 471, 389);
frame.getContentPane().add(commText);
btnServerClose =
new JButton("SERVER Close");
btnServerClose.addActionListener(this);
btnServerClose.setBounds(493, 74, 119, 23);
frame.getContentPane().add(btnServerClose);
-
btnClear.addActionListener(this);
btnClear.setBounds(493, 107, 119, 23);
frame.getContentPane().add(btnClear);
-
sendText.setBounds(12, 409, 471, 21);
frame.getContentPane().add(sendText);
-
btnSend.addActionListener(this);
btnSend.setBounds(493, 407, 119, 23);
frame.getContentPane().add(btnSend);
-
checkEcho.setBounds(493, 375, 119, 23);
frame.getContentPane().add(checkEcho);
checkEcho.addItemListener(this);
}
Line 5 : initailize to serverSWING.
Line 27~31 : GUI's position, registration listener and make button.
This auto making source about actionListener.
@Override
-
}
Add code.
@Override
-
if(e.getSource()==btnServerOpen){
String getPort=portNum.
getText();
if(!getPort.isEmpty()){
int port=
Integer.
parseInt(getPort
);
portOpen(port);
}else{
commText.setText("Not found PORT number");
}
}
}
Line 3 : 'e.getSource()== btnServerOpen' similar android's 'e.getId()'.
but 'getID()' is integer. So android possible to using 'switch-case'.