A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

tabs

  • one year ago

any one who can help me with java swing... my panels are not appearing at the place I want them plzzz urgent help needed

  • This Question is Open
  1. tabs
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    package mytest; import javax.swing.BorderFactory; import javax.swing.GroupLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Testframe extends JFrame { private JPanel jp; private JPanel jp2; private JLabel jb; private JTextField jf; public Testframe() { //frame super ("Location box"); setSize(600,650); setLocation(400,50); //panel jp=new JPanel(); jp.setBorder(BorderFactory.createTitledBorder("NUMBER ADDITION")); jp.setForeground(new java.awt.Color(204, 204, 255)); javax.swing.GroupLayout jpLayout = new GroupLayout(jp); jp.setLayout(jpLayout); setVisible(true); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new Testframe(); } }

  2. e.mccormick
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    1) A code quotre system would make this a lot easier... copy paste does not work well without one on OS. 2) What exactly is wrong with how they get placed? 3) I do not see any code to tell the GroupLayout how you want things grouped. https://docs.oracle.com/javase/tutorial/uiswing/layout/group.html

  3. tabs
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    thnx for your reply.. actually in this case it doesn't matter which layout I use. my panel never appears. and if I apply border layout then it comes but anything I want to add in a panel say textfield never comes into the panel... plz help me through this

  4. e.mccormick
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    See what happens if you comment out the adding of the text field here: ``` public TestFrame() { //frame super ("Location box"); setSize(600,650); setLocation(400,50); //panel jp=new JPanel(); jp.setBorder(BorderFactory.createTitledBorder("NUMBER ADDITION")); jp.setForeground(new java.awt.Color(204, 204, 255)); javax.swing.GroupLayout jpLayout = new GroupLayout(jp); jp.setLayout(jpLayout); jb = new JLabel("Hello"); add(jb); final JTextField jf = new JTextField(6); add(jf); setVisible(true); } ```

  5. tabs
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    it brings jlabel if I comment out txt field... that's what happening things overwrite each other no panel is appearing no titled border either... can u help me make a code having two small panels having separate labels and textfields in a common frame.. plzzzz|dw:1434372890465:dw|

  6. e.mccormick
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Start from this and see what you can do: ``` import java.awt.Color; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.Border; public class FramePanelExample extends JFrame{ private JFrame mainFrame; private JLabel Label1; private JLabel Label2; private JPanel Panel1; private JPanel Panel2; public FramePanelExample(){ buildGUI(); } private void buildGUI(){ mainFrame = new JFrame("I am a frame!"); mainFrame.setSize(600,650); mainFrame.setLocation(400,50); mainFrame.setLayout(new GridLayout(2,1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); Border blackLine = BorderFactory.createLineBorder(Color.black); Panel1 = new JPanel(); Panel1.setBorder(BorderFactory.createTitledBorder(blackLine, "Panel One")); Panel2 = new JPanel(); Panel2.setBorder(BorderFactory.createTitledBorder(blackLine, "Panel Two")); Label1 = new JLabel("This is label one"); Label2 = new JLabel("This is label two"); Panel1.add(Label1); Panel2.add(Label2); mainFrame.add(Panel1); mainFrame.add(Panel2); mainFrame.setVisible(true); } public static void main(String[] args){ new FramePanelExample(); } } ```

  7. e.mccormick
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Oh, and you can move things around to make it more modular. That may or may not make a difference. For a simple program that is for learning, modularity is not a big deal. As you grow as a programmer you usually want things to be more modular, commented, etc. So your names should reflect what is happening so that nobody has to guess what the purpose of each part is. Let me modify mine a bit to show this. ``` import java.awt.Color; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.Border; public class FramePanelExample extends JFrame{ private JFrame mainFrame; private JLabel inputMessage; // Placeholder for input private JLabel outputMessage; // Placeholder for output private JPanel inputPanel; // Panel to hold all input controls private JPanel outputPanel; // Panel to display all output private Border blackLine = BorderFactory.createLineBorder(Color.black); public FramePanelExample(){ buildFrame(); } // end FramePanelExample() private void buildFrame(){ // sets up the main frame of the program mainFrame = new JFrame("Input to Output Program"); mainFrame.setSize(600,650); mainFrame.setLocation(400,50); mainFrame.setLayout(new GridLayout(2,1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); } // end buildFrame() private void addPannels(){ // adds panels to the program inputPanel = new JPanel(); inputPanel.setBorder(BorderFactory.createTitledBorder(blackLine, "Input")); outputPanel = new JPanel(); outputPanel.setBorder(BorderFactory.createTitledBorder(blackLine, "Output")); inputMessage = new JLabel("Input will go here."); outputMessage = new JLabel("Output will show here."); inputPanel.add(inputMessage); outputPanel.add(outputMessage); mainFrame.add(inputPanel); mainFrame.add(outputPanel); } // end addPannels() private void makeVisible(){ mainFrame.setVisible(true); } public static void main(String[] args){ FramePanelExample framePanelExample = new FramePanelExample(); framePanelExample.addPannels(); framePanelExample.makeVisible(); } } ```

  8. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.