Results 1 to 5 of 5
Thread: simple GUI problem
- 04-03-2009, 02:04 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 47
- Rep Power
- 0
simple GUI problem
It displays the GUI with the button saying "Click me!." I just want the text to change when the user clicks the button.
When I click the button, this pops up --Java Code:import javax.swing.*; import java.awt.event.*; public class Testing implements ActionListener{ JButton button; public static void main(String args[]) { Testing gui = new Testing(); gui.go(); } public void go() { JFrame frame = new JFrame(); JButton button = new JButton("Click me!"); button.addActionListener(this); frame.getContentPane().add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.setVisible(true); } public void actionPerformed(ActionEvent event) { button.setText("I've been clicked!"); } }
I'm pretty confused. I don't see why it wouldn't be working. Any help would be appreciated.Java Code:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Testing.actionPerformed(Testing.java:26) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
-
You are declaring "button" twice, once in the class where it is never initialized, never displayed, and once in the go method where it is initialized and displayed. the two button objects are completely unrelated. The actionPerformed method tries to manipulate the non-constructed button.
solution: declare the object only once, in the class.
So change this line:
to this:Java Code:JButton button = new JButton("Click me!"); // button object declared and constructed
Java Code:button = new JButton("Click me!");// button object just constructed
- 04-03-2009, 03:00 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 47
- Rep Power
- 0
Oh man sorry about that, I must've overlooked that :\.
I have another question now though. I tried to add to this a little bit. I wanted to add a rectangle to the pane along with the button. I'm trying to use the setSize(int width, int height) method for the button to make it smaller so that you can see both. However, the button still takes up the whole GUI and the rectangle is practically not there.
My two classes are as follows --
Java Code:import javax.swing.*; import java.awt.event.*; public class Testing implements ActionListener{ DrawPanel board = new DrawPanel(); JButton button; public static void main(String args[]) { Testing gui = new Testing(); gui.go(); } public void go() { JFrame frame = new JFrame(); button = new JButton("Click Me!"); button.addActionListener(this); button.setSize(25, 25); frame.getContentPane().add(board); frame.getContentPane().add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.setVisible(true); } public void actionPerformed(ActionEvent event) { button.setText("I've been clicked!."); } }Maybe the setSize method doesn't what I'm thinking it does? Or did I make a coding error somewhere?Java Code:import javax.swing.*; import java.awt.*; public class DrawPanel extends JPanel{ public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; int green = (int)(Math.random() * 256); int blue = (int) (Math.random() * 256); int red = (int) (Math.random() * 256); Color startColor = new Color(red, green, blue); green = (int)(Math.random() * 256); blue = (int) (Math.random() * 256); red = (int) (Math.random() * 256); Color endColor = new Color(red, green, blue); GradientPaint gradient = new GradientPaint(75, 75, startColor, 150, 150, endColor); g2d.setPaint(gradient); g.fillRect(70, 70, 100, 100); } }
-
um,... and you're welcome.
-
Recommendations
1) Avoid using setSize, and instead prefer using setPreferredSize
2) read up on the layout managers at the Sun layout manager tutorial. It will tell you that a JFrame (actually its contentPane) uses BorderLayout as its default layout manager and why this is causing the effect that you are seeing.
Similar Threads
-
Simple square root problem!
By nortski in forum New To JavaReplies: 7Last Post: 04-01-2009, 02:11 PM -
Simple IO problem
By aamp in forum New To JavaReplies: 2Last Post: 12-01-2008, 02:27 PM -
Simple JFrame Problem
By DC1 in forum New To JavaReplies: 4Last Post: 06-06-2008, 07:09 AM -
My Simple Refresh Problem
By pmcastillo in forum New To JavaReplies: 0Last Post: 03-26-2008, 07:59 AM -
problem with a simple java code
By boy22 in forum New To JavaReplies: 2Last Post: 08-03-2007, 02:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks