I have my code pretty much figured out but need a little hel
I have my code pretty much figured out but need a little help with the formatting of it.
I want my JOption popup to be a plain message with just the OK button on it. What do I need to do to fix that. Also, for some reason I am gettting a -1 autofilled in it and want it to be blank to enter my information.
Also, I want my add Course and Close course to be in the very bottom RIGHT of the page but I can\'t seem to get them in the correct location with a small space between them.
Here is the code that I have so far..Any ideas on how to fix it.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Labs extends JFrame {
private DefaultListModel courses;
private JList list;
public Labs()
{
JPanel p1=new JPanel();
p1.setLayout(new GridLayout(2,2));
p1.add(new JButton (\"Add Course\"));
// create a DefaultListModel to store courses
courses = new DefaultListModel();
// create a JList for courses DefaultListModel
list = new JList( courses );
JButton jbtAddCourse=new JButton(\"Add Course\");
jbtAddCourse.addActionListener(
new ActionListener() {
@Override
public void actionPerformed( ActionEvent event )
{
// prompt user for new course\'s name
String name = JOptionPane.showInputDialog(
Labfive.this, \"Enter Course\",JOptionPane.PLAIN_MESSAGE);
// add new course to model
courses.addElement( name );
}
}
);
// create JButton for removing selected course
JButton jbtcloseButton = new JButton( \"Close\" );
jbtcloseButton.addActionListener(
new ActionListener() {
@Override
public void actionPerformed( ActionEvent event )
{
// exit program
}
}
);
// lay out GUI components
JPanel inputPanel = new JPanel();
inputPanel.add( jbtAddCourse );
inputPanel.add( closeButton );
Container container = getContentPane();
container.add( list, BorderLayout.CENTER );
container.add( inputPanel, BorderLayout.SOUTH );
} // end JList constructor
public static void main( String args[] )
{
Labs frame= new Labs();
frame.setTitle(\"Labs\");
frame.setSize(400,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Solution
With this information you are ready to create a simple applet: