Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN JAVA Please help! I\'ve spent the last two days working on this and have gott

ID: 3829814 • Letter: I

Question

IN JAVA

Please help! I've spent the last two days working on this and have gotten nowhere. Please comment your code so I can understand how you did it.

Write the Java source code necessary to build a solution for the problem below:
In your new job you have been hired to be the user interface designer for the "We build it for you" company. Your job is to build a graphical user interface template so that the sales force can go out and demonstrate the possibilities for interfaces to many different types of customers. To that end you are then to create a GUI panel and draw six different shapes. Fill some of the shapes with your own choice of colors. Use the drawstring method to write "My Advanced GUI" on the panel.

Create three tab panels using JTabbedPane. In the first tab, create two buttons. Display a dialog box when the button is pressed indicating which button is pressed. In the second tab, display five radio buttons. Use a dialog box to indicate which radio button is selected. In the third tab, display four check boxes. Only one radio button should be selected at a time, but multiple checkboxes can be selected.

Use the JSplitPane to split the first GUI panel and the three tabbed panels.

Finally, add a menu bar to the GUI. The menu bar must have at least one item. Create the File menu, and add the exit sub menu to allow users to close the panel using the exit menu.

Explanation / Answer


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;


public class GUIPanel extends JFrame implements ActionListener,ItemListener{
JButton bt1,bt2,bt3;
JRadioButton rb1,rb2,rb3,rb4,rb5;
JCheckBox ch1,ch2,ch3,ch4;
ButtonGroup bg;
JPanel pl1,pl2,pl3,pl4;
JTabbedPane jtp;
public GUIPanel(){
bt1=new JButton("Button 1");
bt2=new JButton("Button 2");
bt3=new JButton("Button 3");
rb1=new JRadioButton("Button 1",true);
rb2=new JRadioButton("Button 2");
rb3=new JRadioButton("Button 3");
rb4=new JRadioButton("Button 4");
rb5=new JRadioButton("Button 5");
bg=new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
bg.add(rb4);
bg.add(rb5);
ch1=new JCheckBox("Checkbox 1");
ch2=new JCheckBox("Checkbox 2");
ch3=new JCheckBox("Checkbox 3");
ch4=new JCheckBox("Checkbox 4");
pl1=new JPanel();
pl1.add(bt1);
pl1.add(bt2);
pl2=new JPanel();
pl2.add(rb1);
pl2.add(rb2);
pl2.add(rb3);
pl2.add(rb4);
pl2.add(rb5);
pl3=new JPanel();
pl3.add(ch1);
pl3.add(ch2);
pl3.add(ch3);
pl3.add(ch4);
pl4=new JPanel();
pl4.add(bt3);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
rb1.addItemListener(this);
rb2.addItemListener(this);
rb3.addItemListener(this);
rb4.addItemListener(this);
rb5.addItemListener(this);
ch1.addActionListener(this);
ch2.addActionListener(this);
ch3.addActionListener(this);
ch4.addActionListener(this);
jtp = new JTabbedPane();
jtp.addTab("Tab 1", pl1);
jtp.addTab("Tab 2", pl2);
jtp.addTab("Tab 3", pl3);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(jtp);
splitPane.setBottomComponent(pl4);
add(splitPane, BorderLayout.CENTER);
}
public static void main(String args[]){
GUIPanel s=new GUIPanel();
s.setTitle("Your name");
s.setSize(800, 800);
s.setVisible(true);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent ae){
String str=ae.getActionCommand();
if(str.equals("Button 1")){
JOptionPane.showMessageDialog(this,"Button 1 is clicked");
}else if(str.equals("Button 2")){
JOptionPane.showMessageDialog(this,"Button 2 is clicked");   
}else if(str.equals("Button 3")){
JOptionPane.showMessageDialog(this,"Button 3 is clicked");   
}else{
String st="";
if(ch1.isSelected()){
st=st+"Checkbox 1 is selected ";   
}
if(ch2.isSelected()){
st=st+"Checkbox 2 is selected ";   
}
if(ch4.isSelected()){
st=st+"Checkbox 2 is selected ";   
}
if(ch3.isSelected()){
st=st+"Checkbox 2 is selected ";   
}
JOptionPane.showMessageDialog(this,st);
}

}
public void itemStateChanged(ItemEvent ie){

if(ie.getSource()==rb1 || ie.getSource()==rb2 ||ie.getSource()==rb3 || ie.getSource()==rb4 || ie.getSource()==rb5){
if(rb1.isSelected()){
JOptionPane.showMessageDialog(this,"RadioButton 1 is clicked");
}else if(rb2.isSelected()){
JOptionPane.showMessageDialog(this,"RadioButton 2 is clicked");
}else if(rb3.isSelected()){
JOptionPane.showMessageDialog(this,"RadioButton 3 is clicked");
}else if(rb4.isSelected()){
JOptionPane.showMessageDialog(this,"RadioButton 4 is clicked");
}else if(rb5.isSelected()){
JOptionPane.showMessageDialog(this,"RadioButton 5 is clicked");
}else{

}   
}
}
@Override
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.black);
g.drawString("My advance GUI", 150, 150);
g.drawRect(100, 250, 50, 50);
g.setColor(Color.blue);
g.fillRect(100, 350, 50, 100);
}
}