Results 1 to 11 of 11
Thread: JFrame button question!
- 05-18-2011, 02:05 PM #1
JFrame button question!
I'm working on a simple artificial intelligence program as my final project for my Java class. I am currently having trouble locating example code both in my textbook and on the internet. I am trying to make an existing window open when I click on a JButton in another window. Nothing I have tried has worked, so if someone could simply walk me through it or just post a code snippet i'd be much obliged! Thanks for your time and hopefully speedy response :)
- 05-18-2011, 02:08 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Is the original window open and minimized? Or closed?
- 05-18-2011, 02:09 PM #3
Member
- Join Date
- Jul 2008
- Location
- London (Kingsbury)
- Posts
- 41
- Rep Power
- 0
if you could post your code. it would be easy to assist you
-
- 05-18-2011, 02:12 PM #5
I'll post the code in a second, but the original window is minimized, we will have the user input his/her name and they will be stored in a text file. After they input their name we want the user to be able to click on a button that will open the next window we have. I'm compiling both mine and my partner's code thus far so....
Why you mad ;)?
- 05-18-2011, 02:14 PM #6
Oh and it's another JFrame we're trying to open, we've only worked at it for 2 days so our code is kinda dismal this far.
Why you mad ;)?
- 05-18-2011, 02:15 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
If you have a series of windows that should change as the user makes choices you may want to look into card layout.
Also, looking up "iconify java" may be helpful.Last edited by sunde887; 05-18-2011 at 02:18 PM.
- 05-18-2011, 02:22 PM #8
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author 655788
*/
public class NamePrompt extends javax.swing.JFrame
{
private String firstname;
FileWriter NameOutput = new FileWriter("UserNames.txt", true);
PrintWriter NamesOutput = new PrintWriter(NameOutput);
/** Creates new form NamePrompt */
public NamePrompt() throws IOException
{
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
first = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jProgressBar1 = new javax.swing.JProgressBar();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jLabel2.setText("First:");
first.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
firstnametextfieldactionevent(evt);
}
});
jLabel3.setText("Last:");
jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lastnametextfieldactionevent(evt);
}
});
jButton1.setText("Enter");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
enterbuttonActionPerformed(evt);
}
});
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton2.setText("Quit");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
jLabel1.setText("Name");
jMenu1.setText("File");
jMenuItem1.setAccelerator(javax.swing.KeyStroke.ge tKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setText("Quit");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
quitmenuoptionActionEvent(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jLabel1))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jLabel2)
.add(4, 4, 4)
.add(first, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 121, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jLabel3)
.add(4, 4, 4)
.add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 122, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(jButton1)
.add(68, 68, 68)
.add(jButton2))
.add(jProgressBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 200, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
.add(layout.createSequentialGroup()
.add(11, 11, 11)
.add(jLabel1)
.add(11, 11, 11)
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(2, 2, 2)
.add(jLabel2))
.add(first, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(15, 15, 15)
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(2, 2, 2)
.add(jLabel3))
.add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(22, 22, 22)
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
.add(jButton1)
.add(jButton2))
.add(11, 11, 11)
.add(jProgressBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void firstnametextfieldactionevent(java.awt.event.Actio nEvent evt) {
}
private void enterbuttonActionPerformed(java.awt.event.ActionEv ent evt) {
//MainGUI = new MainGUI();
String lastname = jTextField2.getText();
NamesOutput.printf("%s" + " %s" + "%n", firstname, lastname);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0); // TODO add your handling code here:
}
private void lastnametextfieldactionevent(java.awt.event.Action Event evt) {
// TODO add your handling code here:
}
private void quitmenuoptionActionEvent(java.awt.event.ActionEve nt evt) {
System.exit(0);
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
String firstname = first.getText();
String lastname = jTextField2.getText();
NamesOutput.printf("%s" + " %s" + "%n", firstname, lastname);
}
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run()
{
try {
new NamePrompt().setVisible(true);
} catch (IOException ex) {
Logger.getLogger(NamePrompt.class.getName()).log(L evel.SEVERE, null, ex);
}
}
});
}
public javax.swing.JTextField first;
public javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JProgressBar jProgressBar1;
public javax.swing.JTextField jTextField2;
private Object MainGUI() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
This is the code for our first JFrame Form, we have a similar window called MainGUI but that one is for the user inputting his questions or answer the questions the program will ask itWhy you mad ;)?
- 05-18-2011, 02:23 PM #9
oh and because we are Java nubs atm we're using the GUI Builder to save time xP
Why you mad ;)?
- 05-18-2011, 02:28 PM #10
It appears we've worked this kink out.... however, if you see any errors, which there probably are..... let me know <3
Why you mad ;)?
- 05-18-2011, 04:00 PM #11
Good luck with that </sarcasm>
Seriously, the visual designer is not a beginners' tool. You're liable to spend much more time learning its quirks and learning how tio implement needed functionality than if you learn to hand-code the GUI instead.
A seasoned Swing developer can and does improve his productivity with a builder, but that's only because he already knows and understands where all the code snippets go. And many professionals prefer to continue to hand-code their GUIs for the sake of flexibility, readability and ease of maintenance.
db
Similar Threads
-
how do i use jframe button to send input selection
By earnest in forum New To JavaReplies: 5Last Post: 04-04-2011, 08:46 PM -
Disable Maximize button in JFrame?
By Manish87 in forum AWT / SwingReplies: 0Last Post: 02-01-2011, 03:39 PM -
How to open new JFrame with clicking on button?
By phearless in forum AWT / SwingReplies: 1Last Post: 01-14-2011, 10:36 AM -
how to disable minimize and maximize button in jframe window
By santhosh_el in forum AWT / SwingReplies: 6Last Post: 11-22-2010, 11:55 AM -
JFrame question
By Try2Live4God in forum New To JavaReplies: 2Last Post: 10-15-2007, 01:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks