Results 1 to 3 of 3
- 02-27-2020, 04:20 AM #1
Member
- Join Date
- May 2015
- Posts
- 15
- Rep Power
- 0
Newb JPanel with rectangle and buttons
I'm trying to create a UI with a rectangle to draw lines on, and some buttons. Trying different approaches, I seem to be able to do one or the other, but not both. I used setLayout(null) because I want the buttons in a specific place. When I run it, I see the rectangle OK, but I only see one button. If I leave out the setLayout(null), then I see both buttons, but they are superimposed on the box. I just want to put the buttons in a specific location, but it seems I need setLayout(null) in order to do that. What am I missing?
Java Code:public class UI extends JPanel { private boolean bline; JButton ResetButton; JButton ZeroButton; final int iButtonWidth = 70; final int iButtonHeight = 70; final int iZeroButtonStartX = 100; final int iZeroButtonStartY = 200; final int iResetButtonStartX = 300; final int iResetButtonStartY = 200; public void paint (Graphics g) { g.setColor(Color.black); g.fillRect(iDisplayStartX, iDisplayStartY, iDisplayWidth, iDisplayHeight); g.drawLine(20, 20, 40, 40); } public UI () { setLayout(null); ResetButton = new JButton("Reset"); ResetButton.setBounds(iResetButtonStartX, iResetButtonStartY, iButtonWidth, iButtonHeight); ZeroButton = new JButton("0"); ZeroButton.setBounds(iZeroButtonStartX, iZeroButtonStartY, iButtonWidth, iButtonHeight); add(ResetButton); add(ZeroButton); revalidate(); } . . . public static void main() { JFrame frame = new JFrame("TITLE"); frame.add(new UI()); frame.setSize(iWindowSizeX, iWindowSizeY); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setResizable(false); }
- 03-02-2020, 11:31 AM #2
Re: Newb JPanel with rectangle and buttons
If you use a Null-layout, buttons need a bounds (width, height, x and y). If you set a layout of some sort, the LayoutManager will decide where the buttons go. By default, a JPanel uses a FlowLayout., so there is always a LayoutManager active for each new JPanel. Setting it to null will remove it.
First of all, you should override paintComponent(Graphics) and NOT paint(Graphics).
Second, public static void main requires String[] as parameter is you want this class to start."It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013
- 03-02-2020, 04:04 PM #3
Member
- Join Date
- May 2015
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Calculator: Buttons don't show up on Test JFrame class from JPanel class(w/ bg image)
By manibby93 in forum New To JavaReplies: 3Last Post: 12-08-2013, 10:28 PM -
How can I to print (printer) a JPanel and its componets (buttons and labels)?
By mad72584 in forum New To JavaReplies: 3Last Post: 08-15-2011, 02:56 PM -
Random box game : unsuccessful Buttons & Graphics listneres and JPanel Placement.
By AcousticBruce in forum New To JavaReplies: 3Last Post: 01-12-2011, 11:43 PM -
non-rectangle JPanel
By itaipee in forum AWT / SwingReplies: 4Last Post: 05-01-2009, 12:58 AM -
How to draw a rectangle in the JPanel by inserting the X and Y
By ngansamuel in forum New To JavaReplies: 2Last Post: 03-22-2009, 02:53 PM
Bookmarks