JAVA programming, panels problem?
-
For my JAVA class, we need to edit this program to include 3 additional buttons in the panel and have the three buttons (Square, Oval, Line) be located UNDER the three buttons (3d Rect, rounded Rect, and circle). I've added the buttons into the panel, but when I try to set the panels rows to 2 and its columns to 3, I just keep getting all the buttons in a single row. My assignment is to make a 2 row panel with working buttons as well as a "clear" button that will remove whatever is on the "canvas". Program Below (So far, oval, Square, and line buttons do nothing). /*************************************… * COURSE: CSC231 Computer Science and Programming II * Lab: Number 4 * FILE: Draw.java * TARGET: Java 5.0 and 6.0 ****************************************… // Import Core Java packages import java.awt.*; import java.awt.event.*; public class Draw extends Frame implements ActionListener, ItemListener { // Initial Frame size static final int WIDTH = 400; // frame width static final int HEIGHT = 300; // frame height // Color choices static final String COLOR_NAMES[] = {"None", "Red", "Blue", "Green"}; static final Color COLORS[] = {null, Color.red, Color.blue, Color.green}; // Button control Button circle; Button roundRec; Button threeDRec; Button square; Button oval; Button line; // Color choice box Choice colorChoice; // the canvas DrawCanvas canvas; /** * Constructor */ public Draw() { super("Java Draw"); setLayout(new BorderLayout()); // create panel for controls Panel topPanel = new Panel(new GridLayout(2, 3)); add(topPanel, BorderLayout.NORTH); // create button control Panel buttonPanel = new Panel(new FlowLayout(FlowLayout.LEFT)); topPanel.add(buttonPanel); circle = new Button("Circle"); buttonPanel.add(circle); roundRec = new Button("Rounded Rectangle"); buttonPanel.add(roundRec); threeDRec = new Button("3D Rectangle"); buttonPanel.add(threeDRec); square = new Button("Square"); buttonPanel.add(square); oval = new Button("Oval"); buttonPanel.add(oval); line = new Button("Line"); buttonPanel.add(line); // add button listener circle.addActionListener(this); roundRec.addActionListener(this); threeDRec.addActionListener(this); square.addActionListener(this); oval.addActionListener(this); line.addActionListener(this); // create panel for color choices Panel colorPanel = new Panel(new FlowLayout(FlowLayout.LEFT)); topPanel.add(colorPanel); Label label = new Label("Filled Color:"); colorPanel.add(label); colorChoice = new Choice(); for(int i=0; i<COLOR_NAMES.length; i++) { colorChoice.add(COLOR_NAMES[i]); } colorPanel.add(colorChoice); colorChoice.addItemListener(this); // create the canvas canvas = new DrawCanvas(); add(canvas, BorderLayout.CENTER); } // end of constructor /** * Implementing ActionListener */ public void actionPerformed(ActionEvent event) { if(event.getSource() == circle) { // circle button canvas.setShape(DrawCanvas.CIRCLE); } else if(event.getSource() == roundRec) { // rounded rectangle button canvas.setShape(DrawCanvas.ROUNDED_RECTA… } else if(event.getSource() == threeDRec) { // 3D rectangle button canvas.setShape(DrawCanvas.RECTANGLE_3D)… } else if(event.getSource() == square) { // square button canvas.setShape(DrawCanvas.SQUARE); } else if(event.getSource() == oval) { // oval button canvas.setShape(DrawCanvas.OVAL); } else if(event.getSource() == line) { // line button canvas.setShape(DrawCanvas.LINE); } } /** * Implementing ItemListener */ public void itemStateChanged(ItemEvent event) { Color color = COLORS[colorChoice.getSelectedIndex()]; canvas.setFilledColor(color); } /** * the main method */ public static void main(String[] argv) { // Create a frame Draw frame = new Draw(); frame.setSize(WIDTH, HEIGHT); frame.setLocation(150, 100); // add window closing listener frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) {
-
Answer:
You should separate your buttonsPanel into 2 separate panels. One panel will be your top row of buttons and the other the bottom row. And then add those panels to the buttonsPanel. So do something like this: // create button control Panel buttonPanel = new Panel(new FlowLayout(FlowLayout.LEFT)); topPanel.add(buttonPanel); //create subpanel for first row of buttons Panel firstRowButtons = new Panel(); circle = new Button("Circle"); firstRowButtons.add(circle); roundRec = new Button("Rounded Rectangle"); firstRowButtons.add(roundRec); threeDRec = new Button("3D Rectangle"); firstRowButtons.add(threeDRec); //create subpanel of the 2nd row of buttons Panel bottomRowButtons = new Panel(); square = new Button("Square"); bottomRowButtons .add(square); oval = new Button("Oval"); bottomRowButtons.add(oval); line = new Button("Line"); bottomRowButtons .add(line); //NOW add those 2 subpanels to the buttonspanel buttonsPanel.add(firstRowButtons) buttonsPanel.add(bottomRowButtons) and continue on with program
dellick1... at Yahoo! Answers Visit the source
Related Q & A:
- How to start programming a game in java?Best solution by Game Development
- What is the difference photovoltaic solar panels and thermal solar panels?Best solution by Yahoo! Answers
- Why am I having this problem with java or Windows Installer?Best solution by Yahoo! Answers
- Computer Science Java Programming?Best solution by AllExperts
- Is java or visual basic a machine level, low level, high level or binary level programming language?Best solution by Quora
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.