DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3

Thread: Swing GUI help

  1. #1
    Join Date
    Aug 2006
    Location
    STL, MO, USA
    Posts
    7

    Swing GUI help

    I just created a swing GUI, but I can not get it to work! I pretty much am using all the same code format as the java.sun.com website....
    Here it is:
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class converterUND implements ActionListener {
    
    JFrame frame;
    JPanel panel;
    JTextField textfield;
    JButton getAnswer;
    JLabel descriptionLabel, answerLabel;
    
    public mainMultiplier(){
    frame = new JFrame("Multiply a number by 2.");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(new Dimension(300, 40));
    panel = new JPanel(GridLayout(2,2));
    
    addObjects();
    
    frame.getRootPane().setDefaultButton(getAnswer);
    frame.getContentPane().add(panel, BorderLayout.CENTER);
    
    frame.pack();
    frame.setVisible(true);
    }
    private void addObjects(){
    textfield = new JTextField(2);
    descriptionLabel = new JLabel("Enter Number to be Multiplied", SwingConstants.LEFT);
    getAnswer = new JButton("Multiply");
    answerLabel = new JLabel("Answer:", SwingConstants.LEFT);
    
    getAnswer.addActionListener(this);
    
    panel.add(textfield);
    panel.add(descriptionLabel);
    panel.add(getAnswer);
    panel.add(answerLabel);
    
    descriptionLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    answerLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    }
    public void actionPerformed(ActionEvent event) {
    int IMPUT = (int)((Double.parseDouble(textfield.getText()))* 2.0);
    answerLabel.setText("Answer: " + IMPUT);
    }
    private static void showGUI(){
    converterUND NEW = new converterUND();
    }
    public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    showGUI();
    }
    });
    }
    }
    There is something wrong with this line:
    Code:
    public mainMultiplier(){
    It says it has an invalid return value. What should I make it? I've tried void, but I'm assuming that's incorrect. Please help!

  2. #2
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    There are (at least) two errors as I can see:
    Code:
    public mainMultiplier(){
    is wrong, it should be called the same as your class name (it's called a constructor):
    Code:
    public converterUND(){
    and

    Code:
    panel = new JPanel(GridLayout(2,2));
    should be:
    Code:
    panel = new JPanel(new GridLayout(2,2));

  3. #3
    Join Date
    Aug 2006
    Location
    STL, MO, USA
    Posts
    7
    Wow ty so much!

Similar Threads

  1. Replies: 2
    Last Post: 03-25-2006, 10:21 AM
  2. Replies: 2
    Last Post: 03-16-2006, 06:39 PM
  3. Java Swing GUI Resize Questions
    By lokelo in forum Java
    Replies: 2
    Last Post: 01-20-2006, 02:15 AM
  4. Swing GUI and Multithreading
    By Ketan Taylor in forum Java
    Replies: 0
    Last Post: 04-24-2003, 08:53 AM
  5. Swing is dead, long live SWT
    By Darren Bell in forum Talk to the Editors
    Replies: 2
    Last Post: 09-20-2002, 10:32 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links