- Engineering
- Computer Science
- i need help with this java assigment i have my...
Question: i need help with this java assigment i have my...
Question details
I need help with this java assigment, I have my code provided
and documents to help. Im not sure how to do GUIs
package lab1in;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ClientGUI extends JFrame {
// Variables declaration
private JButton connect;
private JPanel north;
private JLabel output;
private JPanel south;
private JLabel status;
private JButton stop;
private JButton submit;
//contructor that passes one parameter
public ClientGUI(String title) {
this.setTitle(title); //set the title
GridBagConstraints gridBagConstraints;
north = new JPanel();
status = new JLabel();
output = new JLabel();
south = new JPanel();
connect = new JButton();
submit = new JButton();
stop = new JButton();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
layout.columnWidths = new int[] {0, 15, 0, 15, 0, 15, 0, 15, 0, 15,
0, 15, 0, 15, 0, 15, 0, 15, 0, 15, 0};
layout.rowHeights = new int[] {0, 10, 0, 10, 0, 10, 0, 10, 0, 10,
0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0,
10, 0, 10, 0};
getContentPane().setLayout(layout);
status.setText("Status");
north.add(status);
output.setText("output");
output.setName("lblOutput");
output.setForeground(Color.RED);
north.add(output);
//set the button label to the grid panel
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 6;
gridBagConstraints.gridy = 0;
getContentPane().add(north, gridBagConstraints);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
getContentPane().add(south, gridBagConstraints);
connect.setText("Connect");
connect.setName("connect");
connect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
connectActionPerformed(evt);
System.out.println("Connect button pressed");
}
});
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 6;
gridBagConstraints.gridy = 32;
getContentPane().add(connect, gridBagConstraints);
submit.setText("Submit");
submit.setName("submit");
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
submitActionPerformed(evt);
System.out.println("Submit button pressed");
}
});
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 10;
gridBagConstraints.gridy = 32;
getContentPane().add(submit, gridBagConstraints);
stop.setText("Stop");
stop.setName("stop");
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
stopActionPerformed(evt);
System.out.println("Stop button pressed");
}
});
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 14;
gridBagConstraints.gridy = 32;
getContentPane().add(stop, gridBagConstraints);
pack();
}
//connect button press event
private void connectActionPerformed(ActionEvent evt) {
String show="Connect Button Pressed";
output.setText(show);
}
//submit button press event
private void submitActionPerformed(ActionEvent evt) {
String show="Submit Button Pressed";
output.setText(show);
}
//stop button press event
private void stopActionPerformed(ActionEvent evt) {
String show="Stop Button Pressed";
output.setText(show);
}
//main method
public static void main(String args[]) {
ClientGUI cg = new ClientGUI(args[0]);
cg.setVisible(true);
}
}
Solution by an expert tutor
