Display a Jdialog box when a Jbutton recieves mouse click
I am stuck on a problem where i click on a menu item and need it to bring up a JDialog Box but it wont bring it up. The coding i have researched and found that i should be using if im not mistaken is something like
if(evt.getSource == Jmenuitem1)
Jdialog.setVisible(true);
Jdialog.setBounds (400,0,400,400)
Below is the full coding for the class im working on. Why is it doing nothing when i click on the menu item.
package dvdlibrary2;
import javax.swing.JDialog;
public class UImain extends javax.swing.JFrame {
public UImain() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
input1 = new javax.swing.JDialog();
iTitle1 = new javax.swing.JLabel();
iGenre = new javax.swing.JLabel();
iRating = new javax.swing.JLabel();
iLength = new javax.swing.JLabel();
iDescription = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jComboBox1 = new javax.swing.JComboBox();
jComboBox2 = new javax.swing.JComboBox();
jTextField2 = new javax.swing.JTextField();
jScrollPane3 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane1 = new javax.swing.JScrollPane();
mlist = new javax.swing.JList();
jLabel1 = new javax.swing.JLabel();
dtitle = new javax.swing.JTextField();
jScrollPane2 = new javax.swing.JScrollPane();
ddescription = new javax.swing.JTextArea();
dgenre = new javax.swing.JTextField();
drating = new javax.swing.JTextField();
dlength = new javax.swing.JTextField();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
newdvd = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
input1.setBounds(new java.awt.Rectangle(400, 0, 400, 400));
input1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
input1.setVisible(false);
input1.setBounds(400,0,400,400);
iTitle1.setText("Title");
iGenre.setText("Genre");
iRating.setText("Rating");
iLength.setText("Length");
iDescription.setText("Description");
jTextField1.setText("jTextField1");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jTextField2.setText("jTextField2");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane3.setViewportView(jTextArea1);
javax.swing.GroupLayout input1Layout = new javax.swing.GroupLayout(input1.getContentPane());
input1.getContentPane().setLayout(input1Layout);
input1Layout.setHorizontalGroup(
input1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(input1Layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(input1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(input1Layout.createSequentialGroup()
.addComponent(iDescription)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(input1Layout.createSequentialGroup()
.addComponent(iLength)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField2))
.addGroup(input1Layout.createSequentialGroup()
.addComponent(iRating)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(input1Layout.createSequentialGroup()
.addComponent(iGenre)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(input1Layout.createSequentialGroup()
.addComponent(iTitle1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(159, Short.MAX_VALUE))
);
input1Layout.setVerticalGroup(
input1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(input1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(input1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(iTitle1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(input1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(iGenre)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(24, 24, 24)
.addGroup(input1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(iRating)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(input1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(iLength)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(input1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(input1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(iDescription)
.addContainerGap(102, Short.MAX_VALUE))
.addGroup(input1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREF开发者_如何学PythonERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
);
input1.getAccessibleContext().setAccessibleParent(newdvd);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
mlist.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(mlist);
jLabel1.setText("Movies In Library");
dtitle.setEditable(false);
ddescription.setColumns(20);
ddescription.setRows(5);
ddescription.setEnabled(false);
jScrollPane2.setViewportView(ddescription);
dgenre.setEditable(false);
drating.setEditable(false);
dlength.setEditable(false);
jMenu1.setText("File");
newdvd.setText("New DVD");
newdvd.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
newdvdMouseClicked(evt);
newdvdMouseClicked1(evt);
}
});
jMenu1.add(newdvd);
jMenuItem2.setText("About");
jMenu1.add(jMenuItem2);
jMenuItem3.setText("Preferences");
jMenu1.add(jMenuItem3);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(dtitle, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(10, 10, 10))
.addGroup(layout.createSequentialGroup()
.addGap(87, 87, 87)
.addComponent(dgenre, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(drating, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(dlength, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE))))
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(37, 37, 37))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(dtitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(dgenre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(drating, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dlength, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(6, 6, 6)))
.addContainerGap(55, Short.MAX_VALUE))
);
pack();
}
private void newdvdMouseClicked(java.awt.event.MouseEvent evt) {
if(evt.getSource()== newdvd)
input1.setVisible(true);
input1.setBounds(400,0,400,400);
}
private void newdvdMouseClicked1(java.awt.event.MouseEvent evt) {
}
public static void uimain() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UImain().setVisible(true);
}
});
}
private javax.swing.JTextArea ddescription;
private javax.swing.JTextField dgenre;
private javax.swing.JTextField dlength;
private javax.swing.JTextField drating;
private javax.swing.JTextField dtitle;
private javax.swing.JLabel iDescription;
private javax.swing.JLabel iGenre;
private javax.swing.JLabel iLength;
private javax.swing.JLabel iRating;
private javax.swing.JLabel iTitle1;
private javax.swing.JDialog input1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JList mlist;
private javax.swing.JMenuItem newdvd;
}
Test Results
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dvdlibrary2;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author jon
*/
public class MainTest {
public MainTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of main method, of class Main.
*/
@Test
public void testMain() {
System.out.println("main");
String[] args = null;
Main.main(args);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
}
Test of UImain class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dvdlibrary2;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author jon
*/
public class UImainTest {
public UImainTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of uimain method, of class UImain.
*/
@Test
public void testUimain() {
System.out.println("uimain");
UImain.uimain();
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
}
You should use ActionListener instead of MouseListener to process events. Change:
newdvd.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
newdvdMouseClicked(evt);
newdvdMouseClicked1(evt);
}
});
to:
newdvd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
input1.setVisible(true);
input1.setBounds(400,0,400,400);
}
});
And your dialog will show up.
Instead of using mouseClicked
event use:
public void mousePressed(java.awt.event.MouseEvent evt)
精彩评论